@furystack/inject
Advanced tools
Comparing version 0.1.2 to 1.0.0
import "reflect-metadata"; | ||
import { Injector } from "./Injector"; | ||
import { Constructable } from "./Types/Constructable"; | ||
export declare const Injectable: (injector?: Injector) => <T extends Constructable<any>>(ctor: T) => { | ||
new (...args: any[]): { | ||
[x: string]: any; | ||
}; | ||
} & T; | ||
export declare const Injectable: () => <T extends Constructable<any>>(ctor: T) => void; |
@@ -5,17 +5,10 @@ "use strict"; | ||
const Injector_1 = require("./Injector"); | ||
exports.Injectable = (injector = Injector_1.Injector.Default) => { | ||
exports.Injectable = () => { | ||
return (ctor) => { | ||
const meta = Reflect.getMetadata("design:paramtypes", ctor); | ||
meta && injector.meta.set(ctor.name, meta.map((param) => { | ||
injector.options.scope[param.name] = param; | ||
return param.name; | ||
meta && Injector_1.Injector.Default.meta.set(ctor, meta.map((param) => { | ||
return param; | ||
})); | ||
injector.options.scope[ctor.name] = ctor; | ||
return class extends ctor { | ||
constructor(...args) { | ||
super(...args); | ||
} | ||
}; | ||
}; | ||
}; | ||
//# sourceMappingURL=Injectable.js.map |
@@ -7,12 +7,9 @@ import { IDisposable } from "@sensenet/client-utils"; | ||
parent: Injector; | ||
scope: any; | ||
}; | ||
static Default: Injector; | ||
meta: Map<string, string[]>; | ||
meta: Map<Constructable<any>, Array<Constructable<any>>>; | ||
private cachedSingletons; | ||
private getCtorFromName; | ||
GetInstance<T>(ctor: Constructable<T>): T; | ||
SetInstance<T>(instance: T, key?: string): void; | ||
GetInstanceByName<T>(ctorName: string, ctor?: Constructable<T>, deps?: string[]): T; | ||
GetInstance<T>(ctor: Constructable<T>, dependencies?: Array<Constructable<T>>): T; | ||
SetInstance<T>(instance: T, key?: Constructable<any>): void; | ||
constructor(options?: Partial<Injector["options"]>); | ||
} |
@@ -7,3 +7,2 @@ "use strict"; | ||
parent: Injector.Default, | ||
scope: typeof global !== "undefined" ? global : typeof window !== undefined ? window : {}, | ||
}; | ||
@@ -24,41 +23,21 @@ this.meta = new Map(); | ||
} | ||
getCtorFromName(ctorName) { | ||
const ctor = this.options.scope[ctorName]; | ||
if (!ctor || typeof ctor !== "function") { | ||
throw Error(`Constructor not found in scope for '${ctorName}'`); | ||
GetInstance(ctor, dependencies = []) { | ||
if (dependencies.includes(ctor)) { | ||
throw Error(`Circular dependencies found.`); | ||
} | ||
return ctor; | ||
} | ||
GetInstance(ctor) { | ||
return this.GetInstanceByName(ctor.name, ctor); | ||
} | ||
SetInstance(instance, key = instance.constructor.name) { | ||
this.cachedSingletons.set(key, instance); | ||
} | ||
GetInstanceByName(ctorName, ctor, deps = []) { | ||
if (deps.includes(ctorName)) { | ||
throw Error(`Circular dependency when resolving '${ctorName}', dependency chain is: "${deps.join(" -> ")}"`); | ||
if (this.cachedSingletons.has(ctor)) { | ||
return this.cachedSingletons.get(ctor); | ||
} | ||
if (this.cachedSingletons.has(ctorName)) { | ||
return this.cachedSingletons.get(ctorName); | ||
} | ||
const fromParent = this.options.parent && this.options.parent.options.scope[ctorName] && this.options.parent.GetInstanceByName(ctorName, ctor, deps); | ||
const fromParent = this.options.parent && this.options.parent.GetInstance(ctor); | ||
if (fromParent) { | ||
return fromParent; | ||
} | ||
if (!ctor) { | ||
ctor = this.getCtorFromName(ctorName); | ||
} | ||
const meta = this.meta.get(ctorName); | ||
if (!meta) { | ||
// no meta, create w/o arguments | ||
const instanceWoMeta = new ctor(); | ||
this.cachedSingletons.set(ctorName, instanceWoMeta); | ||
return instanceWoMeta; | ||
} | ||
const argCtors = meta.map((arg) => this.GetInstanceByName(arg, undefined, [...deps, ctorName])); | ||
const instance = new ctor(...argCtors); | ||
this.cachedSingletons.set(ctorName, instance); | ||
return instance; | ||
const deps = (this.meta.get(ctor) || []).map((dep) => this.GetInstance(dep, [...dependencies, ctor])); | ||
const newInstance = new ctor(...deps); | ||
this.SetInstance(newInstance); | ||
return newInstance; | ||
} | ||
SetInstance(instance, key) { | ||
this.cachedSingletons.set(key || instance.constructor, instance); | ||
} | ||
} | ||
@@ -65,0 +44,0 @@ Injector.Default = new Injector({ parent: undefined }); |
{ | ||
"name": "@furystack/inject", | ||
"version": "0.1.2", | ||
"version": "1.0.0", | ||
"description": "Core FuryStack package", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.js", |
@@ -5,16 +5,9 @@ import "reflect-metadata"; | ||
export const Injectable = (injector: Injector= Injector.Default) => { | ||
export const Injectable = () => { | ||
return <T extends Constructable<any>>(ctor: T) => { | ||
const meta = Reflect.getMetadata("design:paramtypes", ctor); | ||
meta && injector.meta.set(ctor.name, (meta as any[]).map((param) => { | ||
injector.options.scope[param.name] = param; | ||
return param.name; | ||
meta && Injector.Default.meta.set(ctor, (meta as any[]).map((param) => { | ||
return param; | ||
})); | ||
injector.options.scope[ctor.name] = ctor; | ||
return class extends ctor { | ||
constructor(...args: any[]) { | ||
super(...args); | ||
} | ||
}; | ||
}; | ||
}; |
@@ -16,58 +16,31 @@ import { IDisposable } from "@sensenet/client-utils"; | ||
public options: { parent: Injector, scope: any } = { | ||
public options: { parent: Injector } = { | ||
parent: Injector.Default, | ||
scope: typeof global !== "undefined" ? global : typeof window !== undefined ? window : {}, | ||
}; | ||
public static Default: Injector = new Injector({ parent: undefined }); | ||
public meta: Map<string, string[]> = new Map(); | ||
public meta: Map<Constructable<any>, Array<Constructable<any>>> = new Map(); | ||
private cachedSingletons: Map<string, any> = new Map(); | ||
private cachedSingletons: Map<Constructable<any>, any> = new Map(); | ||
private getCtorFromName(ctorName: string) { | ||
const ctor = this.options.scope[ctorName]; | ||
if (!ctor || typeof ctor !== "function") { | ||
throw Error(`Constructor not found in scope for '${ctorName}'`); | ||
public GetInstance<T>(ctor: Constructable<T>, dependencies: Array<Constructable<T>> = []): T { | ||
if (dependencies.includes(ctor)) { | ||
throw Error(`Circular dependencies found.`); | ||
} | ||
return ctor as Constructable<any>; | ||
} | ||
public GetInstance<T>(ctor: Constructable<T>) { | ||
return this.GetInstanceByName<T>(ctor.name, ctor); | ||
} | ||
public SetInstance<T>(instance: T, key: string = instance.constructor.name) { | ||
this.cachedSingletons.set(key, instance); | ||
} | ||
public GetInstanceByName<T>(ctorName: string, ctor?: Constructable<T>, deps: string[] = []): T { | ||
if (deps.includes(ctorName)) { | ||
throw Error(`Circular dependency when resolving '${ctorName}', dependency chain is: "${deps.join(" -> ")}"`); | ||
if (this.cachedSingletons.has(ctor)) { | ||
return this.cachedSingletons.get(ctor) as T; | ||
} | ||
if (this.cachedSingletons.has(ctorName)) { | ||
return this.cachedSingletons.get(ctorName) as T; | ||
} | ||
const fromParent = this.options.parent && this.options.parent.options.scope[ctorName] && this.options.parent.GetInstanceByName<T>(ctorName, ctor, deps); | ||
const fromParent = this.options.parent && this.options.parent.GetInstance(ctor); | ||
if (fromParent) { | ||
return fromParent; | ||
} | ||
const deps = (this.meta.get(ctor) || []).map((dep) => this.GetInstance(dep, [...dependencies, ctor])); | ||
const newInstance = new ctor(...deps); | ||
this.SetInstance(newInstance); | ||
return newInstance; | ||
if (!ctor) { | ||
ctor = this.getCtorFromName(ctorName); | ||
} | ||
} | ||
const meta = this.meta.get(ctorName); | ||
if (!meta) { | ||
// no meta, create w/o arguments | ||
const instanceWoMeta = new ctor(); | ||
this.cachedSingletons.set(ctorName, instanceWoMeta); | ||
return instanceWoMeta; | ||
} | ||
const argCtors: Array<Constructable<any>> = meta.map((arg) => this.GetInstanceByName(arg, undefined, [...deps, ctorName])); | ||
const instance = new ctor(...argCtors) as T; | ||
this.cachedSingletons.set(ctorName, instance); | ||
return instance; | ||
public SetInstance<T>(instance: T, key?: Constructable<any>) { | ||
this.cachedSingletons.set(key || instance.constructor as Constructable<T>, instance); | ||
} | ||
@@ -74,0 +47,0 @@ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
0
29241
146