Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: fix ensure error catch by self #23

Merged
merged 1 commit into from
May 17, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 22 additions & 14 deletions src/injector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,8 @@ export class Injector {
* @returns The child injector.
*/
public createChild(dependencies?: Dependency[]): Injector {
this.ensureInjectorNotDisposed()
this._ensureInjectorNotDisposed()

return new Injector(dependencies, this)
}

Expand Down Expand Up @@ -168,7 +169,8 @@ export class Injector {
* @param dependency The dependency or an instance that would be add in the injector.
*/
public add<T>(dependency: DependencyOrInstance<T>): void {
this.ensureInjectorNotDisposed()
this._ensureInjectorNotDisposed()

const identifierOrCtor = dependency[0]
const item = dependency[1]

Expand Down Expand Up @@ -200,7 +202,7 @@ export class Injector {
* @param dependency The dependency that will replace the already existed dependency.
*/
public replace<T>(dependency: Dependency<T>): void {
this.ensureInjectorNotDisposed()
this._ensureInjectorNotDisposed()

const identifier = dependency[0]
if (this.resolvedDependencyCollection.has(identifier)) {
Expand All @@ -222,7 +224,7 @@ export class Injector {
* @param identifier The identifier of the dependency that is supposed to be deleted.
*/
public delete<T>(identifier: DependencyIdentifier<T>): void {
this.ensureInjectorNotDisposed()
this._ensureInjectorNotDisposed()

if (this.resolvedDependencyCollection.has(identifier)) {
throw new DeleteDependencyAfterResolutionError(identifier)
Expand All @@ -243,6 +245,8 @@ export class Injector {
cb: (accessor: IAccessor, ...args: P) => T,
...args: P
): T {
this._ensureInjectorNotDisposed()

const accessor: IAccessor = {
get: <D>(
id: DependencyIdentifier<D>,
Expand Down Expand Up @@ -303,6 +307,8 @@ export class Injector {
quantityOrLookup?: Quantity | LookUp,
lookUp?: LookUp
): T[] | T | null {
this._ensureInjectorNotDisposed();

try {
const newResult = this._get(id, quantityOrLookup, lookUp)
if ((Array.isArray(newResult) && newResult.some((r) => isAsyncHook(r))) || isAsyncHook(newResult)) {
Expand All @@ -325,8 +331,6 @@ export class Injector {
lookUp?: LookUp,
toSelf?: boolean
): T[] | T | AsyncHook<T> | null {
this.ensureInjectorNotDisposed()

let quantity: Quantity = Quantity.REQUIRED
if (
quantityOrLookup === Quantity.REQUIRED ||
Expand Down Expand Up @@ -358,7 +362,7 @@ export class Injector {
* Get a dependency in the async way.
*/
public getAsync<T>(id: DependencyIdentifier<T>): Promise<T> {
this.ensureInjectorNotDisposed()
this._ensureInjectorNotDisposed()

const cachedResult = this.getValue(id, Quantity.REQUIRED)
if (cachedResult !== NotInstantiatedSymbol) {
Expand All @@ -380,12 +384,12 @@ export class Injector {
ctor: new (...args: [...T, ...U]) => C,
...customArgs: T
): C {
this.ensureInjectorNotDisposed()
this._ensureInjectorNotDisposed()

return this._resolveClassImpl(ctor as Ctor<C>, ...customArgs)
}

private resolveDependency<T>(
private _resolveDependency<T>(
id: DependencyIdentifier<T>,
item: DependencyItem<T>,
shouldCache = true
Expand Down Expand Up @@ -451,7 +455,11 @@ export class Injector {
let thing: T

if (item.lazy) {
const idle = new IdleValue<T>(() => this._resolveClassImpl(ctor))
const idle = new IdleValue<T>(() => {
this._ensureInjectorNotDisposed()
return this._resolveClassImpl(ctor)
})

thing = new Proxy(Object.create(null), {
get(target: any, key: string | number | symbol): any {
if (key in target) {
Expand Down Expand Up @@ -631,7 +639,7 @@ export class Injector {
if (isAsyncDependencyItem(item)) {
throw new AsyncItemReturnAsyncItemError(id)
} else {
ret = this.resolveDependency(id, item) as T
ret = this._resolveDependency(id, item) as T
}
} else if (isCtor(thing)) {
ret = this._resolveClassImpl(thing)
Expand Down Expand Up @@ -698,9 +706,9 @@ export class Injector {

let ret: (T | AsyncHook<T>)[] | T | AsyncHook<T> | null = null
if (Array.isArray(registrations)) {
ret = registrations.map((dependencyItem) => this.resolveDependency(id, dependencyItem, shouldCache))
ret = registrations.map((dependencyItem) => this._resolveDependency(id, dependencyItem, shouldCache))
} else if (registrations) {
ret = this.resolveDependency(id, registrations, shouldCache)
ret = this._resolveDependency(id, registrations, shouldCache)
}

return ret
Expand Down Expand Up @@ -751,7 +759,7 @@ export class Injector {
this.resolutionOngoing -= 1
}

private ensureInjectorNotDisposed(): void {
private _ensureInjectorNotDisposed(): void {
if (this.disposed) {
throw new InjectorAlreadyDisposedError()
}
Expand Down
Loading