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
4 changes: 4 additions & 0 deletions dependencies.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import {Injectable} from './injectable';
export interface Dependencies {
providers: Array<Injectable>;
}
3 changes: 2 additions & 1 deletion index.d.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export function BootScript(): any;
export function Inject(): any;
export function fireLoopBootstrap(): any;
23 changes: 18 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,25 @@
"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(injectionKey) {
function f(target) {
function BootScriptInstance(reference) {
var targetType = target.constructor;
if (!targetType.hasOwnProperty('__inject__')) {
targetType.__inject__ = {};
}
targetType.__inject__ = injectionKey;
function InjectInstance(reference) {
return new target(reference);
}
return BootScriptInstance;
return InjectInstance;
}
return f;
}
exports.BootScript = BootScript;
//# sourceMappingURL=index.js.map
exports.__esModule = true;
exports["default"] = Inject;
124 changes: 112 additions & 12 deletions index.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,119 @@
import { Dependencies } from './dependencies';
import { Injectable } from './injectable';

/**
* @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() {
function f(target: any)
{
function BootScriptInstance(reference: any)
{
return new target(reference);
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 <any>BootScriptInstance;
}
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;
}
}
6 changes: 6 additions & 0 deletions injectable.ts
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>;
}
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -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": {
Expand All @@ -22,6 +22,7 @@
],
"author": "Jonathan Casarrubias",
"license": "MIT",

"bugs": {
"url": "https://github.com/mean-expert-official/boot-script/issues"
},
Expand Down