@owja/ioc
Advanced tools
Comparing version 2.0.0-alpha.4 to 2.0.0-alpha.5
@@ -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=()=>new t,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 e=t=>"symbol"!=typeof t,o=t=>e(t)?`Token(${t.type.toString()})`:t.toString(),r=t=>e(t)?t.type:t,s=Symbol("NOCACHE"),u=Symbol("NOPLUGINS");function h(t,n,i,e,o){Object.defineProperty(t,n,{get:function(){const t=i.get(e,o,this);return-1===o.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`${o(t)} was never bound`;return this.i.delete(r(t)),this}get(t,n,i){void 0===n&&(n=[]);const e=this.i.get(r(t));if(void 0===e||void 0===e.injected)throw`nothing bound to ${o(t)}`;const s="function"==typeof e.injected?e.singleton?e.cache=e.cache||e.injected():e.injected():e.injected;return-1===n.indexOf(u)&&e.plugins.concat(this.u).forEach(e=>{e(s,i,n,t,this)}),s}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: ${o(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){var i=[].slice.call(arguments,1);return function(e,o){h(e,o,t,n,i)}}},exports.createResolve=function(t){return function(n){var i=[].slice.call(arguments,1);let e;return function(){return-1===i.indexOf(s)&&void 0!==e||(e=t.get(n,i,this)),e}}},exports.createWire=function(t){return function(n,i,e){h(n,i,t,e,[].slice.call(arguments,3))}},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)?`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)}); | ||
//# sourceMappingURL=ioc.js.map |
import type { Item, NewAble, Factory, Value } from "./types"; | ||
import { Options } from "./options"; | ||
import { PluginOptions } from "./pluginOptions"; | ||
export declare class Bind<T> { | ||
export declare class Bind<T, U extends Array<unknown>> { | ||
private _target; | ||
constructor(_target: Item<T>); | ||
to(object: NewAble<T>): Options<T>; | ||
toFactory(factory: Factory<T>): Options<T>; | ||
to<O extends NewAble<T>>(object: O): Options<T>; | ||
toFactory(factory: Factory<T, U>): Options<T>; | ||
toValue(value: Value<T>): PluginOptions<T>; | ||
} |
@@ -1,2 +0,2 @@ | ||
import type { MaybeToken, Plugin } from "./types"; | ||
import type { Token, MaybeToken, Plugin } from "./types"; | ||
import { Bind } from "./bind"; | ||
@@ -7,6 +7,6 @@ export declare class Container { | ||
private _plugins; | ||
bind<T = never>(token: MaybeToken<T>): Bind<T>; | ||
rebind<T = never>(token: MaybeToken<T>): Bind<T>; | ||
bind<T = never, U extends Array<unknown> = never>(token: MaybeToken<T>): Bind<T, U>; | ||
rebind<T = never, U extends Array<unknown> = never>(token: MaybeToken<T>): Bind<T, U>; | ||
remove(token: MaybeToken): Container; | ||
get<T = never>(token: MaybeToken<T>, tags?: symbol[], target?: unknown): T; | ||
get<T, U extends Array<unknown> = never>(token: Token<T, U> | MaybeToken<T>, tags?: symbol[], target?: unknown, injectedArgs?: Array<unknown>): T; | ||
addPlugin(plugin: Plugin): Container; | ||
@@ -13,0 +13,0 @@ snapshot(): Container; |
@@ -1,3 +0,3 @@ | ||
import type { MaybeToken } from "./types"; | ||
import type { Token, MaybeToken } from "./types"; | ||
import type { Container } from "./container"; | ||
export declare function createDecorator(container: Container): <T>(token: MaybeToken<T>, ...tags: symbol[]) => <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[], ...injectedArgs: K) => <Target extends { [key in Prop]: T; }, Prop extends keyof Target>(target: Target, property: Prop) => void; |
@@ -1,3 +0,3 @@ | ||
import type { MaybeToken } from "./types"; | ||
import type { Token, MaybeToken } from "./types"; | ||
import type { Container } from "./container"; | ||
export declare function createResolve(container: Container): <T = never>(token: MaybeToken<T>, ...tags: symbol[]) => <R>(this: R) => T; | ||
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; |
@@ -1,3 +0,3 @@ | ||
import type { MaybeToken } from "./types"; | ||
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>(target: Target, property: Prop, token: MaybeToken<Value>, ...tags: symbol[]) => 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[], ...injectedArgs: K) => void; |
@@ -1,5 +0,5 @@ | ||
import type { MaybeToken } from "./types"; | ||
import type { Token, MaybeToken } from "./types"; | ||
import { Container } from "./container"; | ||
export declare function define<T, Target extends { | ||
[key in Prop]: T; | ||
}, Prop extends keyof Target>(target: Target, property: Prop, container: Container, token: MaybeToken<T>, tags: symbol[]): void; | ||
}, 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; |
import type { MaybeToken, Token } from "./types"; | ||
export declare const token: <T>(name: string) => Token<T>; | ||
export declare const token: <T, U extends unknown[] = unknown[]>(name: string) => Token<T, U>; | ||
export declare const stringifyToken: (token: MaybeToken) => string; | ||
export declare const getType: (token: MaybeToken) => symbol; |
@@ -11,12 +11,14 @@ import type { Container } from "./container"; | ||
export interface NewAble<T> { | ||
new (...args: unknown[]): T; | ||
new (...ctorArgs: any[]): T; | ||
} | ||
export declare type Factory<T> = () => T; | ||
export declare type Factory<T, U extends Array<unknown> = any> = (...factoryArgs: U) => T; | ||
export declare type Value<T> = T; | ||
export declare type MaybeToken<T = unknown> = Token<T> | symbol; | ||
export declare type MaybeToken<T = unknown, U extends Array<unknown> = unknown[]> = Token<T, U> | symbol; | ||
declare const typeMarker: unique symbol; | ||
export interface Token<T> { | ||
declare const bindedArguments: unique symbol; | ||
export interface Token<T, U extends Array<unknown>> { | ||
type: symbol; | ||
[typeMarker]: T; | ||
[bindedArguments]: U; | ||
} | ||
export {}; |
{ | ||
"name": "@owja/ioc", | ||
"version": "2.0.0-alpha.4", | ||
"version": "2.0.0-alpha.5", | ||
"description": "dependency injection for javascript", | ||
@@ -5,0 +5,0 @@ "main": "dist/ioc.js", |
@@ -364,3 +364,3 @@ # @owja/ioc | ||
class Index extends Component { | ||
@inject(TYPE.TranslationService, SUBSCRIBE) | ||
@inject(TYPE.TranslationService, [SUBSCRIBE]) | ||
readonly service!: TranslatorInterface; | ||
@@ -384,3 +384,3 @@ | ||
super(); | ||
wire(this, "service", TYPE.TranslationService, SUBSCRIBE); | ||
wire(this, "service", TYPE.TranslationService, [SUBSCRIBE]); | ||
} | ||
@@ -392,3 +392,3 @@ | ||
class Index extends Component { | ||
readonly service = resolve(TYPE.TranslationService, SUBSCRIBE); | ||
readonly service = resolve(TYPE.TranslationService, [SUBSCRIBE]); | ||
@@ -409,3 +409,3 @@ [...] | ||
class Example { | ||
@inject(TYPE.MyService, NOPLUGINS) | ||
@inject(TYPE.MyService, [NOPLUGINS]) | ||
readonly service!: MyServiceInterface; | ||
@@ -412,0 +412,0 @@ } |
@@ -5,11 +5,11 @@ import type {Item, NewAble, Factory, Value} from "./types"; | ||
export class Bind<T> { | ||
export class Bind<T, U extends Array<unknown>> { | ||
constructor(private _target: Item<T>) {} | ||
to(object: NewAble<T>): Options<T> { | ||
this._target.injected = () => new object(); | ||
to<O extends NewAble<T>>(object: O): Options<T> { | ||
this._target.injected = (...ctorArgs: U): T => new object(...ctorArgs); | ||
return new Options<T>(this._target); | ||
} | ||
toFactory(factory: Factory<T>): Options<T> { | ||
toFactory(factory: Factory<T, U>): Options<T> { | ||
this._target.injected = factory; | ||
@@ -16,0 +16,0 @@ return new Options<T>(this._target); |
@@ -1,2 +0,2 @@ | ||
import type {Factory, Injected, Item, MaybeToken, Plugin} from "./types"; | ||
import type {Factory, Injected, Item, Token, MaybeToken, Plugin} from "./types"; | ||
import {Bind} from "./bind"; | ||
@@ -13,8 +13,8 @@ import {getType, stringifyToken} from "./token"; | ||
bind<T = never>(token: MaybeToken<T>): Bind<T> { | ||
return new Bind<T>(this._createItem<T>(token)); | ||
bind<T = never, U extends Array<unknown> = never>(token: MaybeToken<T>): Bind<T, U> { | ||
return new Bind<T, U>(this._createItem<T>(token)); | ||
} | ||
rebind<T = never>(token: MaybeToken<T>): Bind<T> { | ||
return this.remove(token).bind<T>(token); | ||
rebind<T = never, U extends Array<unknown> = never>(token: MaybeToken<T>): Bind<T, U> { | ||
return this.remove(token).bind<T, U>(token); | ||
} | ||
@@ -30,3 +30,8 @@ | ||
get<T = never>(token: MaybeToken<T>, tags: symbol[] = [], target?: unknown): T { | ||
get<T, U extends Array<unknown> = never>( | ||
token: Token<T, U> | MaybeToken<T>, | ||
tags: symbol[] = [], | ||
target?: unknown, | ||
injectedArgs: Array<unknown> = [], | ||
): T { | ||
const item = <Item<T> | undefined>this._registry.get(getType(token)); | ||
@@ -38,3 +43,3 @@ | ||
? !item.singleton | ||
? item.injected() | ||
? item.injected(...injectedArgs) | ||
: (item.cache = item.cache || item.injected()) | ||
@@ -41,0 +46,0 @@ : item.injected; |
@@ -1,2 +0,2 @@ | ||
import type {MaybeToken} from "./types"; | ||
import type {Token, MaybeToken} from "./types"; | ||
import type {Container} from "./container"; | ||
@@ -6,3 +6,7 @@ import {define} from "./define"; | ||
export function createDecorator(container: Container) { | ||
return <T>(token: MaybeToken<T>, ...tags: symbol[]) => { | ||
return <T, K extends Array<unknown>>( | ||
token: Token<T, K> | MaybeToken<T>, | ||
tags: symbol[] = [], | ||
...injectedArgs: K | ||
) => { | ||
return function <Target extends {[key in Prop]: T}, Prop extends keyof Target>( | ||
@@ -12,5 +16,5 @@ target: Target, | ||
): void { | ||
define(target, property, container, token, tags); | ||
define(target, property, container, token, tags, ...injectedArgs); | ||
}; | ||
}; | ||
} |
@@ -1,2 +0,2 @@ | ||
import type {MaybeToken} from "./types"; | ||
import type {Token, MaybeToken} from "./types"; | ||
import type {Container} from "./container"; | ||
@@ -6,7 +6,7 @@ import {NOCACHE} from "./tags"; | ||
export function createResolve(container: Container) { | ||
return <T = never>(token: MaybeToken<T>, ...tags: symbol[]) => { | ||
return <T, U extends Array<unknown>>(token: Token<T, U> | MaybeToken<T>, tags: symbol[] = []) => { | ||
let value: T; | ||
return function <R>(this: R): T { | ||
return function <R>(this: R, ...injectedArgs: U): T { | ||
if (tags.indexOf(NOCACHE) !== -1 || value === undefined) { | ||
value = container.get<T>(token, tags, this); | ||
value = container.get(token, tags, this, injectedArgs); | ||
} | ||
@@ -13,0 +13,0 @@ return value; |
@@ -1,2 +0,2 @@ | ||
import type {MaybeToken} from "./types"; | ||
import type {Token, MaybeToken} from "./types"; | ||
import type {Container} from "./container"; | ||
@@ -6,10 +6,11 @@ import {define} from "./define"; | ||
export function createWire(container: Container) { | ||
return <Value, Target extends {[key in Prop]: Value}, Prop extends keyof Target>( | ||
return <Value, Target extends {[key in Prop]: Value}, Prop extends keyof Target, K extends Array<unknown>>( | ||
target: Target, | ||
property: Prop, | ||
token: MaybeToken<Value>, | ||
...tags: symbol[] | ||
token: Token<Value, K> | MaybeToken<Value>, | ||
tags: symbol[] = [], | ||
...injectedArgs: K | ||
) => { | ||
define(target, property, container, token, tags); | ||
define(target, property, container, token, tags, ...injectedArgs); | ||
}; | ||
} |
@@ -1,15 +0,16 @@ | ||
import type {MaybeToken} from "./types"; | ||
import type {Token, MaybeToken} from "./types"; | ||
import {Container} from "./container"; | ||
import {NOCACHE} from "./tags"; | ||
export function define<T, Target extends {[key in Prop]: T}, Prop extends keyof Target>( | ||
export function define<T, Target extends {[key in Prop]: T}, Prop extends keyof Target, K extends Array<unknown>>( | ||
target: Target, | ||
property: Prop, | ||
container: Container, | ||
token: MaybeToken<T>, | ||
token: Token<T, K> | MaybeToken<T>, | ||
tags: symbol[], | ||
...injectedArgs: K | ||
) { | ||
Object.defineProperty(target, property, { | ||
get: function <R>(this: R): T { | ||
const value = container.get<T>(token, tags, this); | ||
const value = container.get(token, tags, this, injectedArgs); | ||
if (tags.indexOf(NOCACHE) === -1) | ||
@@ -16,0 +17,0 @@ Object.defineProperty(this, property, { |
import type {MaybeToken, Token} from "./types"; | ||
export const token = <T>(name: string) => ({type: Symbol(name)} as Token<T>); | ||
export const token = <T, U extends Array<unknown> = unknown[]>(name: string) => ({type: Symbol(name)} as Token<T, U>); | ||
const isToken = <T>(token: MaybeToken<T>): token is Token<T> => typeof token != "symbol"; | ||
const isToken = <T, U extends Array<unknown>>(token: MaybeToken<T, U>): token is Token<T, U> => | ||
typeof token != "symbol"; | ||
@@ -7,0 +8,0 @@ export const stringifyToken = (token: MaybeToken): string => |
@@ -21,15 +21,17 @@ import type {Container} from "./container"; | ||
export interface NewAble<T> { | ||
new (...args: unknown[]): T; | ||
new (...ctorArgs: any[]): T; | ||
} | ||
export type Factory<T> = () => T; | ||
export type Factory<T, U extends Array<unknown> = any> = (...factoryArgs: U) => T; | ||
export type Value<T> = T; | ||
// tokens | ||
export type MaybeToken<T = unknown> = Token<T> | symbol; | ||
export type MaybeToken<T = unknown, U extends Array<unknown> = unknown[]> = Token<T, U> | symbol; | ||
declare const typeMarker: unique symbol; | ||
export interface Token<T> { | ||
declare const bindedArguments: unique symbol; | ||
export interface Token<T, U extends Array<unknown>> { | ||
type: symbol; | ||
[typeMarker]: T; | ||
[bindedArguments]: U; | ||
} |
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
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
57753
315
0