Skip to content

Commit

Permalink
refactor: simplify error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
exuanbo committed Oct 19, 2024
1 parent 0886a13 commit 093259b
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 26 deletions.
11 changes: 5 additions & 6 deletions src/container.ts
Original file line number Diff line number Diff line change
Expand Up @@ -185,10 +185,8 @@ export class Container {

if (context.resolution.stack.has(provider)) {
const dependentRef = context.resolution.dependents.get(provider);
if (dependentRef) {
return dependentRef.current;
}
assert(false, ErrorMessage.CircularDependency);
assert(dependentRef, ErrorMessage.CircularDependency);
return dependentRef.current;
}

let resolvedScope = options?.scope || this.defaultScope;
Expand All @@ -203,8 +201,9 @@ export class Container {
});
try {
if (resolvedScope == Scope.Container) {
if (registration.instance) {
return registration.instance.current;
const instanceRef = registration.instance;
if (instanceRef) {
return instanceRef.current;
}
const instance = instantiate();
registration.instance = {current: instance};
Expand Down
1 change: 0 additions & 1 deletion src/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ export const ErrorMessage = {
ReservedToken: "reserved token",
UnresolvableToken: "unresolvable token",
CircularDependency: "circular dependency",
InvariantViolation: "invariant violation",
InjectOutsideOfContext: "inject outside of context",
} as const;

Expand Down
15 changes: 6 additions & 9 deletions src/inject.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,19 @@
import {assert, ErrorMessage} from "./errors";
import {useInjectionContext} from "./injection-context";
import {ensureInjectionContext} from "./injection-context";
import type {Token, TokenList} from "./token";
import {invariant} from "./utils/invariant";

export function inject<Values extends unknown[]>(...tokens: TokenList<Values>): Values[number];
export function inject<Value>(...tokens: Token<Value>[]): Value {
const context = useInjectionContext();
assert(context, ErrorMessage.InjectOutsideOfContext);
const context = ensureInjectionContext();
return context.container.resolve(...tokens);
}

export namespace inject {
export function by<Values extends unknown[]>(thisArg: any, ...tokens: TokenList<Values>): Values[number];
export function by<Value>(thisArg: any, ...tokens: Token<Value>[]): Value {
const context = useInjectionContext();
assert(context, ErrorMessage.InjectOutsideOfContext);
const context = ensureInjectionContext();
const currentFrame = context.resolution.stack.peek();
assert(currentFrame, ErrorMessage.InvariantViolation);
invariant(currentFrame);
const provider = currentFrame.provider;
context.resolution.dependents.set(provider, {current: thisArg});
try {
Expand All @@ -29,7 +27,6 @@ export namespace inject {

export function injectAll<Values extends unknown[]>(...tokens: TokenList<Values>): NonNullable<Values[number]>[];
export function injectAll<Value>(...tokens: Token<Value>[]): NonNullable<Value>[] {
const context = useInjectionContext();
assert(context, ErrorMessage.InjectOutsideOfContext);
const context = ensureInjectionContext();
return context.container.resolveAll(...tokens);
}
8 changes: 8 additions & 0 deletions src/injection-context.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type {Container} from "./container";
import {assert, ErrorMessage} from "./errors";
import type {InstanceRef} from "./instance";
import type {Provider} from "./provider";
import type {Scope} from "./scope";
Expand All @@ -25,3 +26,10 @@ export type ResolvedScope = Exclude<Scope, typeof Scope.Inherited>;

// @internal
export const [withInjectionContext, useInjectionContext] = createContext<InjectionContext>();

// @internal
export function ensureInjectionContext() {
const context = useInjectionContext();
assert(context, ErrorMessage.InjectOutsideOfContext);
return context;
}
6 changes: 6 additions & 0 deletions src/utils/invariant.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// @internal
export function invariant(condition: unknown): asserts condition {
if (!condition) {
throw new Error("invariant violation");
}
}
24 changes: 14 additions & 10 deletions src/utils/keyed-stack.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
import {invariant} from "./invariant";

// @internal
export class KeyedStack<K, V> {
private entries = new Array<{key: K; value: V}>();

private keys = new Set<K>();

push(key: K, value: V) {
this.entries.push({key, value});
this.keys.add(key);
has(key: K) {
return this.keys.has(key);
}

peek() {
const entry = this.entries.at(-1);
return entry?.value;
}

pop() {
Expand All @@ -16,12 +23,9 @@ export class KeyedStack<K, V> {
}
}

peek() {
const entry = this.entries.at(-1);
return entry?.value;
}

has(key: K) {
return this.keys.has(key);
push(key: K, value: V) {
invariant(!this.keys.has(key));
this.keys.add(key);
this.entries.push({key, value});
}
}

0 comments on commit 093259b

Please sign in to comment.