@deepkit/injector
Advanced tools
Comparing version 1.0.1-alpha.30 to 1.0.1-alpha.31
import { ClassSchema, ExtractClassDefinition, PlainSchemaProps, PropertySchema } from '@deepkit/type'; | ||
import { Provider, ProviderWithScope } from './provider'; | ||
import { Provider, ProviderWithScope, TagRegistry } from './provider'; | ||
import { ClassType, CompilerContext, CustomError } from '@deepkit/core'; | ||
@@ -75,6 +75,7 @@ import { InjectorModule } from './module'; | ||
protected configuredProviderRegistry: ConfiguredProviderRegistry | undefined; | ||
protected tagRegistry: TagRegistry; | ||
circularCheck: boolean; | ||
protected resolved: any[]; | ||
protected retriever(injector: Injector, token: any, frontInjector?: Injector): any; | ||
constructor(providers?: Provider[], parents?: (BasicInjector | Injector)[], injectorContext?: InjectorContext, configuredProviderRegistry?: ConfiguredProviderRegistry | undefined); | ||
constructor(providers?: Provider[], parents?: (BasicInjector | Injector)[], injectorContext?: InjectorContext, configuredProviderRegistry?: ConfiguredProviderRegistry | undefined, tagRegistry?: TagRegistry); | ||
/** | ||
@@ -180,2 +181,3 @@ * Creates a clone of this instance, maintains the provider structure, but drops provider instances. | ||
}; | ||
tagRegistry: TagRegistry; | ||
protected injectors: (Injector | undefined)[]; | ||
@@ -187,3 +189,3 @@ readonly scopeCaches: ScopedContextScopeCaches; | ||
[name: string]: InjectorModule; | ||
}, scopeCaches?: ScopedContextScopeCaches); | ||
}, scopeCaches?: ScopedContextScopeCaches, tagRegistry?: TagRegistry); | ||
getModule(name: string): InjectorModule; | ||
@@ -190,0 +192,0 @@ registerModule(module: InjectorModule, config?: ConfigDefinition<any>): void; |
@@ -157,2 +157,4 @@ "use strict"; | ||
return 'undefined'; | ||
if (token instanceof provider_1.TagProvider) | ||
return 'Tag(' + core_1.getClassName(token.provider.provide) + ')'; | ||
if (core_1.isClass(token)) | ||
@@ -168,3 +170,3 @@ return core_1.getClassName(token); | ||
class Injector { | ||
constructor(providers = [], parents = [], injectorContext = new InjectorContext, configuredProviderRegistry = undefined) { | ||
constructor(providers = [], parents = [], injectorContext = new InjectorContext, configuredProviderRegistry = undefined, tagRegistry = new provider_1.TagRegistry()) { | ||
this.providers = providers; | ||
@@ -174,2 +176,3 @@ this.parents = parents; | ||
this.configuredProviderRegistry = configuredProviderRegistry; | ||
this.tagRegistry = tagRegistry; | ||
this.circularCheck = true; | ||
@@ -195,3 +198,3 @@ this.resolved = []; | ||
fork(parents, injectorContext) { | ||
const injector = new Injector(undefined, parents || this.parents, injectorContext, this.configuredProviderRegistry); | ||
const injector = new Injector(undefined, parents || this.parents, injectorContext, this.configuredProviderRegistry, this.tagRegistry); | ||
injector.providers = this.providers; | ||
@@ -255,2 +258,10 @@ injector.retriever = this.retriever; | ||
} | ||
else if (token === provider_1.TagRegistry) { | ||
return compiler.reserveVariable('tagRegistry', this.tagRegistry); | ||
} | ||
else if (core_1.isPrototypeOfBase(token, provider_1.Tag)) { | ||
const tokenVar = compiler.reserveVariable('token', token); | ||
const providers = compiler.reserveVariable('tagRegistry', this.tagRegistry.resolve(token)); | ||
return `new ${tokenVar}(${providers}.map(v => frontInjector.retriever(frontInjector, v, frontInjector)))`; | ||
} | ||
else { | ||
@@ -296,3 +307,6 @@ if (token === undefined) | ||
for (const provider of this.providers) { | ||
if (provider_1.isValueProvider(provider)) { | ||
if (provider instanceof provider_1.TagProvider) { | ||
normalizedProviders.set(provider, provider); | ||
} | ||
else if (provider_1.isValueProvider(provider)) { | ||
normalizedProviders.set(provider.provide, provider); | ||
@@ -313,3 +327,3 @@ } | ||
} | ||
for (const provider of normalizedProviders.values()) { | ||
for (let provider of normalizedProviders.values()) { | ||
const resolvedId = resolvedIds++; | ||
@@ -320,2 +334,6 @@ this.resolved.push(undefined); | ||
let token; | ||
const tagToken = provider instanceof provider_1.TagProvider ? provider : undefined; | ||
if (provider instanceof provider_1.TagProvider) { | ||
provider = provider.provider; | ||
} | ||
if (provider_1.isValueProvider(provider)) { | ||
@@ -348,5 +366,6 @@ transient = provider.transient === true; | ||
else { | ||
console.log('provider', provider); | ||
throw new Error('Invalid provider'); | ||
} | ||
if (tagToken) | ||
token = tagToken; | ||
const tokenVar = compiler.reserveVariable('token', token); | ||
@@ -584,3 +603,3 @@ const creatingVar = compiler.reserveVariable('creating', false); | ||
class InjectorContext { | ||
constructor(contextManager = new ContextRegistry, scope = 'module', configuredProviderRegistry = new ConfiguredProviderRegistry, parent = undefined, additionalInjectorParent = undefined, modules = {}, scopeCaches) { | ||
constructor(contextManager = new ContextRegistry, scope = 'module', configuredProviderRegistry = new ConfiguredProviderRegistry, parent = undefined, additionalInjectorParent = undefined, modules = {}, scopeCaches, tagRegistry = new provider_1.TagRegistry()) { | ||
this.contextManager = contextManager; | ||
@@ -592,2 +611,3 @@ this.scope = scope; | ||
this.modules = modules; | ||
this.tagRegistry = tagRegistry; | ||
this.injectors = new Array(this.contextManager.contexts.length); | ||
@@ -647,3 +667,3 @@ this.scopeCaches = scopeCaches || new ScopedContextScopeCaches(this.contextManager.size); | ||
const providers = provider_1.getProviders(context.providers, this.scope); | ||
injector = new Injector(providers, parents, this, this.configuredProviderRegistry); | ||
injector = new Injector(providers, parents, this, this.configuredProviderRegistry, this.tagRegistry); | ||
this.injectors[contextId] = injector; | ||
@@ -659,3 +679,3 @@ this.cache.set(contextId, injector); | ||
createChildScope(scope, additionalInjectorParent) { | ||
return new InjectorContext(this.contextManager, scope, this.configuredProviderRegistry, this, additionalInjectorParent, this.modules, this.scopeCaches); | ||
return new InjectorContext(this.contextManager, scope, this.configuredProviderRegistry, this, additionalInjectorParent, this.modules, this.scopeCaches, this.tagRegistry); | ||
} | ||
@@ -662,0 +682,0 @@ } |
@@ -55,9 +55,19 @@ import { ClassType } from '@deepkit/core'; | ||
} | ||
export declare type Provider<T = any> = ClassType | ValueProvider<T> | ClassProvider<T> | ExistingProvider<T> | FactoryProvider<T>; | ||
export declare type Provider<T = any> = ClassType | ValueProvider<T> | ClassProvider<T> | ExistingProvider<T> | FactoryProvider<T> | TagProvider<T>; | ||
export declare type ProviderProvide<T = any> = ValueProvider<T> | ClassProvider<T> | ExistingProvider<T> | FactoryProvider<T>; | ||
export declare class TagRegistry { | ||
tags: TagProvider<any>[]; | ||
constructor(tags?: TagProvider<any>[]); | ||
resolve<T extends ClassType<Tag<any>>>(tag: T): TagProvider<InstanceType<T>>[]; | ||
} | ||
export declare class TagProvider<T> { | ||
provider: NormalizedProvider<T>; | ||
tag: Tag<T>; | ||
constructor(provider: NormalizedProvider<T>, tag: Tag<T>); | ||
} | ||
export declare class Tag<T> { | ||
provider: NormalizedProvider<T>; | ||
readonly services: T[]; | ||
_: () => T; | ||
constructor(provider: NormalizedProvider<T>); | ||
static provide<P extends ClassType<T> | ValueProvider<T> | ClassProvider<T> | ExistingProvider<T> | FactoryProvider<T>, T extends ReturnType<InstanceType<B>['_']>, B extends ClassType<Tag<any>>>(this: B, provider: P): InstanceType<B>; | ||
constructor(services?: T[]); | ||
static provide<P extends ClassType<T> | ValueProvider<T> | ClassProvider<T> | ExistingProvider<T> | FactoryProvider<T>, T extends ReturnType<InstanceType<B>['_']>, B extends ClassType<Tag<any>>>(this: B, provider: P): TagProvider<T>; | ||
} | ||
@@ -68,3 +78,3 @@ export interface ProviderScope { | ||
export declare type NormalizedProvider<T = any> = ProviderProvide<T> & ProviderScope; | ||
export declare type ProviderWithScope<T = any> = ClassType | (ProviderProvide<T> & ProviderScope) | Tag<any>; | ||
export declare type ProviderWithScope<T = any> = ClassType | (ProviderProvide<T> & ProviderScope) | TagProvider<any>; | ||
export declare function isScopedProvider(obj: any): obj is ProviderProvide & ProviderScope; | ||
@@ -71,0 +81,0 @@ export declare function isValueProvider(obj: any): obj is ValueProvider<any>; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.getProviders = exports.isInjectionProvider = exports.isFactoryProvider = exports.isExistingProvider = exports.isClassProvider = exports.isValueProvider = exports.isScopedProvider = exports.Tag = void 0; | ||
exports.getProviders = exports.isInjectionProvider = exports.isFactoryProvider = exports.isExistingProvider = exports.isClassProvider = exports.isValueProvider = exports.isScopedProvider = exports.Tag = exports.TagProvider = exports.TagRegistry = void 0; | ||
/* | ||
@@ -14,11 +14,27 @@ * Deepkit Framework | ||
const core_1 = require("@deepkit/core"); | ||
class Tag { | ||
constructor(provider) { | ||
class TagRegistry { | ||
constructor(tags = []) { | ||
this.tags = tags; | ||
} | ||
resolve(tag) { | ||
return this.tags.filter(v => v.tag instanceof tag); | ||
} | ||
} | ||
exports.TagRegistry = TagRegistry; | ||
class TagProvider { | ||
constructor(provider, tag) { | ||
this.provider = provider; | ||
this.tag = tag; | ||
} | ||
} | ||
exports.TagProvider = TagProvider; | ||
class Tag { | ||
constructor(services = []) { | ||
this.services = services; | ||
} | ||
static provide(provider) { | ||
if (core_1.isClass(provider)) { | ||
return new this({ provide: provider }); | ||
return new TagProvider({ provide: provider }, new this); | ||
} | ||
return new this(provider); | ||
return new TagProvider(provider, new this); | ||
} | ||
@@ -57,5 +73,2 @@ } | ||
} | ||
if (provider instanceof Tag) { | ||
return provider.provider; | ||
} | ||
return provider; | ||
@@ -62,0 +75,0 @@ } |
import { ClassSchema, ExtractClassDefinition, PlainSchemaProps, PropertySchema } from '@deepkit/type'; | ||
import { Provider, ProviderWithScope } from './provider'; | ||
import { Provider, ProviderWithScope, TagRegistry } from './provider'; | ||
import { ClassType, CompilerContext, CustomError } from '@deepkit/core'; | ||
@@ -75,6 +75,7 @@ import { InjectorModule } from './module'; | ||
protected configuredProviderRegistry: ConfiguredProviderRegistry | undefined; | ||
protected tagRegistry: TagRegistry; | ||
circularCheck: boolean; | ||
protected resolved: any[]; | ||
protected retriever(injector: Injector, token: any, frontInjector?: Injector): any; | ||
constructor(providers?: Provider[], parents?: (BasicInjector | Injector)[], injectorContext?: InjectorContext, configuredProviderRegistry?: ConfiguredProviderRegistry | undefined); | ||
constructor(providers?: Provider[], parents?: (BasicInjector | Injector)[], injectorContext?: InjectorContext, configuredProviderRegistry?: ConfiguredProviderRegistry | undefined, tagRegistry?: TagRegistry); | ||
/** | ||
@@ -180,2 +181,3 @@ * Creates a clone of this instance, maintains the provider structure, but drops provider instances. | ||
}; | ||
tagRegistry: TagRegistry; | ||
protected injectors: (Injector | undefined)[]; | ||
@@ -187,3 +189,3 @@ readonly scopeCaches: ScopedContextScopeCaches; | ||
[name: string]: InjectorModule; | ||
}, scopeCaches?: ScopedContextScopeCaches); | ||
}, scopeCaches?: ScopedContextScopeCaches, tagRegistry?: TagRegistry); | ||
getModule(name: string): InjectorModule; | ||
@@ -190,0 +192,0 @@ registerModule(module: InjectorModule, config?: ConfigDefinition<any>): void; |
@@ -11,4 +11,4 @@ /* | ||
import { FieldDecoratorWrapper, getClassSchema, jsonSerializer, t } from '@deepkit/type'; | ||
import { getProviders, isClassProvider, isExistingProvider, isFactoryProvider, isValueProvider } from './provider'; | ||
import { CompilerContext, CustomError, getClassName, isClass, isFunction } from '@deepkit/core'; | ||
import { getProviders, isClassProvider, isExistingProvider, isFactoryProvider, isValueProvider, Tag, TagProvider, TagRegistry } from './provider'; | ||
import { CompilerContext, CustomError, getClassName, isClass, isFunction, isPrototypeOfBase } from '@deepkit/core'; | ||
import { InjectorModule } from './module'; | ||
@@ -143,2 +143,4 @@ export class ConfigToken { | ||
return 'undefined'; | ||
if (token instanceof TagProvider) | ||
return 'Tag(' + getClassName(token.provider.provide) + ')'; | ||
if (isClass(token)) | ||
@@ -153,3 +155,3 @@ return getClassName(token); | ||
export class Injector { | ||
constructor(providers = [], parents = [], injectorContext = new InjectorContext, configuredProviderRegistry = undefined) { | ||
constructor(providers = [], parents = [], injectorContext = new InjectorContext, configuredProviderRegistry = undefined, tagRegistry = new TagRegistry()) { | ||
this.providers = providers; | ||
@@ -159,2 +161,3 @@ this.parents = parents; | ||
this.configuredProviderRegistry = configuredProviderRegistry; | ||
this.tagRegistry = tagRegistry; | ||
this.circularCheck = true; | ||
@@ -180,3 +183,3 @@ this.resolved = []; | ||
fork(parents, injectorContext) { | ||
const injector = new Injector(undefined, parents || this.parents, injectorContext, this.configuredProviderRegistry); | ||
const injector = new Injector(undefined, parents || this.parents, injectorContext, this.configuredProviderRegistry, this.tagRegistry); | ||
injector.providers = this.providers; | ||
@@ -240,2 +243,10 @@ injector.retriever = this.retriever; | ||
} | ||
else if (token === TagRegistry) { | ||
return compiler.reserveVariable('tagRegistry', this.tagRegistry); | ||
} | ||
else if (isPrototypeOfBase(token, Tag)) { | ||
const tokenVar = compiler.reserveVariable('token', token); | ||
const providers = compiler.reserveVariable('tagRegistry', this.tagRegistry.resolve(token)); | ||
return `new ${tokenVar}(${providers}.map(v => frontInjector.retriever(frontInjector, v, frontInjector)))`; | ||
} | ||
else { | ||
@@ -281,3 +292,6 @@ if (token === undefined) | ||
for (const provider of this.providers) { | ||
if (isValueProvider(provider)) { | ||
if (provider instanceof TagProvider) { | ||
normalizedProviders.set(provider, provider); | ||
} | ||
else if (isValueProvider(provider)) { | ||
normalizedProviders.set(provider.provide, provider); | ||
@@ -298,3 +312,3 @@ } | ||
} | ||
for (const provider of normalizedProviders.values()) { | ||
for (let provider of normalizedProviders.values()) { | ||
const resolvedId = resolvedIds++; | ||
@@ -305,2 +319,6 @@ this.resolved.push(undefined); | ||
let token; | ||
const tagToken = provider instanceof TagProvider ? provider : undefined; | ||
if (provider instanceof TagProvider) { | ||
provider = provider.provider; | ||
} | ||
if (isValueProvider(provider)) { | ||
@@ -333,5 +351,6 @@ transient = provider.transient === true; | ||
else { | ||
console.log('provider', provider); | ||
throw new Error('Invalid provider'); | ||
} | ||
if (tagToken) | ||
token = tagToken; | ||
const tokenVar = compiler.reserveVariable('token', token); | ||
@@ -561,3 +580,3 @@ const creatingVar = compiler.reserveVariable('creating', false); | ||
export class InjectorContext { | ||
constructor(contextManager = new ContextRegistry, scope = 'module', configuredProviderRegistry = new ConfiguredProviderRegistry, parent = undefined, additionalInjectorParent = undefined, modules = {}, scopeCaches) { | ||
constructor(contextManager = new ContextRegistry, scope = 'module', configuredProviderRegistry = new ConfiguredProviderRegistry, parent = undefined, additionalInjectorParent = undefined, modules = {}, scopeCaches, tagRegistry = new TagRegistry()) { | ||
this.contextManager = contextManager; | ||
@@ -569,2 +588,3 @@ this.scope = scope; | ||
this.modules = modules; | ||
this.tagRegistry = tagRegistry; | ||
this.injectors = new Array(this.contextManager.contexts.length); | ||
@@ -624,3 +644,3 @@ this.scopeCaches = scopeCaches || new ScopedContextScopeCaches(this.contextManager.size); | ||
const providers = getProviders(context.providers, this.scope); | ||
injector = new Injector(providers, parents, this, this.configuredProviderRegistry); | ||
injector = new Injector(providers, parents, this, this.configuredProviderRegistry, this.tagRegistry); | ||
this.injectors[contextId] = injector; | ||
@@ -636,3 +656,3 @@ this.cache.set(contextId, injector); | ||
createChildScope(scope, additionalInjectorParent) { | ||
return new InjectorContext(this.contextManager, scope, this.configuredProviderRegistry, this, additionalInjectorParent, this.modules, this.scopeCaches); | ||
return new InjectorContext(this.contextManager, scope, this.configuredProviderRegistry, this, additionalInjectorParent, this.modules, this.scopeCaches, this.tagRegistry); | ||
} | ||
@@ -639,0 +659,0 @@ } |
@@ -55,9 +55,19 @@ import { ClassType } from '@deepkit/core'; | ||
} | ||
export declare type Provider<T = any> = ClassType | ValueProvider<T> | ClassProvider<T> | ExistingProvider<T> | FactoryProvider<T>; | ||
export declare type Provider<T = any> = ClassType | ValueProvider<T> | ClassProvider<T> | ExistingProvider<T> | FactoryProvider<T> | TagProvider<T>; | ||
export declare type ProviderProvide<T = any> = ValueProvider<T> | ClassProvider<T> | ExistingProvider<T> | FactoryProvider<T>; | ||
export declare class TagRegistry { | ||
tags: TagProvider<any>[]; | ||
constructor(tags?: TagProvider<any>[]); | ||
resolve<T extends ClassType<Tag<any>>>(tag: T): TagProvider<InstanceType<T>>[]; | ||
} | ||
export declare class TagProvider<T> { | ||
provider: NormalizedProvider<T>; | ||
tag: Tag<T>; | ||
constructor(provider: NormalizedProvider<T>, tag: Tag<T>); | ||
} | ||
export declare class Tag<T> { | ||
provider: NormalizedProvider<T>; | ||
readonly services: T[]; | ||
_: () => T; | ||
constructor(provider: NormalizedProvider<T>); | ||
static provide<P extends ClassType<T> | ValueProvider<T> | ClassProvider<T> | ExistingProvider<T> | FactoryProvider<T>, T extends ReturnType<InstanceType<B>['_']>, B extends ClassType<Tag<any>>>(this: B, provider: P): InstanceType<B>; | ||
constructor(services?: T[]); | ||
static provide<P extends ClassType<T> | ValueProvider<T> | ClassProvider<T> | ExistingProvider<T> | FactoryProvider<T>, T extends ReturnType<InstanceType<B>['_']>, B extends ClassType<Tag<any>>>(this: B, provider: P): TagProvider<T>; | ||
} | ||
@@ -68,3 +78,3 @@ export interface ProviderScope { | ||
export declare type NormalizedProvider<T = any> = ProviderProvide<T> & ProviderScope; | ||
export declare type ProviderWithScope<T = any> = ClassType | (ProviderProvide<T> & ProviderScope) | Tag<any>; | ||
export declare type ProviderWithScope<T = any> = ClassType | (ProviderProvide<T> & ProviderScope) | TagProvider<any>; | ||
export declare function isScopedProvider(obj: any): obj is ProviderProvide & ProviderScope; | ||
@@ -71,0 +81,0 @@ export declare function isValueProvider(obj: any): obj is ValueProvider<any>; |
@@ -11,11 +11,25 @@ /* | ||
import { isClass } from '@deepkit/core'; | ||
export class Tag { | ||
constructor(provider) { | ||
export class TagRegistry { | ||
constructor(tags = []) { | ||
this.tags = tags; | ||
} | ||
resolve(tag) { | ||
return this.tags.filter(v => v.tag instanceof tag); | ||
} | ||
} | ||
export class TagProvider { | ||
constructor(provider, tag) { | ||
this.provider = provider; | ||
this.tag = tag; | ||
} | ||
} | ||
export class Tag { | ||
constructor(services = []) { | ||
this.services = services; | ||
} | ||
static provide(provider) { | ||
if (isClass(provider)) { | ||
return new this({ provide: provider }); | ||
return new TagProvider({ provide: provider }, new this); | ||
} | ||
return new this(provider); | ||
return new TagProvider(provider, new this); | ||
} | ||
@@ -47,5 +61,2 @@ } | ||
} | ||
if (provider instanceof Tag) { | ||
return provider.provider; | ||
} | ||
return provider; | ||
@@ -52,0 +63,0 @@ } |
{ | ||
"name": "@deepkit/injector", | ||
"version": "1.0.1-alpha.30", | ||
"version": "1.0.1-alpha.31", | ||
"description": "Deepkit Dependency Injection", | ||
@@ -30,4 +30,4 @@ "type": "commonjs", | ||
"devDependencies": { | ||
"@deepkit/core": "^1.0.1-alpha.30", | ||
"@deepkit/type": "^1.0.1-alpha.30", | ||
"@deepkit/core": "^1.0.1-alpha.31", | ||
"@deepkit/type": "^1.0.1-alpha.31", | ||
"reflect-metadata": "^0.1.13" | ||
@@ -43,3 +43,3 @@ }, | ||
}, | ||
"gitHead": "a938a59dac95a970f8e2935ef82ff1028f0b4843" | ||
"gitHead": "2433e2b7567e80f5b335b685eb518e3e7cccb92a" | ||
} |
@@ -12,4 +12,4 @@ /* | ||
import { ClassSchema, ExtractClassDefinition, FieldDecoratorWrapper, getClassSchema, jsonSerializer, PlainSchemaProps, PropertySchema, t } from '@deepkit/type'; | ||
import { getProviders, isClassProvider, isExistingProvider, isFactoryProvider, isValueProvider, Provider, ProviderWithScope } from './provider'; | ||
import { ClassType, CompilerContext, CustomError, getClassName, isClass, isFunction } from '@deepkit/core'; | ||
import { getProviders, isClassProvider, isExistingProvider, isFactoryProvider, isValueProvider, Provider, ProviderWithScope, Tag, TagProvider, TagRegistry } from './provider'; | ||
import { ClassType, CompilerContext, CustomError, getClassName, isClass, isFunction, isPrototypeOfBase } from '@deepkit/core'; | ||
import { InjectorModule } from './module'; | ||
@@ -196,2 +196,3 @@ | ||
if (token === undefined) return 'undefined'; | ||
if (token instanceof TagProvider) return 'Tag(' + getClassName(token.provider.provide) + ')'; | ||
if (isClass(token)) return getClassName(token); | ||
@@ -231,3 +232,4 @@ if (isFunction(token.toString)) return token.toString(); | ||
protected injectorContext: InjectorContext = new InjectorContext, | ||
protected configuredProviderRegistry: ConfiguredProviderRegistry | undefined = undefined | ||
protected configuredProviderRegistry: ConfiguredProviderRegistry | undefined = undefined, | ||
protected tagRegistry: TagRegistry = new TagRegistry() | ||
) { | ||
@@ -243,3 +245,3 @@ if (!this.configuredProviderRegistry) this.configuredProviderRegistry = injectorContext.configuredProviderRegistry; | ||
public fork(parents?: Injector[], injectorContext?: InjectorContext) { | ||
const injector = new Injector(undefined, parents || this.parents, injectorContext, this.configuredProviderRegistry); | ||
const injector = new Injector(undefined, parents || this.parents, injectorContext, this.configuredProviderRegistry, this.tagRegistry); | ||
injector.providers = this.providers; | ||
@@ -302,2 +304,8 @@ injector.retriever = this.retriever; | ||
} | ||
} else if (token === TagRegistry) { | ||
return compiler.reserveVariable('tagRegistry', this.tagRegistry); | ||
} else if (isPrototypeOfBase(token, Tag)) { | ||
const tokenVar = compiler.reserveVariable('token', token); | ||
const providers = compiler.reserveVariable('tagRegistry', this.tagRegistry.resolve(token)); | ||
return `new ${tokenVar}(${providers}.map(v => frontInjector.retriever(frontInjector, v, frontInjector)))`; | ||
} else { | ||
@@ -349,3 +357,5 @@ if (token === undefined) throw new Error(`Argument type ${property.name} at position ${argPosition} is undefined. Imported reflect-metadata correctly?`); | ||
for (const provider of this.providers) { | ||
if (isValueProvider(provider)) { | ||
if (provider instanceof TagProvider) { | ||
normalizedProviders.set(provider, provider); | ||
} else if (isValueProvider(provider)) { | ||
normalizedProviders.set(provider.provide, provider); | ||
@@ -363,3 +373,3 @@ } else if (isClassProvider(provider)) { | ||
for (const provider of normalizedProviders.values()) { | ||
for (let provider of normalizedProviders.values()) { | ||
const resolvedId = resolvedIds++; | ||
@@ -370,2 +380,6 @@ this.resolved.push(undefined); | ||
let token: any; | ||
const tagToken = provider instanceof TagProvider ? provider : undefined; | ||
if (provider instanceof TagProvider) { | ||
provider = provider.provider; | ||
} | ||
@@ -395,6 +409,7 @@ if (isValueProvider(provider)) { | ||
} else { | ||
console.log('provider', provider); | ||
throw new Error('Invalid provider'); | ||
} | ||
if (tagToken) token = tagToken; | ||
const tokenVar = compiler.reserveVariable('token', token); | ||
@@ -675,2 +690,3 @@ const creatingVar = compiler.reserveVariable('creating', false); | ||
scopeCaches?: ScopedContextScopeCaches, | ||
public tagRegistry: TagRegistry = new TagRegistry(), | ||
) { | ||
@@ -737,3 +753,3 @@ this.scopeCaches = scopeCaches || new ScopedContextScopeCaches(this.contextManager.size); | ||
injector = new Injector(providers, parents, this, this.configuredProviderRegistry); | ||
injector = new Injector(providers, parents, this, this.configuredProviderRegistry, this.tagRegistry); | ||
this.injectors[contextId] = injector; | ||
@@ -752,4 +768,4 @@ this.cache.set(contextId, injector); | ||
public createChildScope(scope: string, additionalInjectorParent?: Injector): InjectorContext { | ||
return new InjectorContext(this.contextManager, scope, this.configuredProviderRegistry, this, additionalInjectorParent, this.modules, this.scopeCaches); | ||
return new InjectorContext(this.contextManager, scope, this.configuredProviderRegistry, this, additionalInjectorParent, this.modules, this.scopeCaches, this.tagRegistry); | ||
} | ||
} |
@@ -75,6 +75,25 @@ /* | ||
export type Provider<T = any> = ClassType | ValueProvider<T> | ClassProvider<T> | ExistingProvider<T> | FactoryProvider<T>; | ||
export type Provider<T = any> = ClassType | ValueProvider<T> | ClassProvider<T> | ExistingProvider<T> | FactoryProvider<T> | TagProvider<T>; | ||
export type ProviderProvide<T = any> = ValueProvider<T> | ClassProvider<T> | ExistingProvider<T> | FactoryProvider<T>; | ||
export class TagRegistry { | ||
constructor( | ||
public tags: TagProvider<any>[] = [] | ||
) { | ||
} | ||
resolve<T extends ClassType<Tag<any>>>(tag: T): TagProvider<InstanceType<T>>[] { | ||
return this.tags.filter(v => v.tag instanceof tag); | ||
} | ||
} | ||
export class TagProvider<T> { | ||
constructor( | ||
public provider: NormalizedProvider<T>, | ||
public tag: Tag<T>, | ||
) { | ||
} | ||
} | ||
export class Tag<T> { | ||
@@ -84,12 +103,12 @@ _!: () => T; | ||
constructor( | ||
public provider: NormalizedProvider<T>, | ||
public readonly services: T[] = [] | ||
) { | ||
} | ||
static provide<P extends ClassType<T> | ValueProvider<T> | ClassProvider<T> | ExistingProvider<T> | FactoryProvider<T>, T extends ReturnType<InstanceType<B>['_']>, B extends ClassType<Tag<any>>>(this: B, provider: P): InstanceType<B> { | ||
static provide<P extends ClassType<T> | ValueProvider<T> | ClassProvider<T> | ExistingProvider<T> | FactoryProvider<T>, T extends ReturnType<InstanceType<B>['_']>, B extends ClassType<Tag<any>>>(this: B, provider: P): TagProvider<T> { | ||
if (isClass(provider)) { | ||
return new (this as ClassType<any>)({ provide: provider }); | ||
return new TagProvider({ provide: provider }, new this); | ||
} | ||
return new (this as ClassType<any>)(provider); | ||
return new TagProvider(provider as NormalizedProvider<T>, new this); | ||
} | ||
@@ -104,3 +123,3 @@ } | ||
export type ProviderWithScope<T = any> = ClassType | (ProviderProvide<T> & ProviderScope) | Tag<any>; | ||
export type ProviderWithScope<T = any> = ClassType | (ProviderProvide<T> & ProviderScope) | TagProvider<any>; | ||
@@ -142,6 +161,2 @@ export function isScopedProvider(obj: any): obj is ProviderProvide & ProviderScope { | ||
if (provider instanceof Tag) { | ||
return provider.provider; | ||
} | ||
return provider; | ||
@@ -148,0 +163,0 @@ } |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Unidentified License
License(Experimental) Something that seems like a license was found, but its contents could not be matched with a known license.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Unidentified License
License(Experimental) Something that seems like a license was found, but its contents could not be matched with a known license.
Found 1 instance in 1 package
4290
312548