Skip to content

Commit

Permalink
chore(di): Add TokenProvider
Browse files Browse the repository at this point in the history
  • Loading branch information
Romakita committed May 1, 2019
1 parent d2569bd commit ccc6b4a
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 22 deletions.
3 changes: 2 additions & 1 deletion packages/di/src/interfaces/IInjectableProperties.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {InjectablePropertyType} from "./InjectablePropertyType";
import {TokenProvider} from "./TokenProvider";

export interface IInjectableProperty {
propertyKey: string;
Expand All @@ -7,7 +8,7 @@ export interface IInjectableProperty {
export interface IInjectablePropertyService extends IInjectableProperty {
bindingType: InjectablePropertyType.METHOD | InjectablePropertyType.PROPERTY | InjectablePropertyType.INTERCEPTOR;
propertyType: string;
useType: any;
useType: TokenProvider;
options?: any;
}

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

/**
*
Expand All @@ -8,7 +9,7 @@ export interface IProvider<T> {
/**
* An injection token. (Typically an instance of `Type` or `InjectionToken`, but can be `any`).
*/
provide: any;
provide: TokenProvider;

/**
* Class to instantiate for the `token`.
Expand Down
5 changes: 5 additions & 0 deletions packages/di/src/interfaces/TokenProvider.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import {RegistryKey} from "@tsed/core";
/**
* @alias of RegistryKey
*/
export type TokenProvider = RegistryKey;
3 changes: 2 additions & 1 deletion packages/di/src/interfaces/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,6 @@ export * from "./OnInit";
export * from "./OnDestroy";
export * from "./ProviderScope";
export * from "./ProviderType";
export * from "./TypedProvidersRegistry";
export * from "./RegistrySettings";
export * from "./TokenProvider";
export * from "./TypedProvidersRegistry";
13 changes: 7 additions & 6 deletions packages/di/src/registries/GlobalProviders.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {Registry, RegistryKey, Type} from "@tsed/core";
import {Registry, Type} from "@tsed/core";
import {Provider} from "../class/Provider";
import {IProvider, RegistrySettings, TypedProvidersRegistry} from "../interfaces";
import {IProvider, RegistrySettings, TokenProvider, TypedProvidersRegistry} from "../interfaces";

export class GlobalProviderRegistry extends Registry<Provider<any>, IProvider<any>> {
/**
Expand Down Expand Up @@ -42,10 +42,10 @@ export class GlobalProviderRegistry extends Registry<Provider<any>, IProvider<an

/**
*
* @param {string | RegistryKey} target
* @param {string | TokenProvider} target
* @returns {RegistrySettings | undefined}
*/
getRegistrySettings(target: string | RegistryKey): RegistrySettings {
getRegistrySettings(target: string | TokenProvider): RegistrySettings {
let type: string = "provider";

if (typeof target === "string") {
Expand Down Expand Up @@ -87,13 +87,14 @@ export class GlobalProviderRegistry extends Registry<Provider<any>, IProvider<an

/**
*
* @param {string | RegistryKey} target
* @param {string | TokenProvider} target
* @returns {Registry<Provider<any>, IProvider<any>>}
*/
getRegistry(target: string | RegistryKey): TypedProvidersRegistry {
getRegistry(target: string | TokenProvider): TypedProvidersRegistry {
return this.getRegistrySettings(target).registry;
}
}

/**
*
* @type {GlobalProviders}
Expand Down
33 changes: 20 additions & 13 deletions packages/di/src/services/InjectorService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ import {
IInterceptorContext,
InjectablePropertyType,
ProviderScope,
ProviderType
ProviderType,
TokenProvider
} from "../interfaces";
import {GlobalProviders} from "../registries/GlobalProviders";
import {registerFactory} from "../registries/ProviderRegistry";
Expand Down Expand Up @@ -41,7 +42,7 @@ import {registerFactory} from "../registries/ProviderRegistry";
* > Note: `ServerLoader` make this automatically when you use `ServerLoader.mount()` method (or settings attributes) and load services and controllers during the starting server.
*
*/
export class InjectorService extends Map<RegistryKey, Provider<any>> {
export class InjectorService extends Map<TokenProvider, Provider<any>> {
public settings: IDISettings = new Map();
public logger: IDILogger = console;
public scopes: {[key: string]: ProviderScope} = {};
Expand Down Expand Up @@ -74,36 +75,36 @@ export class InjectorService extends Map<RegistryKey, Provider<any>> {
* @param target The class or symbol registered in InjectorService.
* @returns {boolean}
*/
get<T>(target: Type<T> | symbol | any): T | undefined {
return (super.has(target) && super.get(getClassOrSymbol(target))!.instance) || undefined;
get<T>(token: TokenProvider): T | undefined {
return (super.has(token) && super.get(getClassOrSymbol(token))!.instance) || undefined;
}

/**
* The has() method returns a boolean indicating whether an element with the specified key exists or not.
* @param key
* @returns {boolean}
*/
has(key: RegistryKey): boolean {
return super.has(getClassOrSymbol(key)) && !!this.get(key);
has(token: TokenProvider): boolean {
return super.has(getClassOrSymbol(token)) && !!this.get(token);
}

/**
* The getProvider() method returns a specified element from a Map object.
* @param key Required. The key of the element to return from the Map object.
* @returns {T} Returns the element associated with the specified key or undefined if the key can't be found in the Map object.
*/
getProvider(key: RegistryKey): Provider<any> | undefined {
return super.get(getClassOrSymbol(key));
getProvider(token: TokenProvider): Provider<any> | undefined {
return super.get(getClassOrSymbol(token));
}

/**
*
* @param {RegistryKey} key
* @param instance
*/
forkProvider(key: RegistryKey, instance?: any): Provider<any> {
const provider = GlobalProviders.get(key)!.clone();
this.set(key, provider);
forkProvider(token: TokenProvider, instance?: any): Provider<any> {
const provider = GlobalProviders.get(token)!.clone();
this.set(token, provider);

provider.instance = instance;

Expand Down Expand Up @@ -142,13 +143,18 @@ export class InjectorService extends Map<RegistryKey, Provider<any>> {
* }
* ```
*
* @param target The injectable class to invoke. Class parameters are injected according constructor signature.
* @param token The injectable class to invoke. Class parameters are injected according constructor signature.
* @param locals Optional object. If preset then any argument Class are read from this object first, before the `InjectorService` is consulted.
* @param designParamTypes Optional object. List of injectable types.
* @param requiredScope
* @returns {T} The class constructed.
*/
invoke<T>(target: any, locals: Map<string | Function, any> = new Map(), designParamTypes?: any[], requiredScope: boolean = false): T {
invoke<T>(
target: TokenProvider,
locals: Map<string | Function, any> = new Map(),
designParamTypes?: any[],
requiredScope: boolean = false
): T {
const {onInvoke} = GlobalProviders.getRegistrySettings(target);
const provider = this.getProvider(target);
const parentScope = Store.from(target).get("scope");
Expand Down Expand Up @@ -208,6 +214,7 @@ export class InjectorService extends Map<RegistryKey, Provider<any>> {
* @returns {any}
* @param handler The injectable method to invoke. Method parameters are injected according method signature.
* @param options Object to configure the invocation.
* @deprecated
*/
public invokeMethod(handler: any, options: IInjectableMethod<any>): any {
let {designParamTypes} = options;
Expand Down

0 comments on commit ccc6b4a

Please sign in to comment.