Socket
Socket
Sign inDemoInstall

@owja/ioc

Package Overview
Dependencies
0
Maintainers
1
Versions
17
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

Comparing version 2.0.0-alpha.5 to 2.0.0-alpha.6

dist/ioc/utils.d.ts

2

dist/ioc.js

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

class t{constructor(t){this.t=void 0,this.t=t}withPlugin(t){return this.t.plugins.push(t),this}}class n extends t{inSingletonScope(){return this.t.singleton=!0,this}}class i{constructor(t){this.t=void 0,this.t=t}to(t){return this.t.injected=function(){return new t(...[].slice.call(arguments))},new n(this.t)}toFactory(t){return this.t.injected=t,new n(this.t)}toValue(n){if(void 0===n)throw"cannot bind a value of type undefined";return this.t.injected=n,new t(this.t)}}const o=t=>"symbol"!=typeof t,e=t=>o(t)?`Token(${t.type.toString()})`:t.toString(),r=t=>o(t)?t.type:t,s=Symbol("NOCACHE"),u=Symbol("NOPLUGINS");function h(t,n,i,o,e){var r=[].slice.call(arguments,5);Object.defineProperty(t,n,{get:function(){const t=i.get(o,e,this,r);return-1===e.indexOf(s)&&Object.defineProperty(this,n,{value:t,enumerable:!0}),t},configurable:!0,enumerable:!0})}exports.Container=class{constructor(){this.i=new Map,this.o=[],this.u=[]}bind(t){return new i(this.h(t))}rebind(t){return this.remove(t).bind(t)}remove(t){if(void 0===this.i.get(r(t)))throw`${e(t)} was never bound`;return this.i.delete(r(t)),this}get(t,n,i,o){void 0===n&&(n=[]),void 0===o&&(o=[]);const s=this.i.get(r(t));if(void 0===s||void 0===s.injected)throw`nothing bound to ${e(t)}`;const h="function"==typeof s.injected?s.singleton?s.cache=s.cache||s.injected():s.injected(...o):s.injected;return-1===n.indexOf(u)&&s.plugins.concat(this.u).forEach(o=>{o(h,i,n,t,this)}),h}addPlugin(t){return this.u.push(t),this}snapshot(){return this.o.push(new Map(this.i)),this}restore(){return this.i=this.o.pop()||this.i,this}h(t){if(void 0!==this.i.get(r(t)))throw`object can only bound once: ${e(t)}`;const n={plugins:[]};return this.i.set(r(t),n),n}},exports.NOCACHE=s,exports.NOPLUGINS=u,exports.createDecorator=function(t){return function(n,i){void 0===i&&(i=[]);var o=[].slice.call(arguments,2);return function(e,r){h(e,r,t,n,i,...o)}}},exports.createResolve=function(t){return function(n,i){let o;return void 0===i&&(i=[]),function(){return-1===i.indexOf(s)&&void 0!==o||(o=t.get(n,i,this,[].slice.call(arguments))),o}}},exports.createWire=function(t){return function(n,i,o,e){void 0===e&&(e=[]),h(n,i,t,o,e,...[].slice.call(arguments,4))}},exports.token=t=>({type:Symbol(t)});
class t{constructor(t){this.t=void 0,this.t=t}withPlugin(t){return this.t.plugins.push(t),this}}class n extends t{inSingletonScope(){return this.t.singleton=!0,this}}class i{constructor(t){this.t=void 0,this.t=t}to(t){return this.t.injected=function(){return new t(...[].slice.call(arguments))},new n(this.t)}toFactory(t){return this.t.injected=t,new n(this.t)}toValue(n){if(void 0===n)throw"cannot bind a value of type undefined";return this.t.injected=n,new t(this.t)}}const o=t=>"symbol"==typeof t,e=t=>o(t)?[t]:t,r=t=>o(t)?t.toString():`Token(${t.type.toString()})`,s=t=>o(t)?t:t.type,u=Symbol("NOCACHE"),h=Symbol("NOPLUGINS");function c(t,n,i,o,r){var s=[].slice.call(arguments,5);Object.defineProperty(t,n,{get:function(){const t=i.get(o,r,this,s);return-1===e(r).indexOf(u)&&Object.defineProperty(this,n,{value:t,enumerable:!0}),t},configurable:!0,enumerable:!0})}exports.Container=class{constructor(){this.i=new Map,this.o=[],this.u=[]}bind(t){return new i(this.h(t))}rebind(t){return this.remove(t).bind(t)}remove(t){if(void 0===this.i.get(s(t)))throw`${r(t)} was never bound`;return this.i.delete(s(t)),this}get(t,n,i,o){void 0===n&&(n=[]),void 0===o&&(o=[]);const u=this.i.get(s(t));if(void 0===u||void 0===u.injected)throw`nothing bound to ${r(t)}`;const c="function"==typeof u.injected?u.singleton?u.cache=u.cache||u.injected():u.injected(...o):u.injected,d=e(n);return-1===d.indexOf(h)&&u.plugins.concat(this.u).forEach(n=>{n(c,i,d,t,this)}),c}addPlugin(t){return this.u.push(t),this}snapshot(){return this.o.push(new Map(this.i)),this}restore(){return this.i=this.o.pop()||this.i,this}h(t){if(void 0!==this.i.get(s(t)))throw`object can only bound once: ${r(t)}`;const n={plugins:[]};return this.i.set(s(t),n),n}},exports.NOCACHE=u,exports.NOPLUGINS=h,exports.createDecorator=function(t){return function(n,i){void 0===i&&(i=[]);var o=[].slice.call(arguments,2);return function(e,r){c(e,r,t,n,i,...o)}}},exports.createResolve=function(t){return function(n,i){let o;return void 0===i&&(i=[]),function(){return-1===e(i).indexOf(u)&&void 0!==o||(o=t.get(n,i,this,[].slice.call(arguments))),o}}},exports.createWire=function(t){return function(n,i,o,e){void 0===e&&(e=[]),c(n,i,t,o,e,...[].slice.call(arguments,4))}},exports.token=t=>({type:Symbol(t)});
//# sourceMappingURL=ioc.js.map

