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.2 to 2.0.0-alpha.3

4

dist/index.d.ts

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

export { Container } from "./ioc/container";
export { Container, Plugin } from "./ioc/container";
export { createDecorator } from "./ioc/decorator";
export { createWire } from "./ioc/wire";
export { createResolve } from "./ioc/resolve";
export { NOCACHE } from "./ioc/symbol";
export { NOCACHE, NOPLUGINS } from "./ioc/symbol";
export { token } from "./ioc/token";

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

function t(t){return"symbol"!=typeof t}function n(n){return t(n)?`Token(${n.type.toString()})`:n.toString()}function o(n){return t(n)?n.type:n}class e{constructor(t){this.t=void 0,this.t=t}inSingletonScope(){this.t.singleton=!0}}class i{constructor(t){this.t=void 0,this.t=t}to(t){return this.t.object=t,new e(this.t)}toFactory(t){return this.t.factory=t,new e(this.t)}toValue(t){if(void 0===t)throw"cannot bind a value of type undefined";this.t.value=t}}const r=Symbol("NOCACHE");function s(t,n,o,e,i){Object.defineProperty(t,n,{get:function(){const t=o.get(e);return-1===i.indexOf(r)&&Object.defineProperty(this,n,{value:t,enumerable:!0}),t},configurable:!0,enumerable:!0})}exports.Container=class{constructor(){this.o=new Map,this.i=[]}bind(t){return new i(this.u(t))}rebind(t){return this.remove(t).bind(t)}remove(t){if(void 0===this.o.get(o(t)))throw`${n(t)} was never bound`;return this.o.delete(o(t)),this}get(t){const e=this.o.get(o(t));if(void 0===e)throw`nothing bound to ${n(t)}`;const{object:i,factory:r,value:s,cache:u,singleton:c}=e,h=t=>c&&void 0!==u?u:c?(e.cache=t(),e.cache):t();if(void 0!==s)return s;if(void 0!==i)return h(()=>new i);if(void 0!==r)return h(()=>r());throw`nothing is bound to ${n(t)}`}snapshot(){return this.i.push(new Map(this.o)),this}restore(){return this.o=this.i.pop()||this.o,this}u(t){if(void 0!==this.o.get(o(t)))throw`object can only bound once: ${n(t)}`;const e={singleton:!1};return this.o.set(o(t),e),e}},exports.NOCACHE=r,exports.createDecorator=function(t){return function(n){return(o,e)=>{s(o,e,t,n,[].slice.call(arguments,1))}}},exports.createResolve=function(t){return function(n){let o;return()=>(-1===[].slice.call(arguments,1).indexOf(r)&&void 0!==o||(o=t.get(n)),o)}},exports.createWire=function(t){return function(n,o,e){s(n,o,t,e,[].slice.call(arguments,3))}},exports.token=function(t){return{type:Symbol(t)}};
function t(t){return"symbol"!=typeof t}function n(n){return t(n)?`Token(${n.type.toString()})`:n.toString()}function i(n){return t(n)?n.type:n}const r=Symbol("NOCACHE"),o=Symbol("NOPLUGINS");class e{constructor(t){this.t=void 0,this.t=t}withPlugin(t){return this.t.plugins.push(t),this}}class s extends e{inSingletonScope(){return this.t.singleton=!0,this}}class u{constructor(t){this.t=void 0,this.t=t}to(t){return this.t.factory=()=>new t,new s(this.t)}toFactory(t){return this.t.factory=t,new s(this.t)}toValue(t){if(void 0===t)throw"cannot bind a value of type undefined";return this.t.value=t,new e(this.t)}}function h(t,n,i,o,e){Object.defineProperty(t,n,{get:function(){const t=i.get(o,e,this);return-1===e.indexOf(r)&&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 u(this.h(t))}rebind(t){return this.remove(t).bind(t)}remove(t){if(void 0===this.i.get(i(t)))throw`${n(t)} was never bound`;return this.i.delete(i(t)),this}get(t,r,e){void 0===r&&(r=[]);const s=this.i.get(i(t));if(void 0===s)throw`nothing bound to ${n(t)}`;const{factory:u,value:h,cache:c,singleton:f,plugins:a}=s,d=n=>{if(-1!==r.indexOf(o))return n;for(const i of this.u.concat(a))i(n,e,r,t,this);return n};if(void 0!==h)return d(h);if(void 0!==u)return d((l=()=>u(),f&&void 0!==c?c:f?(s.cache=l(),s.cache):l()));var l;throw`nothing is bound to ${n(t)}`}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(i(t)))throw`object can only bound once: ${n(t)}`;const r={plugins:[]};return this.i.set(i(t),r),r}},exports.NOCACHE=r,exports.NOPLUGINS=o,exports.createDecorator=function(t){return function(n){return(i,r)=>{h(i,r,t,n,[].slice.call(arguments,1))}}},exports.createResolve=function(t){return function(n){var i=[].slice.call(arguments,1);let o;return function(){return-1===i.indexOf(r)&&void 0!==o||(o=t.get(n,i,this)),o}}},exports.createWire=function(t){return function(n,i,r){h(n,i,t,r,[].slice.call(arguments,3))}},exports.token=function(t){return{type:Symbol(t)}};
//# sourceMappingURL=ioc.js.map
import { MaybeToken } from "./token";
interface IConfig<T> {
object?: INewAble<T>;
interface Item<T> {
factory?: Factory<T>;
value?: Value<T>;
cache?: T;
singleton: boolean;
singleton?: boolean;
plugins: Plugin<T>[];
}
interface INewAble<T> {
export declare type Plugin<Dependency = any, Target = any> = (dependency: Dependency, target: Target | undefined, args: symbol[], token: MaybeToken<Dependency>, container: Container) => void;
interface NewAble<T> {
new (...args: any[]): T;

@@ -14,13 +15,16 @@ }

declare type Value<T> = T;
declare class Options<T> {
private _target;
constructor(_target: IConfig<T>);
inSingletonScope(): void;
declare class PluginOptions<T> {
protected _target: Item<T>;
constructor(_target: Item<T>);
withPlugin(plugin: Plugin<T>): PluginOptions<T>;
}
declare class Options<T> extends PluginOptions<T> {
inSingletonScope(): PluginOptions<T>;
}
declare class Bind<T> {
private _target;
constructor(_target: IConfig<T>);
to(object: INewAble<T>): Options<T>;
constructor(_target: Item<T>);
to(object: NewAble<T>): Options<T>;
toFactory(factory: Factory<T>): Options<T>;
toValue(value: Value<T>): void;
toValue(value: Value<T>): PluginOptions<T>;
}

@@ -30,10 +34,12 @@ export declare class Container {

private _snapshots;
private _plugins;
bind<T = never>(token: MaybeToken<T>): Bind<T>;
rebind<T = never>(token: MaybeToken<T>): Bind<T>;
remove(token: MaybeToken): Container;
get<T = never>(token: MaybeToken<T>): T;
get<T = never>(token: MaybeToken<T>, args?: symbol[], target?: unknown): T;
addPlugin(plugin: Plugin): Container;
snapshot(): Container;
restore(): Container;
private _add;
private _create;
}
export {};
import { Container } from "./container";
import { MaybeToken } from "./token";
export declare function createDecorator(container: Container): <T>(token: MaybeToken<T>, ...args: MaybeToken[]) => <TTarget extends { [key in TProp]: T; }, TProp extends string>(target: TTarget, property: TProp) => void;
export declare function createDecorator(container: Container): <T>(token: MaybeToken<T>, ...args: symbol[]) => <Target extends { [key in Prop]: T; }, Prop extends string>(target: Target, property: Prop) => void;
import { Container } from "./container";
import { MaybeToken } from "./token";
export declare function define<TVal, TTarget extends {
[key in TProp]: TVal;
}, TProp extends string>(target: TTarget, property: TProp, container: Container, token: MaybeToken<TVal>, argTokens: MaybeToken[]): void;
export declare function define<T, Target extends {
[key in Prop]: T;
}, Prop extends string>(target: Target, property: Prop, container: Container, token: MaybeToken<T>, args: symbol[]): void;
import { Container } from "./container";
import { MaybeToken } from "./token";
export declare function createResolve(container: Container): <T = never>(token: MaybeToken<T>, ...args: MaybeToken[]) => () => T;
export declare function createResolve(container: Container): <T = never>(token: MaybeToken<T>, ...args: symbol[]) => (this: unknown) => T;
export declare const NOCACHE: unique symbol;
export declare const NOPLUGINS: unique symbol;
import { Container } from "./container";
import { MaybeToken } from "./token";
export declare function createWire(container: Container): <TVal, TTarget extends { [key in TProp]: TVal; }, TProp extends string>(target: TTarget, property: TProp, token: MaybeToken<TVal>, ...args: MaybeToken[]) => void;
export declare function createWire(container: Container): <Value, Target extends { [key in Prop]: Value; }, Prop extends string>(target: Target, property: Prop, token: MaybeToken<Value>, ...args: symbol[]) => void;
{
"name": "@owja/ioc",
"version": "2.0.0-alpha.2",
"version": "2.0.0-alpha.3",
"description": "dependency injection for javascript",

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

@@ -23,2 +23,14 @@ # @owja/ioc

## Install
```bash
npm install --save-dev @owja/ioc
```
Latest preview/dev version (alpha or beta)
```bash
npm install --save-dev @owja/ioc@next
```
## The Container API

@@ -241,3 +253,3 @@

## Type-Safe Token (2.0 beta)
## Type-Safe Token (new in 2.0)

@@ -244,0 +256,0 @@ With version 2 we added the possibility to use a type-safe way to identify our dependencies. This is done with tokens:

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

export {Container} from "./ioc/container";
export {Container, Plugin} from "./ioc/container";
export {createDecorator} from "./ioc/decorator";
export {createWire} from "./ioc/wire";
export {createResolve} from "./ioc/resolve";
export {NOCACHE} from "./ioc/symbol";
export {NOCACHE, NOPLUGINS} from "./ioc/symbol";
export {token} from "./ioc/token";
import {getType, MaybeToken, stringifyToken} from "./token";
import {NOPLUGINS} from "./symbol";
interface IConfig<T> {
object?: INewAble<T>;
interface Item<T> {
factory?: Factory<T>;
value?: Value<T>;
cache?: T;
singleton: boolean;
singleton?: boolean;
plugins: Plugin<T>[];
}
interface INewAble<T> {
export type Plugin<Dependency = any, Target = any> = (
dependency: Dependency,
target: Target | undefined,
args: symbol[],
token: MaybeToken<Dependency>,
container: Container,
) => void;
interface NewAble<T> {
new (...args: any[]): T;
}
type Registry = Map<symbol, IConfig<any>>;
type Registry = Map<symbol, Item<any>>;

@@ -20,7 +29,15 @@ type Factory<T> = () => T;

class Options<T> {
constructor(private _target: IConfig<T>) {}
class PluginOptions<T> {
constructor(protected _target: Item<T>) {}
inSingletonScope() {
withPlugin(plugin: Plugin<T>): PluginOptions<T> {
this._target.plugins.push(plugin);
return this;
}
}
class Options<T> extends PluginOptions<T> {
inSingletonScope(): PluginOptions<T> {
this._target.singleton = true;
return this;
}

@@ -30,6 +47,6 @@ }

class Bind<T> {
constructor(private _target: IConfig<T>) {}
constructor(private _target: Item<T>) {}
to(object: INewAble<T>): Options<T> {
this._target.object = object;
to(object: NewAble<T>): Options<T> {
this._target.factory = () => new object();
return new Options<T>(this._target);

@@ -43,3 +60,3 @@ }

toValue(value: Value<T>): void {
toValue(value: Value<T>): PluginOptions<T> {
if (typeof value === "undefined") {

@@ -49,2 +66,3 @@ throw "cannot bind a value of type undefined";

this._target.value = value;
return new PluginOptions<T>(this._target);
}

@@ -54,7 +72,8 @@ }

export class Container {
private _registry: Registry = new Map<symbol, IConfig<any>>();
private _registry: Registry = new Map<symbol, Item<any>>();
private _snapshots: Registry[] = [];
private _plugins: Plugin[] = [];
bind<T = never>(token: MaybeToken<T>): Bind<T> {
return new Bind<T>(this._add<T>(token));
return new Bind<T>(this._create<T>(token));
}

@@ -76,21 +95,30 @@

get<T = never>(token: MaybeToken<T>): T {
const regItem = this._registry.get(getType(token));
get<T = never>(token: MaybeToken<T>, args: symbol[] = [], target?: unknown): T {
const item = this._registry.get(getType(token));
if (regItem === undefined) {
if (item === undefined) {
throw `nothing bound to ${stringifyToken(token)}`;
}
const {object, factory, value, cache, singleton} = regItem;
const {factory, value, cache, singleton, plugins} = item;
const execPlugins = (item: T): T => {
if (args.indexOf(NOPLUGINS) !== -1) return item;
for (const plugin of this._plugins.concat(plugins)) {
plugin(item, target, args, token, this);
}
return item;
};
const cacheItem = (creator: () => T): T => {
if (singleton && typeof cache !== "undefined") return cache;
if (!singleton) return creator();
regItem.cache = creator();
return regItem.cache;
item.cache = creator();
return item.cache;
};
if (typeof value !== "undefined") return value;
if (typeof object !== "undefined") return cacheItem(() => new object());
if (typeof factory !== "undefined") return cacheItem(() => factory());
if (typeof value !== "undefined") return execPlugins(value);
if (typeof factory !== "undefined") return execPlugins(cacheItem(() => factory()));

@@ -100,2 +128,7 @@ throw `nothing is bound to ${stringifyToken(token)}`;

addPlugin(plugin: Plugin): Container {
this._plugins.push(plugin);
return this;
}
snapshot(): Container {

@@ -111,3 +144,3 @@ this._snapshots.push(new Map(this._registry));

private _add<T>(token: MaybeToken<T>): IConfig<T> {
private _create<T>(token: MaybeToken<T>): Item<T> {
if (this._registry.get(getType(token)) !== undefined) {

@@ -117,7 +150,7 @@ throw `object can only bound once: ${stringifyToken(token)}`;

const conf = {singleton: false};
this._registry.set(getType(token), conf);
const item = {plugins: []};
this._registry.set(getType(token), item);
return conf;
return item;
}
}

@@ -6,4 +6,4 @@ import {Container} from "./container";

export function createDecorator(container: Container) {
return <T>(token: MaybeToken<T>, ...args: MaybeToken[]) => {
return <TTarget extends {[key in TProp]: T}, TProp extends string>(target: TTarget, property: TProp): void => {
return <T>(token: MaybeToken<T>, ...args: symbol[]) => {
return <Target extends {[key in Prop]: T}, Prop extends string>(target: Target, property: Prop): void => {
define(target, property, container, token, args);

@@ -10,0 +10,0 @@ };

@@ -5,13 +5,13 @@ import {Container} from "./container";

export function define<TVal, TTarget extends {[key in TProp]: TVal}, TProp extends string>(
target: TTarget,
property: TProp,
export function define<T, Target extends {[key in Prop]: T}, Prop extends string>(
target: Target,
property: Prop,
container: Container,
token: MaybeToken<TVal>,
argTokens: MaybeToken[],
token: MaybeToken<T>,
args: symbol[],
) {
Object.defineProperty(target, property, {
get: function () {
const value = container.get<any>(token);
if (argTokens.indexOf(NOCACHE) === -1) {
const value = container.get<any>(token, args, this);
if (args.indexOf(NOCACHE) === -1) {
Object.defineProperty(this, property, {

@@ -18,0 +18,0 @@ value,

@@ -6,7 +6,7 @@ import {Container} from "./container";

export function createResolve(container: Container) {
return <T = never>(token: MaybeToken<T>, ...args: MaybeToken[]) => {
return <T = never>(token: MaybeToken<T>, ...args: symbol[]) => {
let value: T;
return (): T => {
return function (this: unknown): T {
if (args.indexOf(NOCACHE) !== -1 || value === undefined) {
value = container.get<T>(token);
value = container.get<T>(token, args, this);
}

@@ -13,0 +13,0 @@ return value;

export const NOCACHE = Symbol("NOCACHE");
export const NOPLUGINS = Symbol("NOPLUGINS");

@@ -6,7 +6,7 @@ import {Container} from "./container";

export function createWire(container: Container) {
return <TVal, TTarget extends {[key in TProp]: TVal}, TProp extends string>(
target: TTarget,
property: TProp,
token: MaybeToken<TVal>,
...args: MaybeToken[]
return <Value, Target extends {[key in Prop]: Value}, Prop extends string>(
target: Target,
property: Prop,
token: MaybeToken<Value>,
...args: symbol[]
) => {

@@ -13,0 +13,0 @@ define(target, property, container, token, args);

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