Socket
Socket
Sign inDemoInstall

@mediaclip/dependency-injection

Package Overview
Dependencies
Maintainers
4
Versions
7
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@mediaclip/dependency-injection - npm Package Compare versions

Comparing version 1.0.1 to 1.0.2

32

dist/index.d.ts

@@ -11,16 +11,2 @@ type Prettify<T> = T extends infer U ? {

interface IScopedDiContainer extends IDiContainer {
registerScoped<T>(token: DiToken<T>, valueOrFactory: ((container: IDiContainer) => T) | T): void;
}
declare class ScopedDiContainer extends DiContainer {
private readonly parentContainer;
private scopedInstances;
constructor(parentContainer: IDiContainer);
registerScoped<T>(token: DiToken<T>, valueOrFactory: ((container: IScopedDiContainer) => T) | T): void;
isScopedRegistered<T>(token: DiToken<T>): boolean;
tryResolve<T>(token: DiToken<T>): T | undefined;
createScope(): IScopedDiContainer;
private saveScopeInstances;
}
type InstanceFactory = (container: IDiContainer) => any;

@@ -37,2 +23,6 @@ type TypedInstanceFactory<T> = (container: IDiContainer) => T;

}
interface IScopedDiContainer extends IDiContainer {
registerScoped<T>(token: DiToken<T>, valueOrFactory: ((container: IDiContainer) => T) | T): void;
}
type DiContainerOption = {

@@ -48,3 +38,3 @@ overridesBehaviour: "use-last" | "use-first" | "deny";

registerSingleton<T>(token: DiToken<T>, valueOrFactory: ((container: IDiContainer) => T) | T): void;
registerScoped<T>(token: DiToken<T>, factory: (container: IScopedDiContainer) => T): void;
registerScoped<T>(token: DiToken<T>, factory: TypedInstanceFactory<T>): void;
isRegistered<T>(token: DiToken<T>): boolean;

@@ -62,5 +52,15 @@ isSingletonRegistered<T>(token: DiToken<T>): boolean;

}
declare class ScopedDiContainer extends DiContainer {
private readonly parentContainer;
private scopedInstances;
constructor(parentContainer: IDiContainer);
registerScoped<T>(token: DiToken<T>, valueOrFactory: TypedInstanceFactory<T> | T): void;
isScopedRegistered<T>(token: DiToken<T>): boolean;
tryResolve<T>(token: DiToken<T>): T | undefined;
createScope(): IScopedDiContainer;
private saveScopeInstances;
}
declare function isFunction<T>(valueOrFactory: T | ((container: IDiContainer) => T)): valueOrFactory is (container: IDiContainer) => T;
export { DiContainer, type DiContainerOption, DiToken, type IDiContainer, type IScopedDiContainer, type Prettify, ScopedDiContainer, isFunction };
export { DiContainer, type DiContainerOption, DiToken, type IDiContainer, type IScopedDiContainer, type InstanceFactory, type Prettify, ScopedDiContainer, type TypedInstanceFactory, isFunction };

@@ -6,51 +6,2 @@ // src/utils.ts

// src/scoped-di-container.ts
var ScopedDiContainer = class _ScopedDiContainer extends DiContainer {
constructor(parentContainer) {
super();
this.parentContainer = parentContainer;
this.scopedInstances = /* @__PURE__ */ new Map();
}
registerScoped(token, valueOrFactory) {
if (isFunction(valueOrFactory))
super.registerScoped(token, valueOrFactory);
else {
this.ensureCanBeRegistered(token, "scoped");
this.saveScopeInstances(token, valueOrFactory);
}
}
isScopedRegistered(token) {
return super.isScopedRegistered(token) || this.scopedInstances.has(token.symbol);
}
tryResolve(token) {
const instance = this.tryResolveFrom(token, this.scopedInstances);
if (instance)
return instance;
const newInstance = this.tryResolveFromFactory(token, this.scopedFactories);
if (newInstance)
return this.saveScopeInstances(token, newInstance);
const parentScopedFactory = this.parentContainer.tryResolveScopedFactory(token);
if (parentScopedFactory) {
return this.saveScopeInstances(token, parentScopedFactory(this));
}
const singletonInstance = super.tryResolve(token);
if (singletonInstance) {
return singletonInstance;
}
return this.parentContainer.tryResolve(token);
}
createScope() {
return new _ScopedDiContainer(this);
}
saveScopeInstances(token, value) {
let scopedInstances = this.scopedInstances.get(token.symbol);
if (scopedInstances === void 0) {
scopedInstances = [];
this.scopedInstances.set(token.symbol, scopedInstances);
}
scopedInstances.push(value);
return value;
}
};
// src/di-container.ts

@@ -163,2 +114,49 @@ var DiContainer = class {

};
var ScopedDiContainer = class _ScopedDiContainer extends DiContainer {
constructor(parentContainer) {
super();
this.parentContainer = parentContainer;
this.scopedInstances = /* @__PURE__ */ new Map();
}
registerScoped(token, valueOrFactory) {
if (isFunction(valueOrFactory))
super.registerScoped(token, valueOrFactory);
else {
this.ensureCanBeRegistered(token, "scoped");
this.saveScopeInstances(token, valueOrFactory);
}
}
isScopedRegistered(token) {
return super.isScopedRegistered(token) || this.scopedInstances.has(token.symbol);
}
tryResolve(token) {
const instance = this.tryResolveFrom(token, this.scopedInstances);
if (instance)
return instance;
const newInstance = this.tryResolveFromFactory(token, this.scopedFactories);
if (newInstance)
return this.saveScopeInstances(token, newInstance);
const parentScopedFactory = this.parentContainer.tryResolveScopedFactory(token);
if (parentScopedFactory) {
return this.saveScopeInstances(token, parentScopedFactory(this));
}
const singletonInstance = super.tryResolve(token);
if (singletonInstance) {
return singletonInstance;
}
return this.parentContainer.tryResolve(token);
}
createScope() {
return new _ScopedDiContainer(this);
}
saveScopeInstances(token, value) {
let scopedInstances = this.scopedInstances.get(token.symbol);
if (scopedInstances === void 0) {
scopedInstances = [];
this.scopedInstances.set(token.symbol, scopedInstances);
}
scopedInstances.push(value);
return value;
}
};

@@ -165,0 +163,0 @@ // src/token.ts

@@ -7,3 +7,3 @@ {

"name": "@mediaclip/dependency-injection",
"version": "1.0.1",
"version": "1.0.2",
"main": "./dist/index.js",

@@ -10,0 +10,0 @@ "keywords": [

@@ -50,2 +50,9 @@ # Dependency injection

Example: See [sample](samples/src/scoped.ts)
## Module patterns
To avoid having a big file with the configuration of all the DI, it's preferable to split this into multiple files, kind of 1 module / folder or per things that go together.
Example: See [sample](samples/src/module.ts)

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

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc