Comparing version 3.4.0 to 4.0.0
@@ -15,1 +15,3 @@ "use strict"; | ||
exports.injectAll = inject_all_1.default; | ||
var scoped_1 = require("./scoped"); | ||
exports.scoped = scoped_1.default; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const providers_1 = require("./providers"); | ||
const provider_1 = require("./providers/provider"); | ||
const injection_token_1 = require("./providers/injection-token"); | ||
const registry_1 = require("./registry"); | ||
const lifecycle_1 = require("./types/lifecycle"); | ||
const resolution_context_1 = require("./resolution-context"); | ||
exports.typeInfo = new Map(); | ||
@@ -12,6 +15,15 @@ class InternalDependencyContainer { | ||
} | ||
register(token, provider, options = { singleton: false }) { | ||
if (options.singleton) { | ||
register(token, providerOrConstructor, options = { lifecycle: lifecycle_1.default.Transient }) { | ||
let provider; | ||
if (!provider_1.isProvider(providerOrConstructor)) { | ||
provider = { useClass: providerOrConstructor }; | ||
} | ||
else { | ||
provider = providerOrConstructor; | ||
} | ||
if (options.lifecycle === lifecycle_1.default.Singleton || | ||
options.lifecycle == lifecycle_1.default.ContainerScoped || | ||
options.lifecycle == lifecycle_1.default.ResolutionScoped) { | ||
if (providers_1.isValueProvider(provider) || providers_1.isFactoryProvider(provider)) { | ||
throw "Cannot use {singleton: true} with ValueProviders or FactoryProviders"; | ||
throw `Cannot use lifecycle "${lifecycle_1.default[options.lifecycle]}" with ValueProviders or FactoryProviders`; | ||
} | ||
@@ -42,3 +54,3 @@ } | ||
useToken: to | ||
}, { singleton: true }); | ||
}, { lifecycle: lifecycle_1.default.Singleton }); | ||
} | ||
@@ -48,3 +60,3 @@ else if (to) { | ||
useClass: to | ||
}, { singleton: true }); | ||
}, { lifecycle: lifecycle_1.default.Singleton }); | ||
} | ||
@@ -59,5 +71,5 @@ throw "Cannot register a type name as a singleton without a \"to\" token"; | ||
useClass | ||
}, { singleton: true }); | ||
}, { lifecycle: lifecycle_1.default.Singleton }); | ||
} | ||
resolve(token) { | ||
resolve(token, context = new resolution_context_1.default()) { | ||
const registration = this.getRegistration(token); | ||
@@ -68,41 +80,56 @@ if (!registration && providers_1.isNormalToken(token)) { | ||
if (registration) { | ||
return this.resolveRegistration(registration); | ||
return this.resolveRegistration(registration, context); | ||
} | ||
return this.construct(token); | ||
return this.construct(token, context); | ||
} | ||
resolveRegistration(registration) { | ||
resolveRegistration(registration, context) { | ||
if (registration.options.lifecycle === lifecycle_1.default.ResolutionScoped && | ||
context.scopedResolutions.has(registration)) { | ||
return context.scopedResolutions.get(registration); | ||
} | ||
const isSingleton = registration.options.lifecycle === lifecycle_1.default.Singleton; | ||
const isContainerScoped = registration.options.lifecycle === lifecycle_1.default.ContainerScoped; | ||
const returnInstance = isSingleton || isContainerScoped; | ||
let resolved; | ||
if (providers_1.isValueProvider(registration.provider)) { | ||
return registration.provider.useValue; | ||
resolved = registration.provider.useValue; | ||
} | ||
else if (providers_1.isTokenProvider(registration.provider)) { | ||
return registration.options.singleton | ||
resolved = returnInstance | ||
? registration.instance || | ||
(registration.instance = this.resolve(registration.provider.useToken)) | ||
: this.resolve(registration.provider.useToken); | ||
(registration.instance = this.resolve(registration.provider.useToken, context)) | ||
: this.resolve(registration.provider.useToken, context); | ||
} | ||
else if (providers_1.isClassProvider(registration.provider)) { | ||
return registration.options.singleton | ||
resolved = returnInstance | ||
? registration.instance || | ||
(registration.instance = this.construct(registration.provider.useClass)) | ||
: this.construct(registration.provider.useClass); | ||
(registration.instance = this.construct(registration.provider.useClass, context)) | ||
: this.construct(registration.provider.useClass, context); | ||
} | ||
else if (providers_1.isFactoryProvider(registration.provider)) { | ||
return registration.provider.useFactory(this); | ||
resolved = registration.provider.useFactory(this); | ||
} | ||
else { | ||
return this.construct(registration.provider); | ||
resolved = this.construct(registration.provider, context); | ||
} | ||
if (registration.options.lifecycle === lifecycle_1.default.ResolutionScoped) { | ||
context.scopedResolutions.set(registration, resolved); | ||
} | ||
return resolved; | ||
} | ||
resolveAll(token) { | ||
const registration = this.getAllRegistrations(token); | ||
if (!registration && providers_1.isNormalToken(token)) { | ||
resolveAll(token, context = new resolution_context_1.default()) { | ||
const registrations = this.getAllRegistrations(token); | ||
if (!registrations && providers_1.isNormalToken(token)) { | ||
throw `Attempted to resolve unregistered dependency token: ${token.toString()}`; | ||
} | ||
if (registration) { | ||
return registration.map(item => this.resolveRegistration(item)); | ||
if (registrations) { | ||
return registrations.map(item => this.resolveRegistration(item, context)); | ||
} | ||
return [this.construct(token)]; | ||
return [this.construct(token, context)]; | ||
} | ||
isRegistered(token) { | ||
return this._registry.has(token); | ||
isRegistered(token, recursive = false) { | ||
return (this._registry.has(token) || | ||
(recursive && | ||
(this.parent || false) && | ||
this.parent.isRegistered(token, true))); | ||
} | ||
@@ -113,3 +140,17 @@ reset() { | ||
createChildContainer() { | ||
return new InternalDependencyContainer(this); | ||
const childContainer = new InternalDependencyContainer(this); | ||
for (const [token, registrations] of this._registry.entries()) { | ||
if (registrations.some(({ options }) => options.lifecycle === lifecycle_1.default.ContainerScoped)) { | ||
childContainer._registry.setAll(token, registrations.map(registration => { | ||
if (registration.options.lifecycle === lifecycle_1.default.ContainerScoped) { | ||
return { | ||
provider: registration.provider, | ||
options: registration.options | ||
}; | ||
} | ||
return registration; | ||
})); | ||
} | ||
} | ||
return childContainer; | ||
} | ||
@@ -134,3 +175,3 @@ getRegistration(token) { | ||
} | ||
construct(ctor) { | ||
construct(ctor, context) { | ||
if (ctor.length === 0) { | ||
@@ -147,5 +188,5 @@ return new ctor(); | ||
? this.resolveAll(param.token) | ||
: this.resolve(param.token); | ||
: this.resolve(param.token, context); | ||
} | ||
return this.resolve(param); | ||
return this.resolve(param, context); | ||
}); | ||
@@ -152,0 +193,0 @@ return new ctor(...params); |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const class_provider_1 = require("./class-provider"); | ||
const value_provider_1 = require("./value-provider"); | ||
const token_provider_1 = require("./token-provider"); | ||
const factory_provider_1 = require("./factory-provider"); | ||
function isProvider(provider) { | ||
return (class_provider_1.isClassProvider(provider) || | ||
value_provider_1.isValueProvider(provider) || | ||
token_provider_1.isTokenProvider(provider) || | ||
factory_provider_1.isFactoryProvider(provider)); | ||
} | ||
exports.isProvider = isProvider; |
@@ -7,2 +7,5 @@ "use strict"; | ||
} | ||
entries() { | ||
return this._registryMap.entries(); | ||
} | ||
getAll(key) { | ||
@@ -21,2 +24,5 @@ this.ensure(key); | ||
} | ||
setAll(key, value) { | ||
this._registryMap.set(key, value); | ||
} | ||
has(key) { | ||
@@ -23,0 +29,0 @@ this.ensure(key); |
@@ -7,1 +7,2 @@ export { default as autoInjectable } from "./auto-injectable"; | ||
export { default as injectAll } from "./inject-all"; | ||
export { default as scoped } from "./scoped"; |
@@ -1,2 +0,2 @@ | ||
import * as tslib_1 from "tslib"; | ||
import { __rest } from "tslib"; | ||
import { instance as globalContainer } from "../dependency-container"; | ||
@@ -6,3 +6,3 @@ function registry(registrations = []) { | ||
registrations.forEach((_a) => { | ||
var { token, options } = _a, provider = tslib_1.__rest(_a, ["token", "options"]); | ||
var { token, options } = _a, provider = __rest(_a, ["token", "options"]); | ||
return globalContainer.register(token, provider, options); | ||
@@ -9,0 +9,0 @@ }); |
import { isClassProvider, isFactoryProvider, isNormalToken, isTokenProvider, isValueProvider } from "./providers"; | ||
import { isProvider } from "./providers/provider"; | ||
import { isTokenDescriptor } from "./providers/injection-token"; | ||
import Registry from "./registry"; | ||
import Lifecycle from "./types/lifecycle"; | ||
import ResolutionContext from "./resolution-context"; | ||
export const typeInfo = new Map(); | ||
@@ -10,6 +13,15 @@ class InternalDependencyContainer { | ||
} | ||
register(token, provider, options = { singleton: false }) { | ||
if (options.singleton) { | ||
register(token, providerOrConstructor, options = { lifecycle: Lifecycle.Transient }) { | ||
let provider; | ||
if (!isProvider(providerOrConstructor)) { | ||
provider = { useClass: providerOrConstructor }; | ||
} | ||
else { | ||
provider = providerOrConstructor; | ||
} | ||
if (options.lifecycle === Lifecycle.Singleton || | ||
options.lifecycle == Lifecycle.ContainerScoped || | ||
options.lifecycle == Lifecycle.ResolutionScoped) { | ||
if (isValueProvider(provider) || isFactoryProvider(provider)) { | ||
throw "Cannot use {singleton: true} with ValueProviders or FactoryProviders"; | ||
throw `Cannot use lifecycle "${Lifecycle[options.lifecycle]}" with ValueProviders or FactoryProviders`; | ||
} | ||
@@ -40,3 +52,3 @@ } | ||
useToken: to | ||
}, { singleton: true }); | ||
}, { lifecycle: Lifecycle.Singleton }); | ||
} | ||
@@ -46,3 +58,3 @@ else if (to) { | ||
useClass: to | ||
}, { singleton: true }); | ||
}, { lifecycle: Lifecycle.Singleton }); | ||
} | ||
@@ -57,5 +69,5 @@ throw "Cannot register a type name as a singleton without a \"to\" token"; | ||
useClass | ||
}, { singleton: true }); | ||
}, { lifecycle: Lifecycle.Singleton }); | ||
} | ||
resolve(token) { | ||
resolve(token, context = new ResolutionContext()) { | ||
const registration = this.getRegistration(token); | ||
@@ -66,41 +78,56 @@ if (!registration && isNormalToken(token)) { | ||
if (registration) { | ||
return this.resolveRegistration(registration); | ||
return this.resolveRegistration(registration, context); | ||
} | ||
return this.construct(token); | ||
return this.construct(token, context); | ||
} | ||
resolveRegistration(registration) { | ||
resolveRegistration(registration, context) { | ||
if (registration.options.lifecycle === Lifecycle.ResolutionScoped && | ||
context.scopedResolutions.has(registration)) { | ||
return context.scopedResolutions.get(registration); | ||
} | ||
const isSingleton = registration.options.lifecycle === Lifecycle.Singleton; | ||
const isContainerScoped = registration.options.lifecycle === Lifecycle.ContainerScoped; | ||
const returnInstance = isSingleton || isContainerScoped; | ||
let resolved; | ||
if (isValueProvider(registration.provider)) { | ||
return registration.provider.useValue; | ||
resolved = registration.provider.useValue; | ||
} | ||
else if (isTokenProvider(registration.provider)) { | ||
return registration.options.singleton | ||
resolved = returnInstance | ||
? registration.instance || | ||
(registration.instance = this.resolve(registration.provider.useToken)) | ||
: this.resolve(registration.provider.useToken); | ||
(registration.instance = this.resolve(registration.provider.useToken, context)) | ||
: this.resolve(registration.provider.useToken, context); | ||
} | ||
else if (isClassProvider(registration.provider)) { | ||
return registration.options.singleton | ||
resolved = returnInstance | ||
? registration.instance || | ||
(registration.instance = this.construct(registration.provider.useClass)) | ||
: this.construct(registration.provider.useClass); | ||
(registration.instance = this.construct(registration.provider.useClass, context)) | ||
: this.construct(registration.provider.useClass, context); | ||
} | ||
else if (isFactoryProvider(registration.provider)) { | ||
return registration.provider.useFactory(this); | ||
resolved = registration.provider.useFactory(this); | ||
} | ||
else { | ||
return this.construct(registration.provider); | ||
resolved = this.construct(registration.provider, context); | ||
} | ||
if (registration.options.lifecycle === Lifecycle.ResolutionScoped) { | ||
context.scopedResolutions.set(registration, resolved); | ||
} | ||
return resolved; | ||
} | ||
resolveAll(token) { | ||
const registration = this.getAllRegistrations(token); | ||
if (!registration && isNormalToken(token)) { | ||
resolveAll(token, context = new ResolutionContext()) { | ||
const registrations = this.getAllRegistrations(token); | ||
if (!registrations && isNormalToken(token)) { | ||
throw `Attempted to resolve unregistered dependency token: ${token.toString()}`; | ||
} | ||
if (registration) { | ||
return registration.map(item => this.resolveRegistration(item)); | ||
if (registrations) { | ||
return registrations.map(item => this.resolveRegistration(item, context)); | ||
} | ||
return [this.construct(token)]; | ||
return [this.construct(token, context)]; | ||
} | ||
isRegistered(token) { | ||
return this._registry.has(token); | ||
isRegistered(token, recursive = false) { | ||
return (this._registry.has(token) || | ||
(recursive && | ||
(this.parent || false) && | ||
this.parent.isRegistered(token, true))); | ||
} | ||
@@ -111,3 +138,17 @@ reset() { | ||
createChildContainer() { | ||
return new InternalDependencyContainer(this); | ||
const childContainer = new InternalDependencyContainer(this); | ||
for (const [token, registrations] of this._registry.entries()) { | ||
if (registrations.some(({ options }) => options.lifecycle === Lifecycle.ContainerScoped)) { | ||
childContainer._registry.setAll(token, registrations.map(registration => { | ||
if (registration.options.lifecycle === Lifecycle.ContainerScoped) { | ||
return { | ||
provider: registration.provider, | ||
options: registration.options | ||
}; | ||
} | ||
return registration; | ||
})); | ||
} | ||
} | ||
return childContainer; | ||
} | ||
@@ -132,3 +173,3 @@ getRegistration(token) { | ||
} | ||
construct(ctor) { | ||
construct(ctor, context) { | ||
if (ctor.length === 0) { | ||
@@ -145,5 +186,5 @@ return new ctor(); | ||
? this.resolveAll(param.token) | ||
: this.resolve(param.token); | ||
: this.resolve(param.token, context); | ||
} | ||
return this.resolve(param); | ||
return this.resolve(param, context); | ||
}); | ||
@@ -150,0 +191,0 @@ return new ctor(...params); |
@@ -5,2 +5,5 @@ export default class Registry { | ||
} | ||
entries() { | ||
return this._registryMap.entries(); | ||
} | ||
getAll(key) { | ||
@@ -19,2 +22,5 @@ this.ensure(key); | ||
} | ||
setAll(key, value) { | ||
this._registryMap.set(key, value); | ||
} | ||
has(key) { | ||
@@ -21,0 +27,0 @@ this.ensure(key); |
@@ -1,2 +0,2 @@ | ||
import * as tslib_1 from "tslib"; | ||
import { __extends, __read, __spread } from "tslib"; | ||
import { getParamInfo } from "../reflection-helpers"; | ||
@@ -9,3 +9,3 @@ import { instance as globalContainer } from "../dependency-container"; | ||
return (function (_super) { | ||
tslib_1.__extends(class_1, _super); | ||
__extends(class_1, _super); | ||
function class_1() { | ||
@@ -16,3 +16,3 @@ var args = []; | ||
} | ||
return _super.apply(this, args.concat(paramInfo.slice(args.length).map(function (type, index) { | ||
return _super.apply(this, __spread(args.concat(paramInfo.slice(args.length).map(function (type, index) { | ||
try { | ||
@@ -28,3 +28,3 @@ if (isTokenDescriptor(type)) { | ||
var argIndex = index + args.length; | ||
var _a = target.toString().match(/constructor\(([\w, ]+)\)/) || [], _b = _a[1], params = _b === void 0 ? null : _b; | ||
var _a = __read(target.toString().match(/constructor\(([\w, ]+)\)/) || [], 2), _b = _a[1], params = _b === void 0 ? null : _b; | ||
var argName = params | ||
@@ -35,3 +35,3 @@ ? params.split(",")[argIndex] | ||
} | ||
}))) || this; | ||
})))) || this; | ||
} | ||
@@ -38,0 +38,0 @@ return class_1; |
@@ -7,1 +7,2 @@ export { default as autoInjectable } from "./auto-injectable"; | ||
export { default as injectAll } from "./inject-all"; | ||
export { default as scoped } from "./scoped"; |
@@ -1,2 +0,2 @@ | ||
import * as tslib_1 from "tslib"; | ||
import { __rest } from "tslib"; | ||
import { instance as globalContainer } from "../dependency-container"; | ||
@@ -7,3 +7,3 @@ function registry(registrations) { | ||
registrations.forEach(function (_a) { | ||
var token = _a.token, options = _a.options, provider = tslib_1.__rest(_a, ["token", "options"]); | ||
var token = _a.token, options = _a.options, provider = __rest(_a, ["token", "options"]); | ||
return globalContainer.register(token, provider, options); | ||
@@ -10,0 +10,0 @@ }); |
@@ -0,4 +1,8 @@ | ||
import { __read, __spread, __values } from "tslib"; | ||
import { isClassProvider, isFactoryProvider, isNormalToken, isTokenProvider, isValueProvider } from "./providers"; | ||
import { isProvider } from "./providers/provider"; | ||
import { isTokenDescriptor } from "./providers/injection-token"; | ||
import Registry from "./registry"; | ||
import Lifecycle from "./types/lifecycle"; | ||
import ResolutionContext from "./resolution-context"; | ||
export var typeInfo = new Map(); | ||
@@ -10,7 +14,16 @@ var InternalDependencyContainer = (function () { | ||
} | ||
InternalDependencyContainer.prototype.register = function (token, provider, options) { | ||
if (options === void 0) { options = { singleton: false }; } | ||
if (options.singleton) { | ||
InternalDependencyContainer.prototype.register = function (token, providerOrConstructor, options) { | ||
if (options === void 0) { options = { lifecycle: Lifecycle.Transient }; } | ||
var provider; | ||
if (!isProvider(providerOrConstructor)) { | ||
provider = { useClass: providerOrConstructor }; | ||
} | ||
else { | ||
provider = providerOrConstructor; | ||
} | ||
if (options.lifecycle === Lifecycle.Singleton || | ||
options.lifecycle == Lifecycle.ContainerScoped || | ||
options.lifecycle == Lifecycle.ResolutionScoped) { | ||
if (isValueProvider(provider) || isFactoryProvider(provider)) { | ||
throw "Cannot use {singleton: true} with ValueProviders or FactoryProviders"; | ||
throw "Cannot use lifecycle \"" + Lifecycle[options.lifecycle] + "\" with ValueProviders or FactoryProviders"; | ||
} | ||
@@ -41,3 +54,3 @@ } | ||
useToken: to | ||
}, { singleton: true }); | ||
}, { lifecycle: Lifecycle.Singleton }); | ||
} | ||
@@ -47,3 +60,3 @@ else if (to) { | ||
useClass: to | ||
}, { singleton: true }); | ||
}, { lifecycle: Lifecycle.Singleton }); | ||
} | ||
@@ -58,5 +71,6 @@ throw "Cannot register a type name as a singleton without a \"to\" token"; | ||
useClass: useClass | ||
}, { singleton: true }); | ||
}, { lifecycle: Lifecycle.Singleton }); | ||
}; | ||
InternalDependencyContainer.prototype.resolve = function (token) { | ||
InternalDependencyContainer.prototype.resolve = function (token, context) { | ||
if (context === void 0) { context = new ResolutionContext(); } | ||
var registration = this.getRegistration(token); | ||
@@ -67,42 +81,61 @@ if (!registration && isNormalToken(token)) { | ||
if (registration) { | ||
return this.resolveRegistration(registration); | ||
return this.resolveRegistration(registration, context); | ||
} | ||
return this.construct(token); | ||
return this.construct(token, context); | ||
}; | ||
InternalDependencyContainer.prototype.resolveRegistration = function (registration) { | ||
InternalDependencyContainer.prototype.resolveRegistration = function (registration, context) { | ||
if (registration.options.lifecycle === Lifecycle.ResolutionScoped && | ||
context.scopedResolutions.has(registration)) { | ||
return context.scopedResolutions.get(registration); | ||
} | ||
var isSingleton = registration.options.lifecycle === Lifecycle.Singleton; | ||
var isContainerScoped = registration.options.lifecycle === Lifecycle.ContainerScoped; | ||
var returnInstance = isSingleton || isContainerScoped; | ||
var resolved; | ||
if (isValueProvider(registration.provider)) { | ||
return registration.provider.useValue; | ||
resolved = registration.provider.useValue; | ||
} | ||
else if (isTokenProvider(registration.provider)) { | ||
return registration.options.singleton | ||
resolved = returnInstance | ||
? registration.instance || | ||
(registration.instance = this.resolve(registration.provider.useToken)) | ||
: this.resolve(registration.provider.useToken); | ||
(registration.instance = this.resolve(registration.provider.useToken, context)) | ||
: this.resolve(registration.provider.useToken, context); | ||
} | ||
else if (isClassProvider(registration.provider)) { | ||
return registration.options.singleton | ||
resolved = returnInstance | ||
? registration.instance || | ||
(registration.instance = this.construct(registration.provider.useClass)) | ||
: this.construct(registration.provider.useClass); | ||
(registration.instance = this.construct(registration.provider.useClass, context)) | ||
: this.construct(registration.provider.useClass, context); | ||
} | ||
else if (isFactoryProvider(registration.provider)) { | ||
return registration.provider.useFactory(this); | ||
resolved = registration.provider.useFactory(this); | ||
} | ||
else { | ||
return this.construct(registration.provider); | ||
resolved = this.construct(registration.provider, context); | ||
} | ||
if (registration.options.lifecycle === Lifecycle.ResolutionScoped) { | ||
context.scopedResolutions.set(registration, resolved); | ||
} | ||
return resolved; | ||
}; | ||
InternalDependencyContainer.prototype.resolveAll = function (token) { | ||
InternalDependencyContainer.prototype.resolveAll = function (token, context) { | ||
var _this = this; | ||
var registration = this.getAllRegistrations(token); | ||
if (!registration && isNormalToken(token)) { | ||
if (context === void 0) { context = new ResolutionContext(); } | ||
var registrations = this.getAllRegistrations(token); | ||
if (!registrations && isNormalToken(token)) { | ||
throw "Attempted to resolve unregistered dependency token: " + token.toString(); | ||
} | ||
if (registration) { | ||
return registration.map(function (item) { return _this.resolveRegistration(item); }); | ||
if (registrations) { | ||
return registrations.map(function (item) { | ||
return _this.resolveRegistration(item, context); | ||
}); | ||
} | ||
return [this.construct(token)]; | ||
return [this.construct(token, context)]; | ||
}; | ||
InternalDependencyContainer.prototype.isRegistered = function (token) { | ||
return this._registry.has(token); | ||
InternalDependencyContainer.prototype.isRegistered = function (token, recursive) { | ||
if (recursive === void 0) { recursive = false; } | ||
return (this._registry.has(token) || | ||
(recursive && | ||
(this.parent || false) && | ||
this.parent.isRegistered(token, true))); | ||
}; | ||
@@ -113,3 +146,31 @@ InternalDependencyContainer.prototype.reset = function () { | ||
InternalDependencyContainer.prototype.createChildContainer = function () { | ||
return new InternalDependencyContainer(this); | ||
var e_1, _a; | ||
var childContainer = new InternalDependencyContainer(this); | ||
try { | ||
for (var _b = __values(this._registry.entries()), _c = _b.next(); !_c.done; _c = _b.next()) { | ||
var _d = __read(_c.value, 2), token = _d[0], registrations = _d[1]; | ||
if (registrations.some(function (_a) { | ||
var options = _a.options; | ||
return options.lifecycle === Lifecycle.ContainerScoped; | ||
})) { | ||
childContainer._registry.setAll(token, registrations.map(function (registration) { | ||
if (registration.options.lifecycle === Lifecycle.ContainerScoped) { | ||
return { | ||
provider: registration.provider, | ||
options: registration.options | ||
}; | ||
} | ||
return registration; | ||
})); | ||
} | ||
} | ||
} | ||
catch (e_1_1) { e_1 = { error: e_1_1 }; } | ||
finally { | ||
try { | ||
if (_c && !_c.done && (_a = _b.return)) _a.call(_b); | ||
} | ||
finally { if (e_1) throw e_1.error; } | ||
} | ||
return childContainer; | ||
}; | ||
@@ -134,3 +195,3 @@ InternalDependencyContainer.prototype.getRegistration = function (token) { | ||
}; | ||
InternalDependencyContainer.prototype.construct = function (ctor) { | ||
InternalDependencyContainer.prototype.construct = function (ctor, context) { | ||
var _this = this; | ||
@@ -148,7 +209,7 @@ if (ctor.length === 0) { | ||
? _this.resolveAll(param.token) | ||
: _this.resolve(param.token); | ||
: _this.resolve(param.token, context); | ||
} | ||
return _this.resolve(param); | ||
return _this.resolve(param, context); | ||
}); | ||
return new (ctor.bind.apply(ctor, [void 0].concat(params)))(); | ||
return new (ctor.bind.apply(ctor, __spread([void 0], params)))(); | ||
}; | ||
@@ -155,0 +216,0 @@ return InternalDependencyContainer; |
@@ -5,2 +5,5 @@ var Registry = (function () { | ||
} | ||
Registry.prototype.entries = function () { | ||
return this._registryMap.entries(); | ||
}; | ||
Registry.prototype.getAll = function (key) { | ||
@@ -19,2 +22,5 @@ this.ensure(key); | ||
}; | ||
Registry.prototype.setAll = function (key, value) { | ||
this._registryMap.set(key, value); | ||
}; | ||
Registry.prototype.has = function (key) { | ||
@@ -21,0 +27,0 @@ this.ensure(key); |
@@ -7,1 +7,2 @@ export { default as autoInjectable } from "./auto-injectable"; | ||
export { default as injectAll } from "./inject-all"; | ||
export { default as scoped } from "./scoped"; |
@@ -5,3 +5,4 @@ import ClassProvider from "./class-provider"; | ||
import FactoryProvider from "./factory-provider"; | ||
declare type Provider<T> = ClassProvider<T> | ValueProvider<T> | TokenProvider<T> | FactoryProvider<T>; | ||
declare type Provider<T = any> = ClassProvider<T> | ValueProvider<T> | TokenProvider<T> | FactoryProvider<T>; | ||
export declare function isProvider(provider: any): provider is Provider; | ||
export default Provider; |
@@ -5,5 +5,7 @@ import { InjectionToken } from "."; | ||
protected _registryMap: Map<InjectionToken<any>, Registration<any>[]>; | ||
entries(): IterableIterator<[InjectionToken<any>, Registration[]]>; | ||
getAll(key: InjectionToken<any>): Registration[]; | ||
get(key: InjectionToken<any>): Registration | null; | ||
set(key: InjectionToken<any>, value: Registration): void; | ||
setAll(key: InjectionToken<any>, value: Registration[]): void; | ||
has(key: InjectionToken<any>): boolean; | ||
@@ -10,0 +12,0 @@ clear(): void; |
@@ -13,2 +13,3 @@ import FactoryProvider from "../providers/factory-provider"; | ||
register<T>(token: InjectionToken<T>, provider: ClassProvider<T>, options?: RegistrationOptions): DependencyContainer; | ||
register<T>(token: InjectionToken<T>, provider: constructor<T>, options?: RegistrationOptions): DependencyContainer; | ||
registerSingleton<T>(from: InjectionToken<T>, to: InjectionToken<T>): DependencyContainer; | ||
@@ -18,7 +19,23 @@ registerSingleton<T>(token: constructor<T>): DependencyContainer; | ||
registerInstance<T>(token: InjectionToken<T>, instance: T): DependencyContainer; | ||
/** | ||
* Resolve a token into an instance | ||
* | ||
* @param token The dependency token | ||
* @return An instance of the dependency | ||
*/ | ||
resolve<T>(token: InjectionToken<T>): T; | ||
resolveAll<T>(token: InjectionToken<T>): T[]; | ||
isRegistered<T>(token: InjectionToken<T>): boolean; | ||
/** | ||
* Check if the given dependency is registered | ||
* | ||
* @param token The token to check | ||
* @param recursive Should parent containers be checked? | ||
* @return Whether or not the token is registered | ||
*/ | ||
isRegistered<T>(token: InjectionToken<T>, recursive?: boolean): boolean; | ||
/** | ||
* Clears all registered tokens | ||
*/ | ||
reset(): void; | ||
createChildContainer(): DependencyContainer; | ||
} |
@@ -0,4 +1,5 @@ | ||
import Lifecycle from "./lifecycle"; | ||
declare type RegistrationOptions = { | ||
singleton: boolean; | ||
lifecycle: Lifecycle; | ||
}; | ||
export default RegistrationOptions; |
{ | ||
"name": "tsyringe", | ||
"version": "3.4.0", | ||
"version": "4.0.0", | ||
"description": "Lightweight dependency injection container for JavaScript/TypeScript", | ||
@@ -10,3 +10,3 @@ "main": "dist/cjs/index.js", | ||
"scripts": { | ||
"build": "npm run clean && npm run build:cjs && npm run build:es5 && npm run build:es2015 && npm run build:types", | ||
"build": "yarn clean && yarn build:cjs && yarn build:es5 && yarn build:es2015 && yarn build:types", | ||
"build:cjs": "tsc", | ||
@@ -17,5 +17,7 @@ "build:es5": "tsc -p ./typescript/tsconfig.esm5.json", | ||
"clean": "rimraf ./dist", | ||
"test": "npm run lint && jest --config test/jest.config.js", | ||
"test": "yarn lint && jest --config test/jest.config.js", | ||
"test:inspect": "yarn lint && node --inspect-brk ./node_modules/jest/bin/jest.js --runInBand --config test/jest.config.js", | ||
"test:coverage": "jest --config test/jest.config.js --coverage", | ||
"lint": "prettier --check \"src/**/*.ts\" && eslint \"**/*.ts\"" | ||
"lint": "eslint --ext \".js,.jsx,.ts,.tsx\" \"./src\"", | ||
"lint:fix": "eslint --fix --ext \".js,.jsx,.ts,.tsx\" \"./src\"" | ||
}, | ||
@@ -50,9 +52,10 @@ "repository": { | ||
"@types/node": "^8.10.16", | ||
"@typescript-eslint/eslint-plugin": "^1.6.0", | ||
"@typescript-eslint/parser": "^1.11.0", | ||
"eslint": "^5.16.0", | ||
"eslint-config-prettier": "^4.1.0", | ||
"@typescript-eslint/eslint-plugin": "^2.5.0", | ||
"@typescript-eslint/parser": "^2.5.0", | ||
"eslint": "^6.5.1", | ||
"eslint-config-prettier": "^6.4.0", | ||
"eslint-plugin-prettier": "^3.1.1", | ||
"husky": "^3.0.0", | ||
"jest": "^24.7.1", | ||
"prettier": "1.17.0", | ||
"prettier": "1.18.2", | ||
"reflect-metadata": "^0.1.12", | ||
@@ -63,2 +66,2 @@ "rimraf": "^2.6.2", | ||
} | ||
} | ||
} |
@@ -10,3 +10,3 @@ [![Travis](https://img.shields.io/travis/Microsoft/tsyringe.svg)](https://travis-ci.org/Microsoft/tsyringe/) | ||
<!-- TOC depthFrom:1 depthTo:2 --> | ||
<!-- TOC depthFrom:1 depthTo:3 --> | ||
@@ -17,4 +17,15 @@ - [TSyringe](#tsyringe) | ||
- [Decorators](#decorators) | ||
- [injectable()](#injectable) | ||
- [singleton()](#singleton) | ||
- [autoInjectable()](#autoinjectable) | ||
- [inject()](#inject) | ||
- [injectAll()](#injectall) | ||
- [scoped()](#scoped) | ||
- [Container](#container) | ||
- [Manual resolution](#manual-resolution) | ||
- [Injection Token](#injection-token) | ||
- [Providers](#providers) | ||
- [Register](#register) | ||
- [Registry](#registry) | ||
- [Resolution](#resolution) | ||
- [Child Containers](#child-containers) | ||
- [Full examples](#full-examples) | ||
@@ -188,2 +199,22 @@ - [Example without interfaces](#example-without-interfaces) | ||
### scoped() | ||
Class decorator factory that registers the class as a scoped dependency within the global container. | ||
#### Available scopes | ||
- ResolutionScoped | ||
- The same instance will be resolved for each resolution of this dependency during a single | ||
resolution chain | ||
- ContainerScoped | ||
- The dependency container will return the same instance each time a resolution for this dependency | ||
is requested. This is similar to being a singleton, however if a child container is made, that child | ||
container will resolve an instance unique to it. | ||
#### Usage | ||
```typescript | ||
@scoped(Lifecycle.ContainerScoped) | ||
class Foo {} | ||
``` | ||
## Container | ||
@@ -190,0 +221,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
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
80562
123
1593
477
0
14