@mediaclip/dependency-injection
Advanced tools
Comparing version 1.0.3 to 1.0.4
type Prettify<T> = T extends infer U ? { | ||
[K in keyof U]: U[K]; | ||
} : never; | ||
declare class DiToken<T> { | ||
declare abstract class DiTokenBase<T> { | ||
readonly symbol: symbol; | ||
static mergeTokens<A extends object, B, C, D, E, F, G, H>(a: A, b?: B, c?: C, d?: D, e?: E, f?: F, g?: G, h?: H): Prettify<A & B & C & D & E & F & G & H>; | ||
} | ||
declare class DiToken<T> implements DiTokenBase<T> { | ||
readonly symbol: symbol; | ||
constructor(symbol: symbol); | ||
static create<T>(name: string): DiToken<T>; | ||
static mergeTokens<A extends object, B, C, D, E, F, G, H>(a: A, b?: B, c?: C, d?: D, e?: E, f?: F, g?: G, h?: H): Prettify<A & B & C & D & E & F & G & H>; | ||
} | ||
declare class ScopedDiToken<T> implements DiTokenBase<T> { | ||
readonly symbol: symbol; | ||
constructor(symbol: symbol); | ||
static create<T>(name: string): ScopedDiToken<T>; | ||
} | ||
type InstanceFactory = (container: IDiContainer) => any; | ||
type TypedInstanceFactory<T> = (container: IDiContainer) => T; | ||
type ScopedInstanceFactory = (container: IScopedDiContainer) => any; | ||
type TypedScopedInstanceFactory<T> = (container: IScopedDiContainer) => T; | ||
interface IDiContainer { | ||
registerSingleton<T>(token: DiToken<T>, valueOrFactory: TypedInstanceFactory<T> | T): void; | ||
registerScoped<T>(token: DiToken<T>, factory: TypedInstanceFactory<T>): void; | ||
registerScoped<T>(token: ScopedDiToken<T>, factory: TypedScopedInstanceFactory<T>): void; | ||
createScope(): IScopedDiContainer; | ||
isRegistered<T>(token: DiToken<T>): boolean; | ||
resolve<T>(token: DiToken<T>): T; | ||
tryResolve<T>(token: DiToken<T>): T | undefined; | ||
tryResolveScopedFactory<T>(token: DiToken<T>): TypedInstanceFactory<T> | undefined; | ||
isRegistered<T>(token: DiTokenBase<T>): boolean; | ||
resolve<T>(token: DiTokenBase<T>): T; | ||
tryResolve<T>(token: DiTokenBase<T>): T | undefined; | ||
tryResolveScopedFactory<T>(token: ScopedDiToken<T>): TypedInstanceFactory<T> | undefined; | ||
} | ||
interface IScopedDiContainer extends IDiContainer { | ||
registerScoped<T>(token: DiToken<T>, valueOrFactory: ((container: IDiContainer) => T) | T): void; | ||
registerScoped<T>(token: ScopedDiToken<T>, valueOrFactory: TypedScopedInstanceFactory<T> | T): void; | ||
resolve<T>(token: ScopedDiToken<T>): T; | ||
} | ||
@@ -32,17 +43,17 @@ | ||
protected singletonFactories: Map<symbol, Array<InstanceFactory>>; | ||
protected scopedFactories: Map<symbol, Array<InstanceFactory>>; | ||
protected scopedFactories: Map<symbol, Array<ScopedInstanceFactory>>; | ||
protected containerOption: DiContainerOption; | ||
constructor(containerOption?: DiContainerOption); | ||
registerSingleton<T>(token: DiToken<T>, valueOrFactory: ((container: IDiContainer) => T) | T): void; | ||
registerScoped<T>(token: DiToken<T>, factory: TypedInstanceFactory<T>): void; | ||
isRegistered<T>(token: DiToken<T>): boolean; | ||
isSingletonRegistered<T>(token: DiToken<T>): boolean; | ||
isScopedRegistered<T>(token: DiToken<T>): boolean; | ||
ensureCanBeRegistered<T>(token: DiToken<T>, scope: 'singleton' | 'scoped'): void; | ||
registerScoped<T>(token: ScopedDiToken<T>, factory: TypedInstanceFactory<T>): void; | ||
isRegistered<T>(token: DiTokenBase<T>): boolean; | ||
isSingletonRegistered<T>(token: DiTokenBase<T>): boolean; | ||
isScopedRegistered<T>(token: DiTokenBase<T>): boolean; | ||
ensureCanBeRegistered<T>(token: DiTokenBase<T>, scope: 'singleton' | 'scoped'): void; | ||
resolve<T>(token: DiToken<T>): T; | ||
tryResolve<T>(token: DiToken<T>): T | undefined; | ||
tryResolveScopedFactory<T>(token: DiToken<T>): TypedInstanceFactory<T> | undefined; | ||
tryResolveScopedFactory<T>(token: DiTokenBase<T>): TypedInstanceFactory<T> | undefined; | ||
createScope(): IScopedDiContainer; | ||
protected tryResolveFrom<T>(token: DiToken<T>, registrations: Map<symbol, Array<T>>): T | undefined; | ||
protected tryResolveFromFactory<T>(token: DiToken<T>, registrations: Map<symbol, Array<InstanceFactory>>): T | undefined; | ||
protected tryResolveFrom<T>(token: DiTokenBase<T>, registrations: Map<symbol, Array<T>>): T | undefined; | ||
private tryResolveSingletonFromFactory; | ||
private saveSingletonInstance; | ||
@@ -54,5 +65,6 @@ } | ||
constructor(parentContainer: IDiContainer); | ||
registerScoped<T>(token: DiToken<T>, valueOrFactory: TypedInstanceFactory<T> | T): void; | ||
isScopedRegistered<T>(token: DiToken<T>): boolean; | ||
tryResolve<T>(token: DiToken<T>): T | undefined; | ||
registerScoped<T>(token: ScopedDiToken<T>, valueOrFactory: TypedScopedInstanceFactory<T> | T): void; | ||
isScopedRegistered<T>(token: DiTokenBase<T>): boolean; | ||
tryResolve<T>(token: DiTokenBase<T>): T | undefined; | ||
private tryResolveScopedFromFactory; | ||
createScope(): IScopedDiContainer; | ||
@@ -64,2 +76,2 @@ private saveScopeInstances; | ||
export { DiContainer, type DiContainerOption, DiToken, type IDiContainer, type IScopedDiContainer, type InstanceFactory, type Prettify, ScopedDiContainer, type TypedInstanceFactory, isFunction }; | ||
export { DiContainer, type DiContainerOption, DiToken, DiTokenBase, type IDiContainer, type IScopedDiContainer, type InstanceFactory, type Prettify, ScopedDiContainer, ScopedDiToken, type ScopedInstanceFactory, type TypedInstanceFactory, type TypedScopedInstanceFactory, isFunction }; |
@@ -6,2 +6,25 @@ // src/utils.ts | ||
// src/token.ts | ||
var DiTokenBase = class { | ||
static mergeTokens(a, b, c, d, e, f, g, h) { | ||
return Object.assign(a, b || {}, c || {}, d || {}, e || {}, f || {}, g || {}, h || {}); | ||
} | ||
}; | ||
var DiToken = class _DiToken { | ||
constructor(symbol) { | ||
this.symbol = symbol; | ||
} | ||
static create(name) { | ||
return new _DiToken(Symbol(name)); | ||
} | ||
}; | ||
var ScopedDiToken = class _ScopedDiToken { | ||
constructor(symbol) { | ||
this.symbol = symbol; | ||
} | ||
static create(name) { | ||
return new _ScopedDiToken(Symbol(name)); | ||
} | ||
}; | ||
// src/di-container.ts | ||
@@ -66,3 +89,3 @@ var DiContainer = class { | ||
return instance; | ||
const newInstance = this.tryResolveFromFactory(token, this.singletonFactories); | ||
const newInstance = this.tryResolveSingletonFromFactory(token); | ||
if (newInstance) | ||
@@ -92,4 +115,4 @@ return this.saveSingletonInstance(token, newInstance); | ||
} | ||
tryResolveFromFactory(token, registrations) { | ||
const registeredElements = registrations.get(token.symbol); | ||
tryResolveSingletonFromFactory(token) { | ||
const registeredElements = this.singletonFactories.get(token.symbol); | ||
if (registeredElements !== void 0 && registeredElements.length > 0) { | ||
@@ -137,15 +160,31 @@ let factory; | ||
return instance; | ||
const newInstance = this.tryResolveFromFactory(token, this.scopedFactories); | ||
if (newInstance) | ||
return this.saveScopeInstances(token, newInstance); | ||
const parentScopedFactory = this.parentContainer.tryResolveScopedFactory(token); | ||
if (parentScopedFactory) { | ||
return this.saveScopeInstances(token, parentScopedFactory(this)); | ||
if (token instanceof ScopedDiToken) { | ||
const newInstance = this.tryResolveScopedFromFactory(token); | ||
if (newInstance) | ||
return this.saveScopeInstances(token, newInstance); | ||
const parentScopedFactory = this.parentContainer.tryResolveScopedFactory(token); | ||
if (parentScopedFactory) { | ||
return this.saveScopeInstances(token, parentScopedFactory(this)); | ||
} | ||
} else if (token instanceof DiToken) { | ||
const singletonInstance = super.tryResolve(token); | ||
if (singletonInstance) { | ||
return singletonInstance; | ||
} | ||
} | ||
const singletonInstance = super.tryResolve(token); | ||
if (singletonInstance) { | ||
return singletonInstance; | ||
} | ||
return this.parentContainer.tryResolve(token); | ||
} | ||
tryResolveScopedFromFactory(token) { | ||
const registeredElements = this.scopedFactories.get(token.symbol); | ||
if (registeredElements !== void 0 && registeredElements.length > 0) { | ||
let factory; | ||
if (this.containerOption.overridesBehaviour === "use-last") { | ||
factory = registeredElements[registeredElements.length - 1]; | ||
} else { | ||
factory = registeredElements[0]; | ||
} | ||
return factory(this); | ||
} | ||
return void 0; | ||
} | ||
createScope() { | ||
@@ -164,21 +203,10 @@ return new _ScopedDiContainer(this); | ||
}; | ||
// src/token.ts | ||
var DiToken = class _DiToken { | ||
constructor(symbol) { | ||
this.symbol = symbol; | ||
} | ||
static create(name) { | ||
return new _DiToken(Symbol(name)); | ||
} | ||
static mergeTokens(a, b, c, d, e, f, g, h) { | ||
return Object.assign(a, b || {}, c || {}, d || {}, e || {}, f || {}, g || {}, h || {}); | ||
} | ||
}; | ||
export { | ||
DiContainer, | ||
DiToken, | ||
DiTokenBase, | ||
ScopedDiContainer, | ||
ScopedDiToken, | ||
isFunction | ||
}; | ||
//# sourceMappingURL=index.js.map |
@@ -7,3 +7,3 @@ { | ||
"name": "@mediaclip/dependency-injection", | ||
"version": "1.0.3", | ||
"version": "1.0.4", | ||
"main": "./dist/index.js", | ||
@@ -10,0 +10,0 @@ "keywords": [ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
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
56764
508