@furystack/inject
Advanced tools
Comparing version 8.0.8 to 8.1.0
@@ -0,10 +1,5 @@ | ||
const hasInitMethod = (obj) => { | ||
return typeof obj.init === 'function'; | ||
}; | ||
export class Injector { | ||
constructor() { | ||
/** | ||
* Options object for an injector instance | ||
*/ | ||
this.options = {}; | ||
this.cachedSingletons = new Map(); | ||
this.remove = (ctor) => this.cachedSingletons.delete(ctor); | ||
} | ||
/** | ||
@@ -30,2 +25,13 @@ * Disposes the Injector object and all its disposable injectables | ||
/** | ||
* Options object for an injector instance | ||
*/ | ||
options = {}; | ||
/** | ||
* Static class metadata map, filled by the @Injectable() decorator | ||
*/ | ||
static options = new Map(); | ||
static injectableFields = new Map(); | ||
cachedSingletons = new Map(); | ||
remove = (ctor) => this.cachedSingletons.delete(ctor); | ||
/** | ||
* | ||
@@ -52,3 +58,3 @@ * @param ctor The constructor object (e.g. MyClass) | ||
} | ||
const { lifetime } = meta; | ||
const { lifetime = 'singleton' } = meta; | ||
const injectedFields = Object.entries(Injector.injectableFields.get(ctor) || {}); | ||
@@ -85,2 +91,5 @@ if (lifetime === 'singleton') { | ||
} | ||
if (hasInitMethod(newInstance)) { | ||
newInstance.init(this); | ||
} | ||
return newInstance; | ||
@@ -112,7 +121,2 @@ } | ||
} | ||
/** | ||
* Static class metadata map, filled by the @Injectable() decorator | ||
*/ | ||
Injector.options = new Map(); | ||
Injector.injectableFields = new Map(); | ||
//# sourceMappingURL=injector.js.map |
{ | ||
"name": "@furystack/inject", | ||
"version": "8.0.8", | ||
"version": "8.1.0", | ||
"description": "Core FuryStack package", | ||
@@ -40,9 +40,9 @@ "type": "module", | ||
"dependencies": { | ||
"@furystack/utils": "^4.0.8" | ||
"@furystack/utils": "^4.0.9" | ||
}, | ||
"devDependencies": { | ||
"typescript": "^5.1.6", | ||
"vitest": "^0.34.2" | ||
"typescript": "^5.2.2", | ||
"vitest": "^0.34.3" | ||
}, | ||
"gitHead": "1045d854bfd8c475b7035471d130d401417a2321" | ||
} |
@@ -170,3 +170,3 @@ import type { Disposable } from '@furystack/utils' | ||
expect(() => i.getInstance(UndecoratedTestClass, [Injector])).toThrowError( | ||
`No metadata found for 'UndecoratedTestClass'. Dependencies: _Injector. Be sure that it's decorated with '@Injectable()' or added explicitly with SetInstance()`, | ||
`No metadata found for 'UndecoratedTestClass'. Dependencies: Injector. Be sure that it's decorated with '@Injectable()' or added explicitly with SetInstance()`, | ||
) | ||
@@ -226,2 +226,17 @@ }) | ||
}) | ||
it('Should exec an init() method, if present', () => { | ||
@Injectable() | ||
class InitClass { | ||
public initWasCalled = false | ||
public init() { | ||
this.initWasCalled = true | ||
} | ||
} | ||
using(new Injector(), (i) => { | ||
const instance = i.getInstance(InitClass) | ||
expect(instance.initWasCalled).toBe(true) | ||
}) | ||
}) | ||
}) |
@@ -5,2 +5,6 @@ import type { Disposable } from '@furystack/utils' | ||
const hasInitMethod = (obj: Object): obj is { init: (injector: Injector) => void } => { | ||
return typeof (obj as any).init === 'function' | ||
} | ||
export class Injector implements Disposable { | ||
@@ -75,3 +79,3 @@ /** | ||
const { lifetime } = meta | ||
const { lifetime = 'singleton' } = meta | ||
@@ -116,2 +120,6 @@ const injectedFields = Object.entries(Injector.injectableFields.get(ctor) || {}) | ||
} | ||
if (hasInitMethod(newInstance)) { | ||
newInstance.init(this) | ||
} | ||
return newInstance | ||
@@ -118,0 +126,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
56841
652
Updated@furystack/utils@^4.0.9