Skip to content

Commit

Permalink
refactor: rm usage of namespace
Browse files Browse the repository at this point in the history
  • Loading branch information
exuanbo committed Oct 21, 2024
1 parent ebde22e commit dd98606
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 18 deletions.
1 change: 1 addition & 0 deletions eslint.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ export default tseslint.config(
"@typescript-eslint/no-unsafe-assignment": "off",
"@typescript-eslint/no-unsafe-function-type": "off",
"@typescript-eslint/no-unsafe-return": "off",
"no-var": "off",
},
},
{
Expand Down
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ export type {ContainerOptions} from "./container";
export {Container} from "./container";
export type {ClassDecorator, ClassFieldDecorator, ClassFieldInitializer} from "./decorators";
export {AutoRegister, Inject, Injectable, InjectAll, Scoped} from "./decorators";
export {inject, injectAll} from "./inject";
export {inject, injectAll, injectBy} from "./inject";
export type {InstanceRef} from "./instance";
export type {ClassProvider, FactoryProvider, Provider, ValueProvider} from "./provider";
export type {Registration, RegistrationMap, RegistrationOptions, Registry} from "./registry";
Expand Down
32 changes: 18 additions & 14 deletions src/inject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,24 @@ export function inject<Value>(...tokens: Token<Value>[]): Value {
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 = ensureInjectionContext(inject);
const currentFrame = context.resolution.stack.peek();
invariant(currentFrame);
const provider = currentFrame.provider;
context.resolution.dependents.set(provider, {current: thisArg});
try {
return inject(...tokens);
}
finally {
context.resolution.dependents.delete(provider);
}
export declare namespace inject {
export var by: typeof injectBy;
}

inject.by = injectBy;

export function injectBy<Values extends unknown[]>(thisArg: any, ...tokens: TokenList<Values>): Values[number];
export function injectBy<Value>(thisArg: any, ...tokens: Token<Value>[]): Value {
const context = ensureInjectionContext(injectBy);
const currentFrame = context.resolution.stack.peek();
invariant(currentFrame);
const provider = currentFrame.provider;
context.resolution.dependents.set(provider, {current: thisArg});
try {
return inject(...tokens);
}
finally {
context.resolution.dependents.delete(provider);
}
}

Expand Down
9 changes: 6 additions & 3 deletions src/token.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,14 @@ export function Type<T>(typeName: string): Type<T> {
return type;
}

export namespace Type {
export const Null: Type<null> = Type("null");
export const Undefined: Type<undefined> = Type("undefined");
export declare namespace Type {
export var Null: Type<null>;
export var Undefined: Type<undefined>;
}

Type.Null = Type("null");
Type.Undefined = Type("undefined");

export interface Constructor<Instance extends object> {
new (...args: []): Instance;
}
Expand Down

0 comments on commit dd98606

Please sign in to comment.