Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@tobes31415/dependency-injection

Package Overview
Dependencies
Maintainers
1
Versions
5
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@tobes31415/dependency-injection - npm Package Compare versions

Comparing version 2.0.0 to 3.0.0

29

lib/di-cjs.js

@@ -14,3 +14,3 @@ "use strict";

class Service {
stuff() {}
stuff() {}
}

@@ -87,3 +87,28 @@

}());
var root = new DIContextImplementation();
var DIAdaptiveRootImplementation = /** @class */ (function () {
function DIAdaptiveRootImplementation() {
this.defaultRoot = new DIContextImplementation();
}
DIAdaptiveRootImplementation.prototype.useFactory = function (token, factoryFn) {
return this.context().useFactory(token, factoryFn);
};
DIAdaptiveRootImplementation.prototype.useClass = function (token, substitute) {
return this.context().useClass(token, substitute);
};
;
DIAdaptiveRootImplementation.prototype.useObject = function (token, substitute) {
return this.context().useObject(token, substitute);
};
;
DIAdaptiveRootImplementation.prototype.resolve = function (token) {
return this.context().resolve(token);
};
;
DIAdaptiveRootImplementation.prototype.context = function () {
return currentContext || this.defaultRoot;
};
;
return DIAdaptiveRootImplementation;
}());
var root = new DIAdaptiveRootImplementation();
exports.DI = root;

