@mediaclip/dependency-injection
Advanced tools
Comparing version 1.0.4 to 1.0.5
type Prettify<T> = T extends infer U ? { | ||
[K in keyof U]: U[K]; | ||
} : never; | ||
declare abstract class DiTokenBase<T> { | ||
declare class DiToken<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; | ||
scope: 'singleton'; | ||
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> { | ||
declare class ScopedDiToken<T> { | ||
readonly symbol: symbol; | ||
scope: 'scoped'; | ||
constructor(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>; | ||
static create<T>(name: string): ScopedDiToken<T>; | ||
@@ -23,18 +23,22 @@ } | ||
type TypedScopedInstanceFactory<T> = (container: IScopedDiContainer) => T; | ||
type ScopedDiOptions = { | ||
name: string; | ||
}; | ||
interface IDiContainer { | ||
registerSingleton<T>(token: DiToken<T>, valueOrFactory: TypedInstanceFactory<T> | T): void; | ||
registerScoped<T>(token: ScopedDiToken<T>, factory: TypedScopedInstanceFactory<T>): void; | ||
createScope(): IScopedDiContainer; | ||
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; | ||
createScope(options: ScopedDiOptions): IScopedDiContainer; | ||
isRegistered<T>(token: DiToken<T> | ScopedDiToken<T>): boolean; | ||
resolve<T>(token: DiToken<T>): T; | ||
tryResolve<T>(token: DiToken<T>): T | undefined; | ||
tryResolveScopedFactory<T>(token: ScopedDiToken<T>): TypedScopedInstanceFactory<T> | undefined; | ||
} | ||
interface IScopedDiContainer extends IDiContainer { | ||
registerScoped<T>(token: ScopedDiToken<T>, valueOrFactory: TypedScopedInstanceFactory<T> | T): void; | ||
resolve<T>(token: ScopedDiToken<T>): T; | ||
resolve<T>(token: ScopedDiToken<T> | DiToken<T>): T; | ||
} | ||
type DiContainerOption = { | ||
overridesBehaviour: "use-last" | "use-first" | "deny"; | ||
overridesBehaviour?: "use-last" | "use-first" | "deny"; | ||
name?: string; | ||
}; | ||
@@ -45,32 +49,35 @@ declare class DiContainer implements IDiContainer { | ||
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: 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; | ||
readonly containerOption: Readonly<DiContainerOption>; | ||
constructor(containerOption?: Readonly<DiContainerOption>); | ||
registerSingleton<T>(token: DiToken<T>, valueOrFactory: TypedInstanceFactory<T> | T): void; | ||
isSingletonFactory<T>(valueOrFactory: TypedInstanceFactory<T> | T): valueOrFactory is TypedInstanceFactory<T>; | ||
registerScoped<T>(token: ScopedDiToken<T>, factory: TypedScopedInstanceFactory<T>): void; | ||
isRegistered<T>(token: DiToken<T> | ScopedDiToken<T>): boolean; | ||
isSingletonRegistered<T>(token: DiToken<T> | ScopedDiToken<T>): boolean; | ||
isScopedRegistered<T>(token: DiToken<T> | ScopedDiToken<T>): boolean; | ||
ensureCanBeRegistered<T>(token: DiToken<T> | ScopedDiToken<T>, scope: 'singleton' | 'scoped'): void; | ||
resolve<T>(token: DiToken<T>): T; | ||
tryResolve<T>(token: DiToken<T>): T | undefined; | ||
tryResolveScopedFactory<T>(token: DiTokenBase<T>): TypedInstanceFactory<T> | undefined; | ||
createScope(): IScopedDiContainer; | ||
protected tryResolveFrom<T>(token: DiTokenBase<T>, registrations: Map<symbol, Array<T>>): T | undefined; | ||
tryResolveScopedFactory<T>(token: ScopedDiToken<T>): TypedScopedInstanceFactory<T> | undefined; | ||
createScope(options: ScopedDiOptions): IScopedDiContainer; | ||
protected tryResolveFrom<T>(token: DiToken<T> | ScopedDiToken<T>, registrations: Map<symbol, Array<T>>): T | undefined; | ||
private tryResolveSingletonFromFactory; | ||
private saveSingletonInstance; | ||
} | ||
declare class ScopedDiContainer extends DiContainer { | ||
declare class ScopedDiContainer extends DiContainer implements IScopedDiContainer { | ||
private readonly parentContainer; | ||
private readonly options?; | ||
private scopedInstances; | ||
constructor(parentContainer: IDiContainer); | ||
constructor(parentContainer: DiContainer | ScopedDiContainer, options?: ScopedDiOptions | undefined); | ||
registerScoped<T>(token: ScopedDiToken<T>, valueOrFactory: TypedScopedInstanceFactory<T> | T): void; | ||
isScopedRegistered<T>(token: DiTokenBase<T>): boolean; | ||
tryResolve<T>(token: DiTokenBase<T>): T | undefined; | ||
isScopedFactory<T>(valueOrFactory: TypedScopedInstanceFactory<T> | T): valueOrFactory is TypedScopedInstanceFactory<T>; | ||
isScopedRegistered<T>(token: DiToken<T> | ScopedDiToken<T>): boolean; | ||
tryResolve<T>(token: DiToken<T> | ScopedDiToken<T>, container?: ScopedDiContainer): T | undefined; | ||
tryResolveScopedFactory<T>(token: ScopedDiToken<T>): TypedScopedInstanceFactory<T> | undefined; | ||
tryResolveInstance<T>(token: ScopedDiToken<T>): T | undefined; | ||
private tryResolveScopedFromFactory; | ||
createScope(): IScopedDiContainer; | ||
createScope(options: ScopedDiOptions): IScopedDiContainer; | ||
private saveScopeInstances; | ||
} | ||
declare function isFunction<T>(valueOrFactory: T | ((container: IDiContainer) => T)): valueOrFactory is (container: IDiContainer) => T; | ||
export { DiContainer, type DiContainerOption, DiToken, DiTokenBase, type IDiContainer, type IScopedDiContainer, type InstanceFactory, type Prettify, ScopedDiContainer, ScopedDiToken, type ScopedInstanceFactory, type TypedInstanceFactory, type TypedScopedInstanceFactory, isFunction }; | ||
export { DiContainer, type DiContainerOption, DiToken, type IDiContainer, type IScopedDiContainer, type InstanceFactory, type Prettify, ScopedDiContainer, type ScopedDiOptions, ScopedDiToken, type ScopedInstanceFactory, type TypedInstanceFactory, type TypedScopedInstanceFactory }; |
@@ -1,15 +0,23 @@ | ||
// src/utils.ts | ||
function isFunction(valueOrFactory) { | ||
return typeof valueOrFactory === "function"; | ||
} | ||
var __defProp = Object.defineProperty; | ||
var __getOwnPropSymbols = Object.getOwnPropertySymbols; | ||
var __hasOwnProp = Object.prototype.hasOwnProperty; | ||
var __propIsEnum = Object.prototype.propertyIsEnumerable; | ||
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value; | ||
var __spreadValues = (a, b) => { | ||
for (var prop in b || (b = {})) | ||
if (__hasOwnProp.call(b, prop)) | ||
__defNormalProp(a, prop, b[prop]); | ||
if (__getOwnPropSymbols) | ||
for (var prop of __getOwnPropSymbols(b)) { | ||
if (__propIsEnum.call(b, prop)) | ||
__defNormalProp(a, prop, b[prop]); | ||
} | ||
return a; | ||
}; | ||
// 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; | ||
this.scope = "singleton"; | ||
} | ||
@@ -19,2 +27,5 @@ static create(name) { | ||
} | ||
static mergeTokens(a, b, c, d, e, f, g, h) { | ||
return Object.assign(a, b || {}, c || {}, d || {}, e || {}, f || {}, g || {}, h || {}); | ||
} | ||
}; | ||
@@ -24,3 +35,7 @@ var ScopedDiToken = class _ScopedDiToken { | ||
this.symbol = symbol; | ||
this.scope = "scoped"; | ||
} | ||
static mergeTokens(a, b, c, d, e, f, g, h) { | ||
return Object.assign(a, b || {}, c || {}, d || {}, e || {}, f || {}, g || {}, h || {}); | ||
} | ||
static create(name) { | ||
@@ -31,2 +46,7 @@ return new _ScopedDiToken(Symbol(name)); | ||
// src/utils.ts | ||
function ident(str, spaces) { | ||
return str.split("\n").join("\n" + spaces); | ||
} | ||
// src/di-container.ts | ||
@@ -38,9 +58,10 @@ var DiContainer = class { | ||
this.scopedFactories = /* @__PURE__ */ new Map(); | ||
this.containerOption = containerOption != null ? containerOption : { | ||
overridesBehaviour: "use-last" | ||
}; | ||
this.containerOption = __spreadValues({ | ||
overridesBehaviour: "use-last", | ||
name: void 0 | ||
}, containerOption); | ||
} | ||
registerSingleton(token, valueOrFactory) { | ||
this.ensureCanBeRegistered(token, "singleton"); | ||
if (isFunction(valueOrFactory)) { | ||
if (this.isSingletonFactory(valueOrFactory)) { | ||
let singletonFactories = this.singletonFactories.get(token.symbol); | ||
@@ -56,2 +77,5 @@ if (singletonFactories === void 0) { | ||
} | ||
isSingletonFactory(valueOrFactory) { | ||
return typeof valueOrFactory === "function"; | ||
} | ||
registerScoped(token, factory) { | ||
@@ -84,6 +108,12 @@ this.ensureCanBeRegistered(token, "scoped"); | ||
resolve(token) { | ||
const instance = this.tryResolve(token); | ||
if (instance === void 0) | ||
throw new Error(`The key '${token.symbol.toString()}' was not registered in the DI`); | ||
return instance; | ||
var _a, _b; | ||
try { | ||
const instance = this.tryResolve(token); | ||
if (instance === void 0) | ||
throw new Error(`The key '${token.symbol.toString()}' was not registered in the DI container named '${(_a = this.containerOption.name) != null ? _a : "unnamed"}'`); | ||
return instance; | ||
} catch (e) { | ||
throw new Error(`Error when resolving '${token.symbol.toString()}' from DI container named '${(_b = this.containerOption.name) != null ? _b : "unnamed"}': | ||
${ident(e.toString(), " ")}`); | ||
} | ||
} | ||
@@ -105,4 +135,4 @@ tryResolve(token) { | ||
} | ||
createScope() { | ||
return new ScopedDiContainer(this); | ||
createScope(options) { | ||
return new ScopedDiContainer(this, options); | ||
} | ||
@@ -144,9 +174,10 @@ tryResolveFrom(token, registrations) { | ||
var ScopedDiContainer = class _ScopedDiContainer extends DiContainer { | ||
constructor(parentContainer) { | ||
super(); | ||
constructor(parentContainer, options) { | ||
super(__spreadValues(__spreadValues({}, parentContainer.containerOption), options)); | ||
this.parentContainer = parentContainer; | ||
this.options = options; | ||
this.scopedInstances = /* @__PURE__ */ new Map(); | ||
} | ||
registerScoped(token, valueOrFactory) { | ||
if (isFunction(valueOrFactory)) | ||
if (this.isScopedFactory(valueOrFactory)) | ||
super.registerScoped(token, valueOrFactory); | ||
@@ -158,11 +189,14 @@ else { | ||
} | ||
isScopedFactory(valueOrFactory) { | ||
return typeof valueOrFactory === "function"; | ||
} | ||
isScopedRegistered(token) { | ||
return super.isScopedRegistered(token) || this.scopedInstances.has(token.symbol); | ||
} | ||
tryResolve(token) { | ||
const instance = this.tryResolveFrom(token, this.scopedInstances); | ||
if (instance) | ||
return instance; | ||
tryResolve(token, container) { | ||
if (token instanceof ScopedDiToken) { | ||
const newInstance = this.tryResolveScopedFromFactory(token); | ||
const instance = this.tryResolveInstance(token); | ||
if (instance) | ||
return instance; | ||
const newInstance = this.tryResolveScopedFromFactory(token, container); | ||
if (newInstance) | ||
@@ -172,4 +206,9 @@ return this.saveScopeInstances(token, newInstance); | ||
if (parentScopedFactory) { | ||
return this.saveScopeInstances(token, parentScopedFactory(this)); | ||
return this.saveScopeInstances(token, parentScopedFactory(container != null ? container : this)); | ||
} | ||
if (this.parentContainer instanceof _ScopedDiContainer) { | ||
const parentInstance = this.parentContainer.tryResolve(token, container); | ||
if (parentInstance) | ||
return parentInstance; | ||
} | ||
} else if (token instanceof DiToken) { | ||
@@ -180,6 +219,21 @@ const singletonInstance = super.tryResolve(token); | ||
} | ||
return this.parentContainer.tryResolve(token); | ||
} | ||
return this.parentContainer.tryResolve(token); | ||
} | ||
tryResolveScopedFromFactory(token) { | ||
tryResolveScopedFactory(token) { | ||
var _a; | ||
return (_a = this.tryResolveFrom(token, this.scopedFactories)) != null ? _a : this.parentContainer.tryResolveScopedFactory(token); | ||
} | ||
tryResolveInstance(token) { | ||
const instance = this.tryResolveFrom(token, this.scopedInstances); | ||
if (instance) | ||
return instance; | ||
if (this.parentContainer instanceof _ScopedDiContainer) { | ||
const parentInstance = this.parentContainer.tryResolveInstance(token); | ||
if (parentInstance) | ||
return parentInstance; | ||
} | ||
return void 0; | ||
} | ||
tryResolveScopedFromFactory(token, container) { | ||
const registeredElements = this.scopedFactories.get(token.symbol); | ||
@@ -193,8 +247,8 @@ if (registeredElements !== void 0 && registeredElements.length > 0) { | ||
} | ||
return factory(this); | ||
return factory(container != null ? container : this); | ||
} | ||
return void 0; | ||
} | ||
createScope() { | ||
return new _ScopedDiContainer(this); | ||
createScope(options) { | ||
return new _ScopedDiContainer(this, options); | ||
} | ||
@@ -214,7 +268,5 @@ saveScopeInstances(token, value) { | ||
DiToken, | ||
DiTokenBase, | ||
ScopedDiContainer, | ||
ScopedDiToken, | ||
isFunction | ||
ScopedDiToken | ||
}; | ||
//# sourceMappingURL=index.js.map |
@@ -7,3 +7,3 @@ { | ||
"name": "@mediaclip/dependency-injection", | ||
"version": "1.0.4", | ||
"version": "1.0.5", | ||
"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
69870
614