From e763c50687982a066e249371491ab31ef74faaef Mon Sep 17 00:00:00 2001 From: Andres Jimenez Date: Wed, 23 Nov 2016 13:46:14 -0400 Subject: [PATCH 1/5] initial commit --- index.d.ts | 2 +- index.ts | 14 +++++++------- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/index.d.ts b/index.d.ts index 117ced3..7d8f422 100644 --- a/index.d.ts +++ b/index.d.ts @@ -1 +1 @@ -export function BootScript(): any; \ No newline at end of file +export function Inject(): any; \ No newline at end of file diff --git a/index.ts b/index.ts index b9235d8..cd2911d 100644 --- a/index.ts +++ b/index.ts @@ -1,19 +1,19 @@ /** - * @author Jonathan Casarrubias - * @module BootScript Decorator + * @author Andres Jimenez Fork from @mean-expert/boot-script by Jonathan Casarrubias + * @module Inject Decorator * @license MIT * @description - * This decorator will return boot script instances - * This avoids the need of creating static bootscripts + * This decorator will return singleton instances + * This avoids the need of creating static Injects **/ -export function BootScript() { +export function Inject() { function f(target: any) { - function BootScriptInstance(reference: any) + function InjectInstance(reference: any) { return new target(reference); } - return BootScriptInstance; + return InjectInstance; } return f } From 51984aeb54e17fe7e13a95dbfbaa204b176ba5e4 Mon Sep 17 00:00:00 2001 From: Andres Jimenez Date: Wed, 23 Nov 2016 13:55:15 -0400 Subject: [PATCH 2/5] initial build index --- index.js | 17 ++++++++++++----- package.json | 5 +++-- 2 files changed, 15 insertions(+), 7 deletions(-) diff --git a/index.js b/index.js index cb99cf5..8318b70 100644 --- a/index.js +++ b/index.js @@ -1,12 +1,19 @@ "use strict"; -function BootScript() { +/** + * @author Andres Jimenez Fork from @mean-expert/boot-script by Jonathan Casarrubias + * @module Inject Decorator + * @license MIT + * @description + * This decorator will return singleton instances + * This avoids the need of creating static Injects + **/ +function Inject() { function f(target) { - function BootScriptInstance(reference) { + function InjectInstance(reference) { return new target(reference); } - return BootScriptInstance; + return InjectInstance; } return f; } -exports.BootScript = BootScript; -//# sourceMappingURL=index.js.map \ No newline at end of file +exports.Inject = Inject; diff --git a/package.json b/package.json index 699939a..96d4f66 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { - "name": "@mean-expert/boot-script", + "name": "@mean-expert/inject", "version": "1.0.0", - "description": "FireLoop BootScript Decorator", + "description": "FireLoop Inject Decorator", "main": "index.js", "types": "index.d.ts", "scripts": { @@ -22,6 +22,7 @@ ], "author": "Jonathan Casarrubias", "license": "MIT", + "bugs": { "url": "https://github.com/mean-expert-official/boot-script/issues" }, From 15eb9b99397e6db15c0689a07a7464a47a567827 Mon Sep 17 00:00:00 2001 From: Andres Jimenez Date: Tue, 29 Nov 2016 11:29:03 -0400 Subject: [PATCH 3/5] inject decorator logic --- index.ts | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/index.ts b/index.ts index cd2911d..815b79f 100644 --- a/index.ts +++ b/index.ts @@ -6,9 +6,16 @@ * This decorator will return singleton instances * This avoids the need of creating static Injects **/ -export function Inject() { - function f(target: any) +export default function Inject(injectionKey : any) { + function f(target: any ) { + const targetType : { __inject__? : any } = target.constructor; + + if (!targetType.hasOwnProperty('__inject__')) { + targetType.__inject__ = {}; + } + + targetType.__inject__ = injectionKey; function InjectInstance(reference: any) { return new target(reference); From e879f83673c002a12c3dbbce638fc489607cd0b5 Mon Sep 17 00:00:00 2001 From: Andres Jimenez Date: Tue, 29 Nov 2016 11:40:06 -0400 Subject: [PATCH 4/5] build tsc --- index.js | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/index.js b/index.js index 8318b70..3bc74ed 100644 --- a/index.js +++ b/index.js @@ -7,8 +7,13 @@ * This decorator will return singleton instances * This avoids the need of creating static Injects **/ -function Inject() { +function Inject(injectionKey) { function f(target) { + var targetType = target.constructor; + if (!targetType.hasOwnProperty('__inject__')) { + targetType.__inject__ = {}; + } + targetType.__inject__ = injectionKey; function InjectInstance(reference) { return new target(reference); } @@ -16,4 +21,5 @@ function Inject() { } return f; } -exports.Inject = Inject; +exports.__esModule = true; +exports["default"] = Inject; From a57e332325fca95be643ab50e2ec68489481f300 Mon Sep 17 00:00:00 2001 From: Andres Jimenez Date: Wed, 30 Nov 2016 13:16:28 -0400 Subject: [PATCH 5/5] refactoring for working boostraping app fireloop --- dependencies.ts | 4 ++ index.d.ts | 3 +- index.ts | 119 ++++++++++++++++++++++++++++++++++++++++++------ injectable.ts | 6 +++ 4 files changed, 118 insertions(+), 14 deletions(-) create mode 100644 dependencies.ts create mode 100644 injectable.ts diff --git a/dependencies.ts b/dependencies.ts new file mode 100644 index 0000000..cded41b --- /dev/null +++ b/dependencies.ts @@ -0,0 +1,4 @@ +import {Injectable} from './injectable'; +export interface Dependencies { + providers: Array; +} diff --git a/index.d.ts b/index.d.ts index 7d8f422..32db988 100644 --- a/index.d.ts +++ b/index.d.ts @@ -1 +1,2 @@ -export function Inject(): any; \ No newline at end of file +export function Inject(): any; +export function fireLoopBootstrap(): any; \ No newline at end of file diff --git a/index.ts b/index.ts index 815b79f..3bb83e8 100644 --- a/index.ts +++ b/index.ts @@ -1,3 +1,6 @@ +import { Dependencies } from './dependencies'; +import { Injectable } from './injectable'; + /** * @author Andres Jimenez Fork from @mean-expert/boot-script by Jonathan Casarrubias * @module Inject Decorator @@ -6,21 +9,111 @@ * This decorator will return singleton instances * This avoids the need of creating static Injects **/ -export default function Inject(injectionKey : any) { - function f(target: any ) - { - const targetType : { __inject__? : any } = target.constructor; - - if (!targetType.hasOwnProperty('__inject__')) { - targetType.__inject__ = {}; +let prepared: Array = []; + +export function Inject(dependencies: Dependencies): Function { + 'use strict'; + + let args: Array = []; + + Array.prototype.push.apply(args, dependencies.providers); + + // return the function specified by ts documentation + return (target: any) => { + + // the new constructor behaviour + let f: any = function (): any { + + // prepre holder for instances + let instances: Array = []; + + // retrive instance of the class + for (let entry of f._dependencies) { + instances.push(instanceiateDependency(f, entry)); + } + + // apply to original target constructor + target.apply(this, instances); + f._instance = this; + }; + + + // loop all the providers and attach who is providing them + for (let provider of dependencies.providers) { + + // check if any providing is there + if (provider._providing === undefined) { + provider._providing = []; + } + + // save reference to this class + provider._providing.push(f); } - targetType.__inject__ = injectionKey; - function InjectInstance(reference: any) - { - return new target(reference); + // copy prototype + f.prototype = target.prototype; + f._dependencies = args; + + prepared.push(f); + + return f; + }; +} + +function instanceiateDependency(caller: Injectable, target: Injectable): any { + + 'use strict'; + + // temp reference to instances + let temp: any; + + // check if an instances exists + if (target._instance === undefined) { + + // create new instance + target._instance = new target(); + + // check if any classes should be enabled first + if (target._providing !== undefined) { + + // if caller is not allowed basically + if (target._providing.indexOf(caller) === -1) { + + // create new instances of them first + for (let provider of target._providing) { + + temp = new provider(); + } + + // delete all stuff about providing + delete target._providing; + } } - return InjectInstance; } - return f + + return target._instance; } + +export function fireLoopBootstrap(main: Injectable): void { + + 'use strict'; + + let tmp: any; + + // start everything + for (let entry of prepared) { + + // start only if not allready started + if (entry._instance === undefined) { + + tmp = new entry(); + } + } + + // start instance + if (main._instance === undefined) { + this.main = new main(); + } else { + this.main = main._instance; + } +} \ No newline at end of file diff --git a/injectable.ts b/injectable.ts new file mode 100644 index 0000000..3cbbbff --- /dev/null +++ b/injectable.ts @@ -0,0 +1,6 @@ +export interface Injectable extends Function { + new(...args: Array): any; + _instance?: any; + _dependencies?: Array; + _providing?: Array; +}