Comparing version 2.0.0 to 2.0.1
@@ -9,13 +9,43 @@ "use strict"; | ||
} | ||
register(provider) { | ||
if (!providers_1.isClassProvider(provider) && !providers_1.isTokenProvider(provider) && !providers_1.isValueProvider(provider) && !providers_1.isFactoryProvider(provider)) { | ||
if (!this.isRegistered(provider)) { | ||
this._registry.set(provider, [provider, undefined]); | ||
register(token, provider, options = { singleton: false }) { | ||
if (options.singleton) { | ||
if (providers_1.isValueProvider(provider) || providers_1.isFactoryProvider(provider)) { | ||
throw "Cannot use {singleton: true} with ValueProviders or FactoryProviders"; | ||
} | ||
} | ||
else { | ||
if (!this.isRegistered(provider.token)) { | ||
this._registry.set(provider.token, [provider, undefined]); | ||
this._registry.set(token, { provider, options }); | ||
return this; | ||
} | ||
registerType(from, to) { | ||
if (typeof (to) === "string") { | ||
return this.register(from, { | ||
useToken: to | ||
}); | ||
} | ||
return this.register(from, { | ||
useClass: to | ||
}); | ||
} | ||
registerInstance(token, instance) { | ||
return this.register(token, { | ||
useValue: instance | ||
}); | ||
} | ||
registerSingleton(from, to) { | ||
if (typeof (from) === "string") { | ||
if (typeof (to) === "string") { | ||
return this.register(from, { | ||
useToken: to | ||
}, { singleton: true }); | ||
} | ||
else if (to) { | ||
return this.register(from, { | ||
useClass: to | ||
}, { singleton: true }); | ||
} | ||
throw "Cannot register a type name as a singleton without a \"to\" token"; | ||
} | ||
return this.register(from, { | ||
useClass: from | ||
}, { singleton: true }); | ||
} | ||
@@ -30,21 +60,20 @@ resolve(token) { | ||
if (registration) { | ||
const provider = registration[0]; | ||
const cachedInstance = registration[1]; | ||
if (cachedInstance != undefined) { | ||
return cachedInstance; | ||
if (providers_1.isValueProvider(registration.provider)) { | ||
return registration.provider.useValue; | ||
} | ||
if (providers_1.isValueProvider(provider)) { | ||
return registration[1] = provider.useValue; | ||
else if (providers_1.isTokenProvider(registration.provider)) { | ||
return registration.options.singleton ? | ||
(registration.instance || (registration.instance = this.resolve(registration.provider.useToken))) : | ||
this.resolve(registration.provider.useToken); | ||
} | ||
else if (providers_1.isTokenProvider(provider)) { | ||
return registration[1] = this.resolve(provider.useToken); | ||
else if (providers_1.isClassProvider(registration.provider)) { | ||
return registration.options.singleton ? | ||
(registration.instance || (registration.instance = this.construct(registration.provider.useClass))) : | ||
this.construct(registration.provider.useClass); | ||
} | ||
else if (providers_1.isClassProvider(provider)) { | ||
return registration[1] = this.construct(provider.useClass); | ||
else if (providers_1.isFactoryProvider(registration.provider)) { | ||
return registration.provider.useFactory(this); | ||
} | ||
else if (providers_1.isFactoryProvider(provider)) { | ||
return provider.useFactory(this); | ||
} | ||
else { | ||
return registration[1] = this.construct(provider); | ||
return this.construct(registration.provider); | ||
} | ||
@@ -77,3 +106,3 @@ } | ||
const paramInfo = exports.typeInfo.get(ctor); | ||
if (!paramInfo) { | ||
if (!paramInfo || paramInfo.length === 0) { | ||
throw `TypeInfo not known for ${ctor}`; | ||
@@ -80,0 +109,0 @@ } |
{ | ||
"name": "tsyringe", | ||
"version": "2.0.0", | ||
"version": "2.0.1", | ||
"description": "Lightweight dependency injection container for JavaScript/TypeScript", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.js", |
@@ -11,3 +11,3 @@ [![Travis](https://img.shields.io/travis/Microsoft/tsyringe.svg)](https://travis-ci.org/Microsoft/tsyringe/) | ||
* [injectable()](#injectable) | ||
* [autoInjectable()](#autoInjectable) | ||
* [autoInjectable()](#autoinjectable) | ||
* [inject()](#inject) | ||
@@ -14,0 +14,0 @@ * [Full Examples](#full-examples) |
import * as Types from "./types"; | ||
import { InjectionToken, Provider } from "./providers"; | ||
import { ClassProvider, FactoryProvider, InjectionToken, TokenProvider, ValueProvider } from "./providers"; | ||
import { RegistrationOptions, constructor } from "./types"; | ||
export declare class DependencyContainer implements Types.DependencyContainer { | ||
@@ -7,3 +8,10 @@ private parent; | ||
constructor(parent?: DependencyContainer | undefined); | ||
register<T>(provider: Provider<T>): void; | ||
register<T>(token: InjectionToken<T>, provider: ValueProvider<T>): DependencyContainer; | ||
register<T>(token: InjectionToken<T>, provider: FactoryProvider<T>): DependencyContainer; | ||
register<T>(token: InjectionToken<T>, provider: TokenProvider<T>, options?: RegistrationOptions): DependencyContainer; | ||
register<T>(token: InjectionToken<T>, provider: ClassProvider<T>, options?: RegistrationOptions): DependencyContainer; | ||
registerType<T>(from: InjectionToken<T>, to: InjectionToken<T>): DependencyContainer; | ||
registerInstance<T>(token: InjectionToken<T>, instance: T): DependencyContainer; | ||
registerSingleton<T>(from: InjectionToken<T>, to: InjectionToken<T>): DependencyContainer; | ||
registerSingleton<T>(token: constructor<T>): DependencyContainer; | ||
resolve<T>(token: InjectionToken<T>): T; | ||
@@ -13,3 +21,3 @@ isRegistered<T>(token: InjectionToken<T>): boolean; | ||
createChildContainer(): Types.DependencyContainer; | ||
protected getRegistration<T>(token: InjectionToken<T>): [Provider<any>, any] | null; | ||
private getRegistration<T>(token); | ||
private construct<T>(ctor); | ||
@@ -16,0 +24,0 @@ } |
@@ -1,4 +0,4 @@ | ||
import { constructor, DependencyContainer } from "./types"; | ||
import { DependencyContainer, constructor } from "./types"; | ||
export declare type FactoryFunction<T> = (dependencyContainer: DependencyContainer) => T; | ||
export declare function instanceCachingFactory<T>(factoryFunc: FactoryFunction<T>): FactoryFunction<T>; | ||
export declare function predicateAwareClassFactory<T>(predicate: (dependencyContainer: DependencyContainer) => boolean, trueConstructor: constructor<T>, falseConstructor: constructor<T>, useCaching?: boolean): FactoryFunction<T>; |
@@ -0,16 +1,9 @@ | ||
import * as Types from "./types"; | ||
import * as decorators from "./decorators"; | ||
import * as factories from "./factories"; | ||
import * as providers from "./providers"; | ||
import * as Types from "./types"; | ||
declare module "tsyringe" { | ||
export { DependencyContainer } from "./types"; | ||
export { factories }; | ||
export { providers }; | ||
export { decorators }; | ||
export const container: Types.DependencyContainer; | ||
} | ||
declare module "tsyringe/decorators" { | ||
export { decorators }; | ||
} | ||
export { DependencyContainer } from "./types"; | ||
export { factories }; | ||
export { providers }; | ||
export { decorators }; | ||
export declare const container: Types.DependencyContainer; |
import { DependencyContainer } from "./types"; | ||
import { constructor } from "./types"; | ||
export declare type InjectionToken<T> = constructor<T> | string; | ||
export interface BaseProvider { | ||
token: InjectionToken<any>; | ||
} | ||
export interface ClassProvider<T> extends BaseProvider { | ||
export declare type InjectionToken<T = any> = constructor<T> | string; | ||
export interface ClassProvider<T> { | ||
useClass: constructor<T>; | ||
} | ||
export interface ValueProvider<T> extends BaseProvider { | ||
export interface ValueProvider<T> { | ||
useValue: T; | ||
} | ||
export interface TokenProvider<T> extends BaseProvider { | ||
export interface TokenProvider<T> { | ||
useToken: InjectionToken<T>; | ||
} | ||
export interface FactoryProvider<T> extends BaseProvider { | ||
export interface FactoryProvider<T> { | ||
useFactory: (dependencyContainer: DependencyContainer) => T; | ||
} | ||
export declare type Provider<T> = constructor<T> | ClassProvider<T> | ValueProvider<T> | TokenProvider<T> | FactoryProvider<T>; | ||
export declare type Provider<T> = ClassProvider<T> | ValueProvider<T> | TokenProvider<T> | FactoryProvider<T>; | ||
export declare function isClassProvider<T>(provider: Provider<T>): provider is ClassProvider<any>; | ||
@@ -21,0 +18,0 @@ export declare function isValueProvider<T>(provider: Provider<T>): provider is ValueProvider<T>; |
@@ -1,2 +0,2 @@ | ||
import { Provider, InjectionToken } from "./providers"; | ||
import { ClassProvider, FactoryProvider, InjectionToken, TokenProvider, ValueProvider } from "./providers"; | ||
export declare type constructor<T> = { | ||
@@ -8,4 +8,14 @@ new (...args: any[]): T; | ||
}; | ||
export declare type RegistrationOptions = { | ||
singleton: boolean; | ||
}; | ||
export interface DependencyContainer { | ||
register<T>(provider: Provider<T>): void; | ||
register<T>(token: InjectionToken<T>, provider: ValueProvider<T>): DependencyContainer; | ||
register<T>(token: InjectionToken<T>, provider: FactoryProvider<T>): DependencyContainer; | ||
register<T>(token: InjectionToken<T>, provider: TokenProvider<T>, options?: RegistrationOptions): DependencyContainer; | ||
register<T>(token: InjectionToken<T>, provider: ClassProvider<T>, options?: RegistrationOptions): DependencyContainer; | ||
registerSingleton<T>(from: InjectionToken<T>, to: InjectionToken<T>): DependencyContainer; | ||
registerSingleton<T>(token: constructor<T>): DependencyContainer; | ||
registerType<T>(from: InjectionToken<T>, to: InjectionToken<T>): DependencyContainer; | ||
registerInstance<T>(token: InjectionToken<T>, instance: T): DependencyContainer; | ||
resolve<T>(token: InjectionToken<T>): T; | ||
@@ -12,0 +22,0 @@ isRegistered<T>(token: InjectionToken<T>): boolean; |
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
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
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
21878
327
1