@furystack/inject
Advanced tools
Comparing version 1.0.5 to 1.0.8
export * from "./Injectable"; | ||
export * from "./Injector"; | ||
export * from "./Types"; | ||
//# sourceMappingURL=index.d.ts.map |
import "reflect-metadata"; | ||
import { Constructable } from "./Types/Constructable"; | ||
export declare const Injectable: () => <T extends Constructable<any>>(ctor: T) => void; | ||
export interface InjectableOptions { | ||
ResolveDependencies: boolean; | ||
} | ||
export declare const defaultInjectableOptions: InjectableOptions; | ||
export declare const Injectable: (options?: Partial<InjectableOptions> | undefined) => <T extends Constructable<any>>(ctor: T) => void; | ||
//# sourceMappingURL=Injectable.d.ts.map |
@@ -5,10 +5,17 @@ "use strict"; | ||
const Injector_1 = require("./Injector"); | ||
exports.Injectable = () => { | ||
exports.defaultInjectableOptions = { | ||
ResolveDependencies: true, | ||
}; | ||
exports.Injectable = (options) => { | ||
return (ctor) => { | ||
const meta = Reflect.getMetadata("design:paramtypes", ctor); | ||
meta && Injector_1.Injector.Default.meta.set(ctor, meta.map((param) => { | ||
return param; | ||
})); | ||
const metaValue = { | ||
Dependencies: meta && meta.map((param) => { | ||
return param; | ||
}) || [], | ||
Options: { ...exports.defaultInjectableOptions, ...options }, | ||
}; | ||
Injector_1.Injector.Default.meta.set(ctor, metaValue); | ||
}; | ||
}; | ||
//# sourceMappingURL=Injectable.js.map |
@@ -10,4 +10,8 @@ import { IDisposable } from "@sensenet/client-utils"; | ||
static Default: Injector; | ||
meta: Map<Constructable<any>, Array<Constructable<any>>>; | ||
meta: Map<Constructable<any>, { | ||
Dependencies: Array<Constructable<any>>; | ||
Options: import("./Injectable").InjectableOptions; | ||
}>; | ||
private cachedSingletons; | ||
Remove: <T>(ctor: Constructable<T>) => boolean; | ||
GetInstance<T>(ctor: Constructable<T>, local?: boolean, dependencies?: Array<Constructable<T>>): T; | ||
@@ -17,1 +21,2 @@ SetInstance<T>(instance: T, key?: Constructable<any>): void; | ||
} | ||
//# sourceMappingURL=Injector.d.ts.map |
@@ -11,2 +11,3 @@ "use strict"; | ||
this.cachedSingletons = new Map(); | ||
this.Remove = (ctor) => this.cachedSingletons.delete(ctor); | ||
this.options = { ...this.options, ...options }; | ||
@@ -27,2 +28,5 @@ } | ||
GetInstance(ctor, local = false, dependencies = []) { | ||
if (ctor === this.constructor) { | ||
return this; | ||
} | ||
if (dependencies.includes(ctor)) { | ||
@@ -38,3 +42,7 @@ throw Error(`Circular dependencies found.`); | ||
} | ||
const deps = (Injector.Default.meta.get(ctor) || []).map((dep) => this.GetInstance(dep, local, [...dependencies, ctor])); | ||
const meta = Injector.Default.meta.get(ctor); | ||
if (!meta) { | ||
throw Error(`No metadata found for '${ctor.name}'. Dependencies: ${dependencies.map((d) => d.name).join(",")} Be sure that it's decorated with '@Injectable()' or added explicitly with SetInstance()`); | ||
} | ||
const deps = meta.Options.ResolveDependencies ? meta.Dependencies.map((dep) => this.GetInstance(dep, false, [...dependencies, ctor])) : []; | ||
const newInstance = new ctor(...deps); | ||
@@ -45,2 +53,5 @@ this.SetInstance(newInstance); | ||
SetInstance(instance, key) { | ||
if (instance.constructor === this.constructor) { | ||
throw Error("Cannot set an injector instance as injectable"); | ||
} | ||
this.cachedSingletons.set(key || instance.constructor, instance); | ||
@@ -47,0 +58,0 @@ } |
export declare type Constructable<T> = (new (...args: any[]) => object) & (new (...args: any[]) => T); | ||
//# sourceMappingURL=Constructable.d.ts.map |
export * from "./Constructable"; | ||
//# sourceMappingURL=index.d.ts.map |
{ | ||
"name": "@furystack/inject", | ||
"version": "1.0.5", | ||
"version": "1.0.8", | ||
"description": "Core FuryStack package", | ||
@@ -14,6 +14,6 @@ "main": "dist/index.js", | ||
"test": "jest", | ||
"prebuild": "tslint --project tsconfig.json", | ||
"lint": "tslint --project tsconfig.json", | ||
"clean": "rimraf dist && rimraf coverage", | ||
"build": "tsc -b", | ||
"prepublishOnly": "npm run test && npm run build", | ||
"prepublishOnly": "npm run build", | ||
"publish:development": "npm run build && npm t && npm run typedoc && npm publish --tag development", | ||
@@ -59,3 +59,3 @@ "typedoc": "typedoc --mode file --out documentation src --tsconfig tsconfig.json --theme c:/Users/%USERNAME%/AppData/Roaming/npm/node_modules/@sensenet/typedoc-theme/sn-theme" | ||
"devDependencies": { | ||
"@types/jest": "^23.3.9", | ||
"@types/jest": "^23.3.11", | ||
"jest": "^23.6.0", | ||
@@ -73,3 +73,3 @@ "rimraf": "^2.6.1", | ||
"typings": "./dist/index.d.ts", | ||
"gitHead": "d4c394442cdc67c7ba1ae87225c0181336454e70" | ||
"gitHead": "5f79804932864b40d04209df5a8c8ec514e329e5" | ||
} |
@@ -5,9 +5,21 @@ import "reflect-metadata"; | ||
export const Injectable = () => { | ||
export interface InjectableOptions { | ||
ResolveDependencies: boolean; | ||
} | ||
export const defaultInjectableOptions: InjectableOptions = { | ||
ResolveDependencies: true, | ||
}; | ||
export const Injectable = (options?: Partial<InjectableOptions>) => { | ||
return <T extends Constructable<any>>(ctor: T) => { | ||
const meta = Reflect.getMetadata("design:paramtypes", ctor); | ||
meta && Injector.Default.meta.set(ctor, (meta as any[]).map((param) => { | ||
return param; | ||
})); | ||
const metaValue = { | ||
Dependencies: meta && (meta as any[]).map((param) => { | ||
return param; | ||
}) || [], | ||
Options: {...defaultInjectableOptions, ...options}, | ||
}; | ||
Injector.Default.meta.set(ctor, metaValue); | ||
}; | ||
}; |
@@ -11,6 +11,6 @@ import { IDisposable } from "@sensenet/client-utils"; | ||
.map(async (s) => { | ||
if (s.dispose) { | ||
await s.dispose(); | ||
} | ||
}); | ||
if (s.dispose) { | ||
await s.dispose(); | ||
} | ||
}); | ||
await Promise.all(disposeRequests); | ||
@@ -25,7 +25,12 @@ } | ||
public static Default: Injector = new Injector({ parent: undefined }); | ||
public meta: Map<Constructable<any>, Array<Constructable<any>>> = new Map(); | ||
public meta: Map<Constructable<any>, {Dependencies: Array<Constructable<any>>, Options: import ("./Injectable").InjectableOptions}> = new Map(); | ||
private cachedSingletons: Map<Constructable<any>, any> = new Map(); | ||
public Remove = <T>(ctor: Constructable<T>) => this.cachedSingletons.delete(ctor); | ||
public GetInstance<T>(ctor: Constructable<T>, local: boolean = false, dependencies: Array<Constructable<T>> = []): T { | ||
if (ctor === this.constructor) { | ||
return this as any as T; | ||
} | ||
if (dependencies.includes(ctor)) { | ||
@@ -41,10 +46,16 @@ throw Error(`Circular dependencies found.`); | ||
} | ||
const deps = (Injector.Default.meta.get(ctor) || []).map((dep) => this.GetInstance(dep, local, [...dependencies, ctor])); | ||
const meta = Injector.Default.meta.get(ctor); | ||
if (!meta) { | ||
throw Error(`No metadata found for '${ctor.name}'. Dependencies: ${dependencies.map((d) => d.name).join(",")} Be sure that it's decorated with '@Injectable()' or added explicitly with SetInstance()`); | ||
} | ||
const deps = meta.Options.ResolveDependencies ? meta.Dependencies.map((dep) => this.GetInstance(dep, false, [...dependencies, ctor])) : []; | ||
const newInstance = new ctor(...deps); | ||
this.SetInstance(newInstance); | ||
return newInstance; | ||
} | ||
public SetInstance<T>(instance: T, key?: Constructable<any>) { | ||
if (instance.constructor === this.constructor) { | ||
throw Error("Cannot set an injector instance as injectable"); | ||
} | ||
this.cachedSingletons.set(key || instance.constructor as Constructable<T>, instance); | ||
@@ -51,0 +62,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
34331
28
200