@@ -10,3 +10,3 @@ import type { Token, MaybeToken, Plugin } from "./types";

remove(token: MaybeToken): Container;
get<T, U extends Array<unknown> = never>(token: Token<T, U> | MaybeToken<T>, tags?: symbol[], target?: unknown, injectedArgs?: Array<unknown>): T;
get<T, U extends Array<unknown> = never>(token: Token<T, U> | MaybeToken<T>, tags?: symbol[] | symbol, target?: unknown, injectedArgs?: Array<unknown>): T;
addPlugin(plugin: Plugin): Container;

@@ -13,0 +13,0 @@ snapshot(): Container;

import type { Token, MaybeToken } from "./types";
import type { Container } from "./container";
export declare function createDecorator(container: Container): <T, K extends unknown[]>(token: Token<T, K> | MaybeToken<T, unknown[]>, tags?: symbol[], ...injectedArgs: K) => <Target extends { [key in Prop]: T; }, Prop extends keyof Target>(target: Target, property: Prop) => void;
export declare function createDecorator(container: Container): <T, K extends unknown[]>(token: Token<T, K> | MaybeToken<T, unknown[]>, tags?: symbol[] | symbol, ...injectedArgs: K) => <Target extends { [key in Prop]: T; }, Prop extends keyof Target>(target: Target, property: Prop) => void;
import type { Token, MaybeToken } from "./types";
import type { Container } from "./container";
export declare function createResolve(container: Container): <T, U extends unknown[]>(token: Token<T, U> | MaybeToken<T, unknown[]>, tags?: symbol[]) => <R>(this: R, ...injectedArgs: U) => T;
export declare function createResolve(container: Container): <T, U extends unknown[]>(token: Token<T, U> | MaybeToken<T, unknown[]>, tags?: symbol[] | symbol) => <R>(this: R, ...injectedArgs: U) => T;
import type { Token, MaybeToken } from "./types";
import type { Container } from "./container";
export declare function createWire(container: Container): <Value, Target extends { [key in Prop]: Value; }, Prop extends keyof Target, K extends unknown[]>(target: Target, property: Prop, token: Token<Value, K> | MaybeToken<Value, unknown[]>, tags?: symbol[], ...injectedArgs: K) => void;
export declare function createWire(container: Container): <Value, Target extends { [key in Prop]: Value; }, Prop extends keyof Target, K extends unknown[]>(target: Target, property: Prop, token: Token<Value, K> | MaybeToken<Value, unknown[]>, tags?: symbol[] | symbol, ...injectedArgs: K) => void;

