Skip to content

Commit

Permalink
Merge d2da885 into 3697616
Browse files Browse the repository at this point in the history
  • Loading branch information
Romakita committed Jul 18, 2022
2 parents 3697616 + d2da885 commit 3b4c2ea
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 36 deletions.
10 changes: 5 additions & 5 deletions packages/di/src/domain/DIContext.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import type {Env} from "@tsed/core";
import {InjectorService} from "../services/InjectorService";
import {runInContext} from "../utils/runInContext";
import {ContextLogger, ContextLoggerOptions} from "./ContextLogger";
import {LocalsContainer} from "./LocalsContainer";
import {runInContext} from "../utils/runInContext";

export interface ContextMethods extends Map<any, any> {
readonly id: string;
Expand All @@ -22,6 +22,7 @@ export interface DIContextOptions extends Omit<ContextLoggerOptions, "dateStart"

export class DIContext extends Map<any, any> implements ContextMethods {
[x: string]: any;

/**
* Request id generated by @@contextMiddleware@@.
*
Expand Down Expand Up @@ -95,10 +96,9 @@ export class DIContext extends Map<any, any> implements ContextMethods {
}

async runInContext(next: Function) {
return runInContext(this, async () => {
next = (await this.injector?.alterAsync("$alterRunInContext", next, this)) || next;
return next();
});
next = (await this.injector?.alterAsync("$alterRunInContext", next, this)) || next;

return runInContext(this, next);
}
}

Expand Down
2 changes: 2 additions & 0 deletions packages/di/src/domain/Provider.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ describe("Provider", () => {
expect(!!provider.store).toEqual(true);
expect(cloned.useClass).toEqual(T2);
expect(cloned.scope).toEqual(ProviderScope.REQUEST);
expect(provider.hasChildren()).toEqual(false);
expect(provider.hasParent()).toEqual(false);
});
});

Expand Down
2 changes: 1 addition & 1 deletion packages/di/src/interfaces/TokenProvider.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
import type {Type} from "@tsed/core";

export type TokenProvider = string | symbol | Type | Function | any;
export type TokenProvider<T = any> = string | symbol | Type<T> | Function | any;
6 changes: 1 addition & 5 deletions packages/di/src/services/InjectorService.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,6 @@ describe("InjectorService", () => {

jest.spyOn(injector as any, "resolve");
jest.spyOn(injector as any, "invoke");
jest.spyOn(injector, "get");
jest.spyOn(injector, "getProvider");

const locals = new Map();
Expand All @@ -141,7 +140,7 @@ describe("InjectorService", () => {
// THEN
expect(result1 !== result2).toEqual(true);
expect(injector.getProvider).toBeCalledWith(token);
expect(injector.get).toBeCalledWith(token);

expect((injector as any).resolve).toBeCalledWith(token, locals, {rebuild: true});
expect((injector as any).invoke).toBeCalledWith(InjectorService, locals, {
parent: token
Expand All @@ -163,7 +162,6 @@ describe("InjectorService", () => {
await injector.load(container);

jest.spyOn(injector as any, "resolve");
jest.spyOn(injector, "get");
jest.spyOn(injector, "getProvider");

const locals = new Map();
Expand All @@ -175,8 +173,6 @@ describe("InjectorService", () => {

// THEN
expect(result1).toEqual(result2);
expect(injector.getProvider).toBeCalledWith(token);
expect(injector.get).toBeCalledWith(token);

return expect((injector as any).resolve).not.toBeCalled();
});
Expand Down
40 changes: 23 additions & 17 deletions packages/di/src/services/InjectorService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,9 @@ import {DIConfiguration} from "./DIConfiguration";
export class InjectorService extends Container {
public settings: DIConfiguration = new DIConfiguration();
public logger: DILogger = console;
/**
* @deprecated
*/
public runInContext = runInContext;
private resolvedConfiguration: boolean = false;
#cache = new LocalsContainer();
Expand Down Expand Up @@ -136,7 +139,7 @@ export class InjectorService extends Container {
* @param options
* @returns {boolean}
*/
get<T = any>(token: TokenProvider, options: any = {}): T | undefined {
get<T = any>(token: TokenProvider<T>, options: any = {}): T | undefined {
const instance = this.getInstance(token);

if (instance !== undefined) {
Expand Down Expand Up @@ -194,24 +197,35 @@ export class InjectorService extends Container {
* @param options
* @returns {T} The class constructed.
*/
public invoke<T>(
public invoke<T = any>(
token: TokenProvider,
locals: Map<TokenProvider, any> = new LocalsContainer(),
options: Partial<InvokeOptions<T>> = {}
): T {
const provider = this.ensureProvider(token);
let instance: any = undefined;
let instance: any;

if (token === Configuration) {
return this.settings as unknown as T;
}

instance = locals.get(token);

if (instance !== undefined) {
return instance;
}

!locals.has(Configuration) && locals.set(Configuration, this.settings);
instance = !options.rebuild ? this.getInstance(token) : undefined;

if (locals.has(token)) {
return locals.get(token);
if (instance != undefined) {
return instance;
}

if (token === DI_PARAM_OPTIONS) {
return {} as T;
}

const provider = this.ensureProvider(token);

if (!provider || options.rebuild) {
instance = this.resolve(token, locals, options);

Expand All @@ -222,14 +236,10 @@ export class InjectorService extends Container {
return instance;
}

instance = this.resolve(token, locals, options);

switch (this.scopeOf(provider)) {
case ProviderScope.SINGLETON:
if (this.has(token)) {
return this.get<T>(token)!;
}

instance = this.resolve(token, locals, options);

if (!provider.isAsync()) {
this.#cache.set(token, instance);
return instance;
Expand All @@ -247,12 +257,8 @@ export class InjectorService extends Container {
return instance;

case ProviderScope.REQUEST:
instance = this.resolve(token, locals, options);
locals.set(token, instance);
return instance;

case ProviderScope.INSTANCE:
return this.resolve(provider.provide, locals, options) as any;
}

return instance;
Expand Down
12 changes: 5 additions & 7 deletions packages/di/src/utils/runInContext.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,16 @@
import {AsyncLocalStorage} from "async_hooks";
import {DIContext} from "../domain/DIContext";

let store: AsyncLocalStorage<DIContext>;
const storage: AsyncLocalStorage<DIContext> = new AsyncLocalStorage();

export function getAsyncStore() {
store = store || new AsyncLocalStorage();
return store;
return storage;
}

export function getContext() {
return store?.getStore();
export function getContext<Context = DIContext>(): Context | undefined {
return storage.getStore() as any;
}

export function runInContext(ctx: DIContext, cb: any) {
// istanbul ignore else
return AsyncLocalStorage ? getAsyncStore().run(ctx, cb) : cb();
return storage.run(ctx, cb);
}
1 change: 0 additions & 1 deletion packages/platform/common/src/builder/PlatformBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,6 @@ export class PlatformBuilder<App = TsED.Application, Router = TsED.Router> {
const {startedAt} = this;

await this.callHook("$onReady");
await this.injector.emit("$onReady");
await this.injector.emit("$onServerReady");

this.log(`Started in ${new Date().getTime() - startedAt.getTime()} ms`);
Expand Down

0 comments on commit 3b4c2ea

Please sign in to comment.