diff --git a/core.ts b/core.ts index a303ce2..69c8e22 100644 --- a/core.ts +++ b/core.ts @@ -1,96 +1,8 @@ -import {ComponentMetadataService} from "./decorators/component/metadata.service"; - export * from "./decorators/ng-module"; export * from "./decorators/component"; export * from "./decorators/directive"; +export * from "./decorators/input"; +export * from "./decorators/output"; +export * from "./decorators/inject"; export * from "./decorators/lifecycle_hooks"; - -export function Input(alias?: string): PropertyDecorator { - return function (target: any, property: string | symbol) { - const componentMetadata = new ComponentMetadataService(target.constructor); - - if (!target.constructor.bindings) { - target.constructor.bindings = {}; - } - const propName = String(property); - const attrBinding = alias ? alias : propName; - - target.constructor.bindings[propName] = "<" + attrBinding; - componentMetadata.addInput(propName, attrBinding); - }; -} - -export function Output(alias?: string): PropertyDecorator { - return function (target: any, property: string | symbol) { - const componentMetadata = new ComponentMetadataService(target.constructor); - - if (!target.constructor.bindings) { - target.constructor.bindings = {}; - } - - const propName = String(property); - const privateCallbackName = `__${propName}`; - const attrBinding = alias ? alias : propName; - - target.constructor.bindings[privateCallbackName] = `&${attrBinding}`; - componentMetadata.addOutput(propName, attrBinding); - - Object.defineProperty(target, privateCallbackName, { - set: function (callback: Function) { - if (!this.__callbackCache) { - this.__callbackCache = {}; - } - - if (typeof callback === "function") { - this.__callbackCache[propName] = callback; - } - }, - enumerable: false, - configurable: true - }); - - Object.defineProperty(target, propName, { - set: function (eventEmitterInstance) { - if (!this.__eventEmitterCache) { - this.__eventEmitterCache = {}; - } - - if (eventEmitterInstance && eventEmitterInstance.subscribe) { - eventEmitterInstance.subscribe((eventData: any) => { - if (typeof this.__callbackCache[propName] === "function") { - this.__callbackCache[propName]({$event: eventData}); - } - }); - this.__eventEmitterCache[propName] = eventEmitterInstance; - } - }, - get: function () { - return this.__eventEmitterCache[propName]; - }, - enumerable: true, - configurable: true - }); - }; -} - -export function Inject(dependencyName: string): ParameterDecorator { - return function (target: any, property: string | symbol, parameterIndex: number) { - if (!target.$inject) { - target.$inject = []; - } - - target.$inject[parameterIndex] = dependencyName; - }; -} - -export class EventEmitter { - private listeners: Array = []; - - emit(event: any = null) { - this.listeners.forEach(callback => callback.call(null, event)); - } - - subscribe(callback: Function) { - this.listeners.push(callback); - } -} +export * from "./decorators/event_emitter"; diff --git a/decorators/event_emitter.ts b/decorators/event_emitter.ts new file mode 100644 index 0000000..9f33b2a --- /dev/null +++ b/decorators/event_emitter.ts @@ -0,0 +1,11 @@ +export class EventEmitter { + private listeners: Array<(event: T) => void> = []; + + emit(event?: T) { + this.listeners.forEach(callback => callback.call(null, event)); + } + + subscribe(callback: (event: T) => void) { + this.listeners.push(callback); + } +} diff --git a/decorators/inject/index.ts b/decorators/inject/index.ts new file mode 100644 index 0000000..583b0f3 --- /dev/null +++ b/decorators/inject/index.ts @@ -0,0 +1,9 @@ +export function Inject(dependencyName: string): ParameterDecorator { + return function (target: any, property: string | symbol, parameterIndex: number) { + if (!target.$inject) { + target.$inject = []; + } + + target.$inject[parameterIndex] = dependencyName; + }; +} diff --git a/decorators/input/index.ts b/decorators/input/index.ts new file mode 100644 index 0000000..af83a97 --- /dev/null +++ b/decorators/input/index.ts @@ -0,0 +1,16 @@ +import {ComponentMetadataService} from "../component/metadata.service"; + +export function Input(alias?: string): PropertyDecorator { + return function (target: any, property: string | symbol) { + const componentMetadata = new ComponentMetadataService(target.constructor); + + if (!target.constructor.bindings) { + target.constructor.bindings = {}; + } + const propName = String(property); + const attrBinding = alias ? alias : propName; + + target.constructor.bindings[propName] = "<" + attrBinding; + componentMetadata.addInput(propName, attrBinding); + }; +} diff --git a/decorators/output/index.ts b/decorators/output/index.ts new file mode 100644 index 0000000..130d92c --- /dev/null +++ b/decorators/output/index.ts @@ -0,0 +1,54 @@ +import {ComponentMetadataService} from "../component/metadata.service"; + +export function Output(alias?: string): PropertyDecorator { + return function (target: any, property: string | symbol) { + const componentMetadata = new ComponentMetadataService(target.constructor); + + if (!target.constructor.bindings) { + target.constructor.bindings = {}; + } + + const propName = String(property); + const privateCallbackName = `__${propName}`; + const attrBinding = alias ? alias : propName; + + target.constructor.bindings[privateCallbackName] = `&${attrBinding}`; + componentMetadata.addOutput(propName, attrBinding); + + Object.defineProperty(target, privateCallbackName, { + set: function (callback: Function) { + if (!this.__callbackCache) { + this.__callbackCache = {}; + } + + if (typeof callback === "function") { + this.__callbackCache[propName] = callback; + } + }, + enumerable: false, + configurable: true + }); + + Object.defineProperty(target, propName, { + set: function (eventEmitterInstance) { + if (!this.__eventEmitterCache) { + this.__eventEmitterCache = {}; + } + + if (eventEmitterInstance && eventEmitterInstance.subscribe) { + eventEmitterInstance.subscribe((eventData: any) => { + if (typeof this.__callbackCache[propName] === "function") { + this.__callbackCache[propName]({$event: eventData}); + } + }); + this.__eventEmitterCache[propName] = eventEmitterInstance; + } + }, + get: function () { + return this.__eventEmitterCache[propName]; + }, + enumerable: true, + configurable: true + }); + }; +} diff --git a/test/inject.test.ts b/test/inject.test.ts index dc1d3d8..7e10662 100644 --- a/test/inject.test.ts +++ b/test/inject.test.ts @@ -4,14 +4,17 @@ class Test { constructor( @Inject("service") service: Function, @Inject("service2") service2: Function - ) {} + ) { + } } -describe("Inject decorator", function() { +describe("Inject decorator", function () { let instance: any; - beforeEach(function() { - instance = new Test(() => {}, () => {}); + beforeEach(function () { + instance = new Test(() => { + }, () => { + }); }); test("should create $inject", function () { diff --git a/test/input.test.ts b/test/input.test.ts index 9433235..7bc515b 100644 --- a/test/input.test.ts +++ b/test/input.test.ts @@ -5,7 +5,7 @@ class Test { @Input("prop2") prop: string; } -describe("Input decorator", function() { +describe("input decorator", function() { let instance: any; beforeEach(function() { diff --git a/test/output.test.ts b/test/output.test.ts index 562f0d7..f57fa02 100644 --- a/test/output.test.ts +++ b/test/output.test.ts @@ -10,11 +10,11 @@ class TestWithManyOutputs { @Output("cb2") callback2: Function; } -describe("Output decorator", function() { +describe("Output decorator", function () { let testOneOutput: any, testManyOutputs: any; - beforeEach(function() { + beforeEach(function () { testOneOutput = new TestWithOneOutput(); testManyOutputs = new TestWithManyOutputs(); });