@@ -5,2 +5,2 @@ import type { Token, MaybeToken } from "./types";

[key in Prop]: T;
}, Prop extends keyof Target, K extends Array<unknown>>(target: Target, property: Prop, container: Container, token: Token<T, K> | MaybeToken<T>, tags: symbol[], ...injectedArgs: K): void;
}, Prop extends keyof Target, K extends Array<unknown>>(target: Target, property: Prop, container: Container, token: Token<T, K> | MaybeToken<T>, tags: symbol[] | symbol, ...injectedArgs: K): void;
{
"name": "@owja/ioc",
"version": "2.0.0-alpha.5",
"version": "2.0.0-alpha.6",
"description": "dependency injection for javascript",

@@ -5,0 +5,0 @@ "main": "dist/ioc.js",

@@ -14,3 +14,3 @@ # @owja/ioc

* Less Features but **straight forward**
* Can bind dependencies as **classes**, **factories** and **static values**
* Can bind dependencies as **classes**, **factories** and **static values** and provide dependencie arguments or parameters if needed
* Supports binding in **singleton scope**

@@ -54,4 +54,5 @@ * **Cached** - Resolves only once in each dependent class by default

This is the default way to bind a dependency. The class will get instantiated when the
dependency gets resolved.
dependency gets resolved. You will be able to pass down it's dependencie arguments once you resolve it.
```ts

@@ -76,2 +77,3 @@ container.bind<ServiceInterface>(symbol).to(Service);

container.bind<string>(symbol).toFactory(() => "just a string");
container.bind<string>(symbol).toFactory((a: string) => `I need a string parameter: ${a}`);
```

@@ -159,3 +161,3 @@

class Example {
@inject(symbol)
@inject(symbol/*, [tags], ...dependencie arguments*/)
readonly service!: Interface;

@@ -186,3 +188,3 @@

constructor() {
wire(this, "service", symbol);
wire(this, "service", symbol/*, [tags], ...dependencie arguments*/);
}

@@ -215,3 +217,3 @@

method() {
this.service().doSomething();
this.service(/*...dependencie arguments*/).doSomething();
}

@@ -224,3 +226,3 @@ }

const service = resolve<Interface>(symbol);
service().doSomething();
service(/*...dependencie arguments*/).doSomething();
}

