Comparing version 4.6.0 to 4.7.0
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.instance = exports.typeInfo = void 0; | ||
const tslib_1 = require("tslib"); | ||
const providers_1 = require("./providers"); | ||
@@ -12,2 +13,3 @@ const provider_1 = require("./providers/provider"); | ||
const lazy_helpers_1 = require("./lazy-helpers"); | ||
const disposable_1 = require("./types/disposable"); | ||
const interceptors_1 = require("./interceptors"); | ||
@@ -20,4 +22,7 @@ exports.typeInfo = new Map(); | ||
this.interceptors = new interceptors_1.default(); | ||
this.disposed = false; | ||
this.disposables = new Set(); | ||
} | ||
register(token, providerOrConstructor, options = { lifecycle: lifecycle_1.default.Transient }) { | ||
this.ensureNotDisposed(); | ||
let provider; | ||
@@ -59,2 +64,3 @@ if (!provider_1.isProvider(providerOrConstructor)) { | ||
registerType(from, to) { | ||
this.ensureNotDisposed(); | ||
if (providers_1.isNormalToken(to)) { | ||
@@ -70,2 +76,3 @@ return this.register(from, { | ||
registerInstance(token, instance) { | ||
this.ensureNotDisposed(); | ||
return this.register(token, { | ||
@@ -76,2 +83,3 @@ useValue: instance | ||
registerSingleton(from, to) { | ||
this.ensureNotDisposed(); | ||
if (providers_1.isNormalToken(from)) { | ||
@@ -99,2 +107,3 @@ if (providers_1.isNormalToken(to)) { | ||
resolve(token, context = new resolution_context_1.default()) { | ||
this.ensureNotDisposed(); | ||
const registration = this.getRegistration(token); | ||
@@ -142,2 +151,3 @@ if (!registration && providers_1.isNormalToken(token)) { | ||
resolveRegistration(registration, context) { | ||
this.ensureNotDisposed(); | ||
if (registration.options.lifecycle === lifecycle_1.default.ResolutionScoped && | ||
@@ -178,2 +188,3 @@ context.scopedResolutions.has(registration)) { | ||
resolveAll(token, context = new resolution_context_1.default()) { | ||
this.ensureNotDisposed(); | ||
const registrations = this.getAllRegistrations(token); | ||
@@ -194,2 +205,3 @@ if (!registrations && providers_1.isNormalToken(token)) { | ||
isRegistered(token, recursive = false) { | ||
this.ensureNotDisposed(); | ||
return (this._registry.has(token) || | ||
@@ -201,2 +213,3 @@ (recursive && | ||
reset() { | ||
this.ensureNotDisposed(); | ||
this._registry.clear(); | ||
@@ -207,2 +220,3 @@ this.interceptors.preResolution.clear(); | ||
clearInstances() { | ||
this.ensureNotDisposed(); | ||
for (const [token, registrations] of this._registry.entries()) { | ||
@@ -218,2 +232,3 @@ this._registry.setAll(token, registrations | ||
createChildContainer() { | ||
this.ensureNotDisposed(); | ||
const childContainer = new InternalDependencyContainer(this); | ||
@@ -247,2 +262,15 @@ for (const [token, registrations] of this._registry.entries()) { | ||
} | ||
dispose() { | ||
return tslib_1.__awaiter(this, void 0, void 0, function* () { | ||
this.disposed = true; | ||
const promises = []; | ||
this.disposables.forEach(disposable => { | ||
const maybePromise = disposable.dispose(); | ||
if (maybePromise) { | ||
promises.push(maybePromise); | ||
} | ||
}); | ||
yield Promise.all(promises); | ||
}); | ||
} | ||
getRegistration(token) { | ||
@@ -270,13 +298,19 @@ if (this.isRegistered(token)) { | ||
} | ||
const paramInfo = exports.typeInfo.get(ctor); | ||
if (!paramInfo || paramInfo.length === 0) { | ||
if (ctor.length === 0) { | ||
return new ctor(); | ||
const instance = (() => { | ||
const paramInfo = exports.typeInfo.get(ctor); | ||
if (!paramInfo || paramInfo.length === 0) { | ||
if (ctor.length === 0) { | ||
return new ctor(); | ||
} | ||
else { | ||
throw new Error(`TypeInfo not known for "${ctor.name}"`); | ||
} | ||
} | ||
else { | ||
throw new Error(`TypeInfo not known for "${ctor.name}"`); | ||
} | ||
const params = paramInfo.map(this.resolveParams(context, ctor)); | ||
return new ctor(...params); | ||
})(); | ||
if (disposable_1.isDisposable(instance)) { | ||
this.disposables.add(instance); | ||
} | ||
const params = paramInfo.map(this.resolveParams(context, ctor)); | ||
return new ctor(...params); | ||
return instance; | ||
} | ||
@@ -308,4 +342,9 @@ resolveParams(context, ctor) { | ||
} | ||
ensureNotDisposed() { | ||
if (this.disposed) { | ||
throw new Error("This container has been disposed, you cannot interact with a disposed container"); | ||
} | ||
} | ||
} | ||
exports.instance = new InternalDependencyContainer(); | ||
exports.default = exports.instance; |
@@ -0,1 +1,2 @@ | ||
import { __awaiter } from "tslib"; | ||
import { isClassProvider, isFactoryProvider, isNormalToken, isTokenProvider, isValueProvider } from "./providers"; | ||
@@ -9,2 +10,3 @@ import { isProvider } from "./providers/provider"; | ||
import { DelayedConstructor } from "./lazy-helpers"; | ||
import { isDisposable } from "./types/disposable"; | ||
import Interceptors from "./interceptors"; | ||
@@ -17,4 +19,7 @@ export const typeInfo = new Map(); | ||
this.interceptors = new Interceptors(); | ||
this.disposed = false; | ||
this.disposables = new Set(); | ||
} | ||
register(token, providerOrConstructor, options = { lifecycle: Lifecycle.Transient }) { | ||
this.ensureNotDisposed(); | ||
let provider; | ||
@@ -56,2 +61,3 @@ if (!isProvider(providerOrConstructor)) { | ||
registerType(from, to) { | ||
this.ensureNotDisposed(); | ||
if (isNormalToken(to)) { | ||
@@ -67,2 +73,3 @@ return this.register(from, { | ||
registerInstance(token, instance) { | ||
this.ensureNotDisposed(); | ||
return this.register(token, { | ||
@@ -73,2 +80,3 @@ useValue: instance | ||
registerSingleton(from, to) { | ||
this.ensureNotDisposed(); | ||
if (isNormalToken(from)) { | ||
@@ -96,2 +104,3 @@ if (isNormalToken(to)) { | ||
resolve(token, context = new ResolutionContext()) { | ||
this.ensureNotDisposed(); | ||
const registration = this.getRegistration(token); | ||
@@ -139,2 +148,3 @@ if (!registration && isNormalToken(token)) { | ||
resolveRegistration(registration, context) { | ||
this.ensureNotDisposed(); | ||
if (registration.options.lifecycle === Lifecycle.ResolutionScoped && | ||
@@ -175,2 +185,3 @@ context.scopedResolutions.has(registration)) { | ||
resolveAll(token, context = new ResolutionContext()) { | ||
this.ensureNotDisposed(); | ||
const registrations = this.getAllRegistrations(token); | ||
@@ -191,2 +202,3 @@ if (!registrations && isNormalToken(token)) { | ||
isRegistered(token, recursive = false) { | ||
this.ensureNotDisposed(); | ||
return (this._registry.has(token) || | ||
@@ -198,2 +210,3 @@ (recursive && | ||
reset() { | ||
this.ensureNotDisposed(); | ||
this._registry.clear(); | ||
@@ -204,2 +217,3 @@ this.interceptors.preResolution.clear(); | ||
clearInstances() { | ||
this.ensureNotDisposed(); | ||
for (const [token, registrations] of this._registry.entries()) { | ||
@@ -215,2 +229,3 @@ this._registry.setAll(token, registrations | ||
createChildContainer() { | ||
this.ensureNotDisposed(); | ||
const childContainer = new InternalDependencyContainer(this); | ||
@@ -244,2 +259,15 @@ for (const [token, registrations] of this._registry.entries()) { | ||
} | ||
dispose() { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
this.disposed = true; | ||
const promises = []; | ||
this.disposables.forEach(disposable => { | ||
const maybePromise = disposable.dispose(); | ||
if (maybePromise) { | ||
promises.push(maybePromise); | ||
} | ||
}); | ||
yield Promise.all(promises); | ||
}); | ||
} | ||
getRegistration(token) { | ||
@@ -267,13 +295,19 @@ if (this.isRegistered(token)) { | ||
} | ||
const paramInfo = typeInfo.get(ctor); | ||
if (!paramInfo || paramInfo.length === 0) { | ||
if (ctor.length === 0) { | ||
return new ctor(); | ||
const instance = (() => { | ||
const paramInfo = typeInfo.get(ctor); | ||
if (!paramInfo || paramInfo.length === 0) { | ||
if (ctor.length === 0) { | ||
return new ctor(); | ||
} | ||
else { | ||
throw new Error(`TypeInfo not known for "${ctor.name}"`); | ||
} | ||
} | ||
else { | ||
throw new Error(`TypeInfo not known for "${ctor.name}"`); | ||
} | ||
const params = paramInfo.map(this.resolveParams(context, ctor)); | ||
return new ctor(...params); | ||
})(); | ||
if (isDisposable(instance)) { | ||
this.disposables.add(instance); | ||
} | ||
const params = paramInfo.map(this.resolveParams(context, ctor)); | ||
return new ctor(...params); | ||
return instance; | ||
} | ||
@@ -305,4 +339,9 @@ resolveParams(context, ctor) { | ||
} | ||
ensureNotDisposed() { | ||
if (this.disposed) { | ||
throw new Error("This container has been disposed, you cannot interact with a disposed container"); | ||
} | ||
} | ||
} | ||
export const instance = new InternalDependencyContainer(); | ||
export default instance; |
@@ -1,2 +0,2 @@ | ||
import { __read, __spread, __values } from "tslib"; | ||
import { __awaiter, __generator, __read, __spread, __values } from "tslib"; | ||
import { isClassProvider, isFactoryProvider, isNormalToken, isTokenProvider, isValueProvider } from "./providers"; | ||
@@ -10,2 +10,3 @@ import { isProvider } from "./providers/provider"; | ||
import { DelayedConstructor } from "./lazy-helpers"; | ||
import { isDisposable } from "./types/disposable"; | ||
import Interceptors from "./interceptors"; | ||
@@ -18,5 +19,8 @@ export var typeInfo = new Map(); | ||
this.interceptors = new Interceptors(); | ||
this.disposed = false; | ||
this.disposables = new Set(); | ||
} | ||
InternalDependencyContainer.prototype.register = function (token, providerOrConstructor, options) { | ||
if (options === void 0) { options = { lifecycle: Lifecycle.Transient }; } | ||
this.ensureNotDisposed(); | ||
var provider; | ||
@@ -58,2 +62,3 @@ if (!isProvider(providerOrConstructor)) { | ||
InternalDependencyContainer.prototype.registerType = function (from, to) { | ||
this.ensureNotDisposed(); | ||
if (isNormalToken(to)) { | ||
@@ -69,2 +74,3 @@ return this.register(from, { | ||
InternalDependencyContainer.prototype.registerInstance = function (token, instance) { | ||
this.ensureNotDisposed(); | ||
return this.register(token, { | ||
@@ -75,2 +81,3 @@ useValue: instance | ||
InternalDependencyContainer.prototype.registerSingleton = function (from, to) { | ||
this.ensureNotDisposed(); | ||
if (isNormalToken(from)) { | ||
@@ -99,2 +106,3 @@ if (isNormalToken(to)) { | ||
if (context === void 0) { context = new ResolutionContext(); } | ||
this.ensureNotDisposed(); | ||
var registration = this.getRegistration(token); | ||
@@ -164,2 +172,3 @@ if (!registration && isNormalToken(token)) { | ||
InternalDependencyContainer.prototype.resolveRegistration = function (registration, context) { | ||
this.ensureNotDisposed(); | ||
if (registration.options.lifecycle === Lifecycle.ResolutionScoped && | ||
@@ -202,2 +211,3 @@ context.scopedResolutions.has(registration)) { | ||
if (context === void 0) { context = new ResolutionContext(); } | ||
this.ensureNotDisposed(); | ||
var registrations = this.getAllRegistrations(token); | ||
@@ -221,2 +231,3 @@ if (!registrations && isNormalToken(token)) { | ||
if (recursive === void 0) { recursive = false; } | ||
this.ensureNotDisposed(); | ||
return (this._registry.has(token) || | ||
@@ -228,2 +239,3 @@ (recursive && | ||
InternalDependencyContainer.prototype.reset = function () { | ||
this.ensureNotDisposed(); | ||
this._registry.clear(); | ||
@@ -235,2 +247,3 @@ this.interceptors.preResolution.clear(); | ||
var e_3, _a; | ||
this.ensureNotDisposed(); | ||
try { | ||
@@ -257,2 +270,3 @@ for (var _b = __values(this._registry.entries()), _c = _b.next(); !_c.done; _c = _b.next()) { | ||
var e_4, _a; | ||
this.ensureNotDisposed(); | ||
var childContainer = new InternalDependencyContainer(this); | ||
@@ -301,2 +315,24 @@ try { | ||
}; | ||
InternalDependencyContainer.prototype.dispose = function () { | ||
return __awaiter(this, void 0, void 0, function () { | ||
var promises; | ||
return __generator(this, function (_a) { | ||
switch (_a.label) { | ||
case 0: | ||
this.disposed = true; | ||
promises = []; | ||
this.disposables.forEach(function (disposable) { | ||
var maybePromise = disposable.dispose(); | ||
if (maybePromise) { | ||
promises.push(maybePromise); | ||
} | ||
}); | ||
return [4, Promise.all(promises)]; | ||
case 1: | ||
_a.sent(); | ||
return [2]; | ||
} | ||
}); | ||
}); | ||
}; | ||
InternalDependencyContainer.prototype.getRegistration = function (token) { | ||
@@ -327,13 +363,19 @@ if (this.isRegistered(token)) { | ||
} | ||
var paramInfo = typeInfo.get(ctor); | ||
if (!paramInfo || paramInfo.length === 0) { | ||
if (ctor.length === 0) { | ||
return new ctor(); | ||
var instance = (function () { | ||
var paramInfo = typeInfo.get(ctor); | ||
if (!paramInfo || paramInfo.length === 0) { | ||
if (ctor.length === 0) { | ||
return new ctor(); | ||
} | ||
else { | ||
throw new Error("TypeInfo not known for \"" + ctor.name + "\""); | ||
} | ||
} | ||
else { | ||
throw new Error("TypeInfo not known for \"" + ctor.name + "\""); | ||
} | ||
var params = paramInfo.map(_this.resolveParams(context, ctor)); | ||
return new (ctor.bind.apply(ctor, __spread([void 0], params)))(); | ||
})(); | ||
if (isDisposable(instance)) { | ||
this.disposables.add(instance); | ||
} | ||
var params = paramInfo.map(this.resolveParams(context, ctor)); | ||
return new (ctor.bind.apply(ctor, __spread([void 0], params)))(); | ||
return instance; | ||
}; | ||
@@ -366,2 +408,7 @@ InternalDependencyContainer.prototype.resolveParams = function (context, ctor) { | ||
}; | ||
InternalDependencyContainer.prototype.ensureNotDisposed = function () { | ||
if (this.disposed) { | ||
throw new Error("This container has been disposed, you cannot interact with a disposed container"); | ||
} | ||
}; | ||
return InternalDependencyContainer; | ||
@@ -368,0 +415,0 @@ }()); |
@@ -1,2 +0,2 @@ | ||
export { DependencyContainer, Lifecycle, RegistrationOptions, Frequency } from "./types"; | ||
export { DependencyContainer, Disposable, Lifecycle, RegistrationOptions, Frequency } from "./types"; | ||
export * from "./decorators"; | ||
@@ -3,0 +3,0 @@ export * from "./factories"; |
@@ -8,2 +8,3 @@ import FactoryProvider from "../providers/factory-provider"; | ||
import RegistrationOptions from "./registration-options"; | ||
import Disposable from "./disposable"; | ||
import InterceptorOptions from "./interceptor-options"; | ||
@@ -26,3 +27,3 @@ export declare type ResolutionType = "Single" | "All"; | ||
} | ||
export default interface DependencyContainer { | ||
export default interface DependencyContainer extends Disposable { | ||
register<T>(token: InjectionToken<T>, provider: ValueProvider<T>): DependencyContainer; | ||
@@ -73,2 +74,7 @@ register<T>(token: InjectionToken<T>, provider: FactoryProvider<T>): DependencyContainer; | ||
afterResolution<T>(token: InjectionToken<T>, callback: PostResolutionInterceptorCallback<T>, options?: InterceptorOptions): void; | ||
/** | ||
* Calls `.dispose()` on all disposable instances created by the container. | ||
* After calling this, the container may no longer be used. | ||
*/ | ||
dispose(): Promise<void> | void; | ||
} |
@@ -6,4 +6,5 @@ export { default as constructor } from "./constructor"; | ||
export { default as Lifecycle } from "./lifecycle"; | ||
export { default as Disposable } from "./disposable"; | ||
export { default as InterceptionOptions } from "./interceptor-options"; | ||
export { default as Frequency } from "./frequency"; | ||
export { default as Transform } from "./transform"; |
{ | ||
"name": "tsyringe", | ||
"version": "4.6.0", | ||
"version": "4.7.0", | ||
"description": "Lightweight dependency injection container for JavaScript/TypeScript", | ||
@@ -5,0 +5,0 @@ "main": "dist/cjs/index.js", |
@@ -36,2 +36,3 @@ [![Travis](https://img.shields.io/travis/Microsoft/tsyringe.svg)](https://travis-ci.org/Microsoft/tsyringe/) | ||
- [Interfaces and circular dependencies](#interfaces-and-circular-dependencies) | ||
- [Disposable instances](#disposable-instances) | ||
- [Full examples](#full-examples) | ||
@@ -223,6 +224,6 @@ - [Example without interfaces](#example-without-interfaces) | ||
@injectable | ||
@injectable() | ||
class Foo {} | ||
@injectable | ||
@injectable() | ||
class Bar { | ||
@@ -235,3 +236,3 @@ constructor(@injectAll(Foo) fooArray: Foo[]) { | ||
### injectWithTransform | ||
### injectWithTransform() | ||
@@ -261,3 +262,3 @@ Parameter decorator which allows for a transformer object to take an action on the resolved object | ||
### injectAllWithTransform | ||
### injectAllWithTransform() | ||
@@ -268,3 +269,3 @@ This parameter decorator allows for array contents to be passed through a transformer. The transformer can return any type, so this | ||
```typescript | ||
@injectable | ||
@injectable() | ||
class Foo { | ||
@@ -280,3 +281,3 @@ public value; | ||
@injectable | ||
@injectable() | ||
class Bar { | ||
@@ -422,3 +423,4 @@ constructor(@injectAllWithTransform(Foo, FooTransform) stringArray: string[]) { | ||
{ | ||
token: useFactory: predicateAwareClassFactory<Foo>( | ||
token: "FooHttp", | ||
useFactory: predicateAwareClassFactory<Foo>( | ||
c => c.resolve(Bar).useHttps, // Predicate for evaluation | ||
@@ -690,2 +692,16 @@ FooHttps, // A FooHttps will be resolved from the container if predicate is true | ||
# Disposable instances | ||
All instances created by the container that implement the [`Disposable`](./src/types/disposable.ts) | ||
interface will automatically be disposed of when the container is disposed. | ||
```typescript | ||
container.dispose(); | ||
``` | ||
or to await all asynchronous disposals: | ||
```typescript | ||
await container.dispose(); | ||
``` | ||
# Full examples | ||
@@ -692,0 +708,0 @@ |
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
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
140390
167
2754
822