@@ -90,0 +115,0 @@ /**

2

lib/di-cjs.min.js

@@ -1,1 +0,1 @@

"use strict";var e;exports.__esModule=!0;var t=function(){function t(){this.instances=new Map,this.resolvers=new Map,this.useFactory=this.useFactory.bind(this),this.resolve=this.resolve.bind(this)}return t.prototype.useFactory=function(e,t){if(this.instances.has(e))throw new Error("This token has already been resolved");if(this.resolvers.has(e))throw new Error("A resolver has already been supplied for this token");if("function"!=typeof t)throw new Error("useFactory expects a function");this.resolvers.set(e,t)},t.prototype.useClass=function(e,t){var r=this;this.useFactory(e,function(){return r.resolve(t)})},t.prototype.useObject=function(e,t){this.useFactory(e,function(){return t})},t.prototype.resolve=function(t){var r=!e;try{if(r&&(e=this),e!==this)throw new Error("Cross Context resolution detected");if(!this.instances.has(t)){var s=(this.resolvers.get(t)||this.defaultResolver)(t);if(!s)throw new Error("Resolver didn't return an instance");this.instances.set(t,s)}return this.instances.get(t)}finally{r&&(e=void 0)}},t.prototype.defaultResolver=function(e){if("function"!=typeof e)throw new Error("Tokens must either be factory functions, or you must supply a resolver before trying to resolve them");return new e},t}(),r=new t;exports.DI=r,exports.createRoot=function(){return new t},exports.default=r;
"use strict";var t;exports.__esModule=!0;var e=function(){function e(){this.instances=new Map,this.resolvers=new Map,this.useFactory=this.useFactory.bind(this),this.resolve=this.resolve.bind(this)}return e.prototype.useFactory=function(t,e){if(this.instances.has(t))throw new Error("This token has already been resolved");if(this.resolvers.has(t))throw new Error("A resolver has already been supplied for this token");if("function"!=typeof e)throw new Error("useFactory expects a function");this.resolvers.set(t,e)},e.prototype.useClass=function(t,e){var r=this;this.useFactory(t,function(){return r.resolve(e)})},e.prototype.useObject=function(t,e){this.useFactory(t,function(){return e})},e.prototype.resolve=function(e){var r=!t;try{if(r&&(t=this),t!==this)throw new Error("Cross Context resolution detected");if(!this.instances.has(e)){var o=(this.resolvers.get(e)||this.defaultResolver)(e);if(!o)throw new Error("Resolver didn't return an instance");this.instances.set(e,o)}return this.instances.get(e)}finally{r&&(t=void 0)}},e.prototype.defaultResolver=function(t){if("function"!=typeof t)throw new Error("Tokens must either be factory functions, or you must supply a resolver before trying to resolve them");return new t},e}(),r=new(function(){function r(){this.defaultRoot=new e}return r.prototype.useFactory=function(t,e){return this.context().useFactory(t,e)},r.prototype.useClass=function(t,e){return this.context().useClass(t,e)},r.prototype.useObject=function(t,e){return this.context().useObject(t,e)},r.prototype.resolve=function(t){return this.context().resolve(t)},r.prototype.context=function(){return t||this.defaultRoot},r}());exports.DI=r,exports.createRoot=function(){return new e},exports.default=r;

@@ -13,3 +13,3 @@ /**

class Service {
stuff() {}
stuff() {}
}

@@ -16,0 +16,0 @@

@@ -13,3 +13,3 @@ /**

class Service {
stuff() {}
stuff() {}
}

@@ -83,3 +83,27 @@

}
const root = new DIContextImplementation();
class DIAdaptiveRootImplementation {
constructor() {
this.defaultRoot = new DIContextImplementation();
}
useFactory(token, factoryFn) {
return this.context().useFactory(token, factoryFn);
}
useClass(token, substitute) {
return this.context().useClass(token, substitute);
}
;
useObject(token, substitute) {
return this.context().useObject(token, substitute);
}
;
resolve(token) {
return this.context().resolve(token);
}
;
context() {
return currentContext || this.defaultRoot;
}
;
}
const root = new DIAdaptiveRootImplementation();
/**

@@ -86,0 +110,0 @@ * Creates a new dependency injection root with no resolved instances. Use for unit testing to provide mocks

@@ -1,1 +0,1 @@

let e;class s{constructor(){this.instances=new Map,this.resolvers=new Map,this.useFactory=this.useFactory.bind(this),this.resolve=this.resolve.bind(this)}useFactory(e,s){if(this.instances.has(e))throw new Error("This token has already been resolved");if(this.resolvers.has(e))throw new Error("A resolver has already been supplied for this token");if("function"!=typeof s)throw new Error("useFactory expects a function");this.resolvers.set(e,s)}useClass(e,s){this.useFactory(e,()=>this.resolve(s))}useObject(e,s){this.useFactory(e,()=>s)}resolve(s){const t=!e;try{if(t&&(e=this),e!==this)throw new Error("Cross Context resolution detected");if(!this.instances.has(s)){const e=(this.resolvers.get(s)||this.defaultResolver)(s);if(!e)throw new Error("Resolver didn't return an instance");this.instances.set(s,e)}return this.instances.get(s)}finally{t&&(e=void 0)}}defaultResolver(e){if("function"!=typeof e)throw new Error("Tokens must either be factory functions, or you must supply a resolver before trying to resolve them");return new e}}const t=new s;export function createRoot(){return new s};export default t;export{t as DI};
let e;class t{constructor(){this.instances=new Map,this.resolvers=new Map,this.useFactory=this.useFactory.bind(this),this.resolve=this.resolve.bind(this)}useFactory(e,t){if(this.instances.has(e))throw new Error("This token has already been resolved");if(this.resolvers.has(e))throw new Error("A resolver has already been supplied for this token");if("function"!=typeof t)throw new Error("useFactory expects a function");this.resolvers.set(e,t)}useClass(e,t){this.useFactory(e,()=>this.resolve(t))}useObject(e,t){this.useFactory(e,()=>t)}resolve(t){const s=!e;try{if(s&&(e=this),e!==this)throw new Error("Cross Context resolution detected");if(!this.instances.has(t)){const e=(this.resolvers.get(t)||this.defaultResolver)(t);if(!e)throw new Error("Resolver didn't return an instance");this.instances.set(t,e)}return this.instances.get(t)}finally{s&&(e=void 0)}}defaultResolver(e){if("function"!=typeof e)throw new Error("Tokens must either be factory functions, or you must supply a resolver before trying to resolve them");return new e}}const s=new class{constructor(){this.defaultRoot=new t}useFactory(e,t){return this.context().useFactory(e,t)}useClass(e,t){return this.context().useClass(e,t)}useObject(e,t){return this.context().useObject(e,t)}resolve(e){return this.context().resolve(e)}context(){return e||this.defaultRoot}};export function createRoot(){return new t};export default s;export{s as DI};
{
"name": "@tobes31415/dependency-injection",
"version": "2.0.0",
"version": "3.0.0",
"description": "Tiny dependency injection",

@@ -5,0 +5,0 @@ "main": "lib/di-cjs.min.js",

import { createRoot, DIContext } from "./di";
import root from "./di";
describe("DI", () => {
let DI: DIContext;
beforeEach(() => {

@@ -11,13 +12,13 @@ DI = createRoot();

test("it creates an instance of a function", () => {
const foo = function() {};
const foo = function () { };
const instance = DI.resolve(foo);
expect (instance.constructor).toBe(foo);
expect(instance.constructor).toBe(foo);
});
test("it handles nested dependencies", () => {
const Inside = function() { }
const Outside = function() {
const Inside = function () { }
const Outside = function () {
const inside = DI.resolve(Inside);
expect (inside.constructor).toBe(Inside);
expect(inside.constructor).toBe(Inside);
}

@@ -29,12 +30,12 @@ const outside = DI.resolve(Outside);

test("token resolver can be overridden wtih a factory function ", () => {
const A = function() {};
const A = function () { };
const B = {};
DI.useFactory(A, () => B);
const instance = DI.resolve(A);
expect (instance).toBe(B);
expect(instance).toBe(B);
})
test("token resolver can be overridden with another token", () => {
const A = function () {};
const B = function () {};
const A = function () { };
const B = function () { };
DI.useClass(A, B);

@@ -46,3 +47,3 @@ const instance = DI.resolve(A);

test("token resolver can be overridden with an instance object", () => {
const A = function () {};
const A = function () { };
const B = {};

@@ -64,3 +65,26 @@ DI.useObject(A, B);

expect(o2).toBe(o3);
})
});
describe("Root", () => {
test("Chooses the correct context when invoked globally", () => {
const primaryToken = {};
const secondaryToken = {};
const A = () => 123;
const B = () => 456;
const test = () => root.resolve(secondaryToken);
root.useFactory(primaryToken, test);
root.useFactory(secondaryToken, A);
const context = createRoot();
context.useFactory(primaryToken, test);
context.useFactory(secondaryToken, B);
const o1 = root.resolve(primaryToken);
const o2 = context.resolve(primaryToken);
expect(o1).toBe(123);
expect(o2).toBe(456);
});
});
});

@@ -13,3 +13,3 @@ /**

class Service {
stuff() {}
stuff() {}
}

@@ -40,3 +40,3 @@

*/
useFactory(token:any, factory:()=>any): void;
useFactory(token: any, factory: () => any): void;