@@ -277,3 +279,3 @@ ```

class Example {
@inject(TYPE.Service) // throws a type error because WrongInterface is not compatible with MyServiceInterface
@inject(TYPE.Service/*, [tags], ...dependencie arguments*/) // throws a type error because WrongInterface is not compatible with MyServiceInterface
readonly service!: WrongInterface;

@@ -287,3 +289,3 @@ }

class Example {
@inject(TYPE.Service)
@inject(TYPE.Service/*, [tags], ...dependencie arguments*/)
readonly service!: MyServiceInterface;

@@ -373,3 +375,3 @@ }

class Index extends Component {
@inject(TYPE.TranslationService, [SUBSCRIBE])
@inject(TYPE.TranslationService, [SUBSCRIBE]/*, ...dependencie arguments*/)
readonly service!: TranslatorInterface;

@@ -393,3 +395,3 @@

super();
wire(this, "service", TYPE.TranslationService, [SUBSCRIBE]);
wire(this, "service", TYPE.TranslationService, [SUBSCRIBE]/*, ...dependencie arguments*/);
}

@@ -401,3 +403,3 @@

class Index extends Component {
readonly service = resolve(TYPE.TranslationService, [SUBSCRIBE]);
readonly service = resolve(TYPE.TranslationService, [SUBSCRIBE]/*, ...dependencie arguments*/);

@@ -418,3 +420,3 @@ [...]

class Example {
@inject(TYPE.MyService, [NOPLUGINS])
@inject(TYPE.MyService, [NOPLUGINS]/*, ...dependencie arguments*/)
readonly service!: MyServiceInterface;

@@ -499,6 +501,6 @@ }

class Example {
@inject(TYPE.MyService)
@inject(TYPE.MyService/*, [tags], ...dependencie arguments*/)
readonly myService!: MyServiceInterface;
@inject(TYPE.MyOtherService)
@inject(TYPE.MyOtherService/*, [tags], ...dependencie arguments*/)
readonly myOtherService!: MyOtherServiceInterface;

@@ -527,3 +529,3 @@ }

@inject(TYPE.MyOtherSerice, NOCACHE)
@inject(TYPE.MyOtherSerice, NOCACHE/*, ...dependencie arguments*/)
readonly myOtherService!: MyOtherServiceInterface;

@@ -530,0 +532,0 @@ }

@@ -5,2 +5,3 @@ import type {Factory, Injected, Item, Token, MaybeToken, Plugin} from "./types";

import {NOPLUGINS} from "./tags";
import {valueOrArrayToArray} from "./utils";

@@ -32,3 +33,3 @@ const isFactory = <T>(i: Injected<T>): i is Factory<T> => typeof i === "function";

token: Token<T, U> | MaybeToken<T>,
tags: symbol[] = [],
tags: symbol[] | symbol = [],
target?: unknown,

@@ -47,5 +48,7 @@ injectedArgs: Array<unknown> = [],

if (tags.indexOf(NOPLUGINS) === -1)
const tagsArr = valueOrArrayToArray(tags);
if (tagsArr.indexOf(NOPLUGINS) === -1)
item.plugins.concat(this._plugins).forEach((plugin) => {
plugin(value, target, tags, token, this);
plugin(value, target, tagsArr, token, this);
});

@@ -52,0 +55,0 @@

@@ -8,3 +8,3 @@ import type {Token, MaybeToken} from "./types";

token: Token<T, K> | MaybeToken<T>,
tags: symbol[] = [],
tags: symbol[] | symbol = [],
...injectedArgs: K

@@ -11,0 +11,0 @@ ) => {

import type {Token, MaybeToken} from "./types";
import type {Container} from "./container";
import {NOCACHE} from "./tags";
import {valueOrArrayToArray} from "./utils";
export function createResolve(container: Container) {
return <T, U extends Array<unknown>>(token: Token<T, U> | MaybeToken<T>, tags: symbol[] = []) => {
return <T, U extends Array<unknown>>(token: Token<T, U> | MaybeToken<T>, tags: symbol[] | symbol = []) => {
let value: T;
return function <R>(this: R, ...injectedArgs: U): T {
if (tags.indexOf(NOCACHE) !== -1 || value === undefined) {
if (valueOrArrayToArray(tags).indexOf(NOCACHE) !== -1 || value === undefined) {
value = container.get(token, tags, this, injectedArgs);

@@ -11,0 +12,0 @@ }

@@ -10,3 +10,3 @@ import type {Token, MaybeToken} from "./types";

token: Token<Value, K> | MaybeToken<Value>,
tags: symbol[] = [],
tags: symbol[] | symbol = [],
...injectedArgs: K

@@ -13,0 +13,0 @@ ) => {

import type {Token, MaybeToken} from "./types";
import {Container} from "./container";
import {NOCACHE} from "./tags";
import {valueOrArrayToArray} from "./utils";

@@ -10,3 +11,3 @@ export function define<T, Target extends {[key in Prop]: T}, Prop extends keyof Target, K extends Array<unknown>>(

token: Token<T, K> | MaybeToken<T>,
tags: symbol[],
tags: symbol[] | symbol,
...injectedArgs: K

@@ -17,3 +18,3 @@ ) {

const value = container.get(token, tags, this, injectedArgs);
if (tags.indexOf(NOCACHE) === -1)
if (valueOrArrayToArray(tags).indexOf(NOCACHE) === -1)
Object.defineProperty(this, property, {

@@ -20,0 +21,0 @@ value,

import type {MaybeToken, Token} from "./types";
import {isSymbol} from "./utils";
export const token = <T, U extends Array<unknown> = unknown[]>(name: string) => ({type: Symbol(name)} as Token<T, U>);
const isToken = <T, U extends Array<unknown>>(token: MaybeToken<T, U>): token is Token<T, U> =>
typeof token != "symbol";
export const stringifyToken = (token: MaybeToken): string =>
isToken(token) ? `Token(${token.type.toString()})` : token.toString();
!isSymbol(token) ? `Token(${token.type.toString()})` : token.toString();
export const getType = (token: MaybeToken): symbol => (isToken(token) ? token.type : token);
export const getType = (token: MaybeToken): symbol => (!isSymbol(token) ? token.type : token);

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

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc