Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
96 changes: 4 additions & 92 deletions core.ts
Original file line number Diff line number Diff line change
@@ -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<Function> = [];

emit(event: any = null) {
this.listeners.forEach(callback => callback.call(null, event));
}

subscribe(callback: Function) {
this.listeners.push(callback);
}
}
export * from "./decorators/event_emitter";
11 changes: 11 additions & 0 deletions decorators/event_emitter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
export class EventEmitter<T extends any = any> {
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);
}
}
9 changes: 9 additions & 0 deletions decorators/inject/index.ts
Original file line number Diff line number Diff line change
@@ -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;
};
}
16 changes: 16 additions & 0 deletions decorators/input/index.ts
Original file line number Diff line number Diff line change
@@ -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);
};
}
54 changes: 54 additions & 0 deletions decorators/output/index.ts
Original file line number Diff line number Diff line change
@@ -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
});
};
}
11 changes: 7 additions & 4 deletions test/inject.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 () {
Expand Down
2 changes: 1 addition & 1 deletion test/input.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ class Test {
@Input("prop2") prop: string;
}

describe("Input decorator", function() {
describe("input decorator", function() {
let instance: any;

beforeEach(function() {
Expand Down
4 changes: 2 additions & 2 deletions test/output.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});
Expand Down