@@ -48,3 +48,3 @@ /**

*/
useClass(token:any, substitute:any): void;
useClass(token: any, substitute: any): void;

@@ -56,8 +56,8 @@ /**

*/
useObject(token:any, substitute: any): void;
useObject(token: any, substitute: any): void;
}
class DIContextImplementation implements DIContext {
instances: Map<any,any> = new Map();
resolvers: Map<any,(v)=>any> = new Map();
instances: Map<any, any> = new Map();
resolvers: Map<any, (v) => any> = new Map();

@@ -81,7 +81,7 @@ constructor() {

}
useClass(token:any, substitute:any) {
this.useFactory(token, ()=> this.resolve(substitute));
useClass(token: any, substitute: any) {
this.useFactory(token, () => this.resolve(substitute));
};
useObject(token:any, substitute: any) {
this.useFactory(token, ()=> substitute);
useObject(token: any, substitute: any) {
this.useFactory(token, () => substitute);
};

@@ -124,4 +124,26 @@

const root: DIContext = new DIContextImplementation();
class DIAdaptiveRootImplementation implements DIContext {
private defaultRoot = new DIContextImplementation();
useFactory(token, factoryFn) {
return this.context().useFactory(token, factoryFn);
}
useClass(token: any, substitute: any) {
return this.context().useClass(token, substitute);
};
useObject(token: any, substitute: any) {
return this.context().useObject(token, substitute);
};
resolve(token) {
return this.context().resolve(token);
};
private context() {
return currentContext || this.defaultRoot;
};
}
const root: DIContext = new DIAdaptiveRootImplementation();
/**

@@ -128,0 +150,0 @@ * Creates a new dependency injection root with no resolved instances. Use for unit testing to provide mocks

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