-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
281 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
node_modules |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
import {Injectable} from './injectable'; | ||
export interface Dependencies { | ||
providers: Array<Injectable>; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export function Inject(): any; | ||
export function fireLoopBootstrap(): any; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
"use strict"; | ||
/** | ||
* @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 | ||
**/ | ||
var prepared = []; | ||
function Inject(dependencies) { | ||
'use strict'; | ||
var args = []; | ||
Array.prototype.push.apply(args, dependencies.providers); | ||
// return the function specified by ts documentation | ||
return function (target) { | ||
// the new constructor behaviour | ||
var f = function () { | ||
// prepre holder for instances | ||
var instances = []; | ||
// retrive instance of the class | ||
for (var _i = 0, _a = f._dependencies; _i < _a.length; _i++) { | ||
var entry = _a[_i]; | ||
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 (var _i = 0, _a = dependencies.providers; _i < _a.length; _i++) { | ||
var provider = _a[_i]; | ||
// check if any providing is there | ||
if (provider._providing === undefined) { | ||
provider._providing = []; | ||
} | ||
// save reference to this class | ||
provider._providing.push(f); | ||
} | ||
// copy prototype | ||
f.prototype = target.prototype; | ||
f._dependencies = args; | ||
prepared.push(f); | ||
return f; | ||
}; | ||
} | ||
exports.Inject = Inject; | ||
function instanceiateDependency(caller, target) { | ||
'use strict'; | ||
// temp reference to instances | ||
var temp; | ||
// 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 (var _i = 0, _a = target._providing; _i < _a.length; _i++) { | ||
var provider = _a[_i]; | ||
temp = new provider(); | ||
} | ||
// delete all stuff about providing | ||
delete target._providing; | ||
} | ||
} | ||
} | ||
return target._instance; | ||
} | ||
function fireLoopBootstrap(main) { | ||
'use strict'; | ||
var tmp; | ||
// start everything | ||
for (var _i = 0, prepared_1 = prepared; _i < prepared_1.length; _i++) { | ||
var entry = prepared_1[_i]; | ||
// 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; | ||
} | ||
} | ||
exports.fireLoopBootstrap = fireLoopBootstrap; |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
import { Dependencies } from './dependencies'; | ||
import { Injectable } from './injectable'; | ||
|
||
/** | ||
* @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 | ||
**/ | ||
let prepared: Array<any> = []; | ||
|
||
export function Inject(dependencies: Dependencies): Function { | ||
'use strict'; | ||
|
||
let args: Array<Function> = []; | ||
|
||
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<any> = []; | ||
|
||
// 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); | ||
} | ||
|
||
// 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 target._instance; | ||
} | ||
|
||
|
||
/** | ||
* Propouse this function for the bootstrap instance of the app loopback/fireLoopBootstrap | ||
* with typescript, but dependens of migration server.js to typescript. | ||
* | ||
* @export | ||
* @param {Injectable} main | ||
*/ | ||
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
export interface Injectable extends Function { | ||
new(...args: Array<any>): any; | ||
_instance?: any; | ||
_dependencies?: Array<any>; | ||
_providing?: Array<any>; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
{ | ||
"name": "@mean-expert/inject", | ||
"version": "1.0.0", | ||
"description": "FireLoop Inject Decorator", | ||
"main": "index.js", | ||
"types": "index.d.ts", | ||
"scripts": { | ||
"test": "echo \"Error: no test specified\" && exit 1" | ||
}, | ||
"files": [ | ||
"index.js", | ||
"index.js.map", | ||
"index.d.ts", | ||
"package.json" | ||
], | ||
"repository": { | ||
"type": "git", | ||
"url": "git+https://github.com/mean-expert-official/boot-script.git" | ||
}, | ||
"keywords": [ | ||
"index.js", | ||
"index.js.map", | ||
"index.d.ts", | ||
"package.json" | ||
], | ||
"author": "Jonathan Casarrubias", | ||
"license": "MIT", | ||
|
||
"bugs": { | ||
"url": "https://github.com/mean-expert-official/boot-script/issues" | ||
}, | ||
"homepage": "https://github.com/mean-expert-official/boot-script#readme", | ||
"dependencies": {}, | ||
"devDependencies": {} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
{ | ||
"compilerOptions": { | ||
"module": "commonjs", | ||
"target": "es5", | ||
"moduleResolution": "node", | ||
"noImplicitAny": true, | ||
"removeComments": true, | ||
"preserveConstEnums": true, | ||
"sourceMap": true | ||
}, | ||
"files": [ | ||
"index.ts" | ||
] | ||
} |