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

@owja/ioc

Package Overview
Dependencies
Maintainers
1
Versions
18
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@owja/ioc - npm Package Compare versions

Comparing version 1.0.0-beta.3 to 1.0.0-beta.4

2

dist/index.d.ts
export { Container } from "./ioc/container";
export { createDecorator, NOCACHE } from "./ioc/inject";
export { createDecorator, createWire, createResolve, NOCACHE } from "./ioc/inject";

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

var n=function(n){this.t=n};n.prototype.inSingletonScope=function(){this.t.singleton=!0};var t=function(n){this.t=n};t.prototype.to=function(t){return this.t.object=t,new n(this.t)},t.prototype.toFactory=function(t){return this.t.factory=t,new n(this.t)},t.prototype.toValue=function(n){if(void 0===n)throw"cannot bind a value of type undefined";this.t.value=n};var i=function(){this.i=new Map,this.o=[]};i.prototype.bind=function(n){return new t(this.u(n))},i.prototype.rebind=function(n){return this.remove(n).bind(n)},i.prototype.remove=function(n){if(void 0===this.i.get(n))throw n.toString()+" was never bound";return this.i.delete(n),this},i.prototype.get=function(n){var t=this.i.get(n);if(void 0===t)throw"nothing bound to "+n.toString();var i=t.object,r=t.factory,o=t.value,u=t.cache,e=t.singleton,f=function(n){return e&&void 0!==u?u:e?(t.cache=n(),t.cache):n()};if(void 0!==o)return o;if(void 0!==i)return f(function(){return new i});if(void 0!==r)return f(function(){return r()});throw"nothing is bound to "+n.toString()},i.prototype.snapshot=function(){return this.o.push(new Map(this.i)),this},i.prototype.restore=function(){return this.i=this.o.pop()||this.i,this},i.prototype.u=function(n){if(void 0!==this.i.get(n))throw"object can only bound once: "+n.toString();var t={singleton:!1};return this.i.set(n,t),t};var r=Symbol("NOCACHE");exports.Container=i,exports.createDecorator=function(n){return function(t){for(var i=[],o=arguments.length-1;o-- >0;)i[o]=arguments[o+1];return function(n,t,i){return function(o,u){Object.defineProperty(o,u,{get:function(){var o=t.get(n);return-1===i.indexOf(r)&&Object.defineProperty(this,u,{value:o,enumerable:!0}),o},configurable:!0,enumerable:!0})}}(t,n,i)}},exports.NOCACHE=r;
var n=function(n){this.t=n};n.prototype.inSingletonScope=function(){this.t.singleton=!0};var t=function(n){this.t=n};t.prototype.to=function(t){return this.t.object=t,new n(this.t)},t.prototype.toFactory=function(t){return this.t.factory=t,new n(this.t)},t.prototype.toValue=function(n){if(void 0===n)throw"cannot bind a value of type undefined";this.t.value=n};var i=function(){this.i=new Map,this.o=[]};i.prototype.bind=function(n){return new t(this.u(n))},i.prototype.rebind=function(n){return this.remove(n).bind(n)},i.prototype.remove=function(n){if(void 0===this.i.get(n))throw n.toString()+" was never bound";return this.i.delete(n),this},i.prototype.get=function(n){var t=this.i.get(n);if(void 0===t)throw"nothing bound to "+n.toString();var i=t.object,r=t.factory,o=t.value,u=t.cache,e=t.singleton,f=function(n){return e&&void 0!==u?u:e?(t.cache=n(),t.cache):n()};if(void 0!==o)return o;if(void 0!==i)return f(function(){return new i});if(void 0!==r)return f(function(){return r()});throw"nothing is bound to "+n.toString()},i.prototype.snapshot=function(){return this.o.push(new Map(this.i)),this},i.prototype.restore=function(){return this.i=this.o.pop()||this.i,this},i.prototype.u=function(n){if(void 0!==this.i.get(n))throw"object can only bound once: "+n.toString();var t={singleton:!1};return this.i.set(n,t),t};var r=Symbol("NOCACHE");function o(n,t,i,o,u){Object.defineProperty(n,t,{get:function(){var n=i.get(o);return-1===u.indexOf(r)&&Object.defineProperty(this,t,{value:n,enumerable:!0}),n},configurable:!0,enumerable:!0})}exports.Container=i,exports.createDecorator=function(n){return function(t){for(var i=[],r=arguments.length-1;r-- >0;)i[r]=arguments[r+1];return function(n,t,i){return function(r,u){o(r,u,t,n,i)}}(t,n,i)}},exports.createWire=function(n){return function(t,i,r){for(var u=[],e=arguments.length-3;e-- >0;)u[e]=arguments[e+3];o(t,i,n,r,u)}},exports.createResolve=function(n){return function(t){for(var i,o=[],u=arguments.length-1;u-- >0;)o[u]=arguments[u+1];return function(){return-1===o.indexOf(r)&&void 0!==i||(i=n.get(t)),i}}},exports.NOCACHE=r;
//# sourceMappingURL=ioc.js.map
import { Container } from "./container";
export declare const NOCACHE: unique symbol;
export declare function createDecorator(container: Container): (type: symbol, ...args: symbol[]) => (target: object, property: string) => void;
export declare function createWire(container: Container): <T extends object>(target: T, property: keyof T & string, type: symbol, ...args: symbol[]) => void;
export declare function createResolve(container: Container): <T = never>(type: symbol, ...args: symbol[]) => () => T;
{
"name": "@owja/ioc",
"version": "1.0.0-beta.3",
"version": "1.0.0-beta.4",
"description": "dependency injection for javascript",

@@ -38,12 +38,12 @@ "main": "dist/ioc.js",

"@types/jest": "^24.0.13",
"@typescript-eslint/eslint-plugin": "^1.9.0",
"@typescript-eslint/parser": "^1.9.0",
"@typescript-eslint/eslint-plugin": "^1.10.2",
"@typescript-eslint/parser": "^1.10.2",
"eslint": "^5.16.0",
"eslint-plugin-jest": "22.6.3",
"eslint-plugin-jest": "^22.6.4",
"jest": "^24.8.0",
"microbundle": "^0.11.0",
"prettier": "1.17.1",
"prettier": "^1.18.2",
"ts-jest": "^24.0.2",
"ts-node": "^8.1.0",
"typescript": "^3.4.5"
"ts-node": "^8.2.0",
"typescript": "^3.5.1"
},

@@ -50,0 +50,0 @@ "mangle": {

@@ -17,2 +17,3 @@ ![OWJA! IoC](resources/owja-ioc-logo.png)

* Similar syntax to InversifyJS
* Can be used without decorators
* Less Features but **straight forward**

@@ -127,11 +128,13 @@ * Can bind dependencies as **classes**, **factories** and **static values**

We have to create a `inject` decorator for each container.
To use the decorator you have to set `experimentalDecorators` to `true`
in your `tsconfig.json`.
First we have to create a `inject` decorator for each container:
```ts
import {createDecorator} from "@owja/ioc";
export const inject = createDecorator(container);
```
This decorator is needed to resolve our dependencies.
Then we can use the decorator to inject the dependency.

@@ -142,5 +145,72 @@ ```ts

readonly service!: Interface;
method() {
this.service.doSomething();
}
}
```
## The `wire()` Function
If we do not want to use decorators, we can use the wire function. It does the same like the `inject`
decorator and we have to create the function first like we do with `inject`.
```ts
import {createWire} from "@owja/ioc";
export const wire = createWire(container);
```
Then we can wire up the dependent to the dependency.
```ts
class Example {
readonly service!: Interface;
constructor() {
wire(this, "service", symbol);
}
method() {
this.service.doSomething();
}
}
```
> Notice: With `wire()` the property, in this case `service`, has to be public.
## The `resolve()` Function
A second way to resolve a dependency without decorators is to use `resolve()`.
To use `resolve()` we have to create the function first.
```ts
import {createResolve} from "@owja/ioc";
export const resolve = createResolve(container);
```
Then we can resolve the dependency in classes and even functions.
```ts
class Example {
readonly service = resolve<Interface>(symbol)
method() {
this.service().doSomething();
}
}
```
```ts
function Example() {
const service = resolve<Interface>(symbol)
service().doSomething();
}
```
> Notice: We access the dependency trough a function.
> The dependency is not assigned directly to the property/constant.
> If we want direct access we can use `container.get()` but we should avoid
> using `get()` inside of classes because we then loose the lazy dependency
> resolving/injection behavior.
## The `symbol`

@@ -198,4 +268,4 @@

import {IMyService, MyService} from "./service/my-service";
import {IMyOtherService, MyOtherService} from "./service/my-other-service";
import {MyServiceInterface, MyService} from "./service/my-service";
import {MyOtherServiceInterface, MyOtherService} from "./service/my-other-service";

@@ -205,4 +275,4 @@ const container = new Container();

container.bind<IMyService>(TYPE.MyService).to(MyService);
container.bind<IMyOtherService>(TYPE.MyOtherService).to(MyOtherService);
container.bind<MyServiceInterface>(TYPE.MyService).to(MyService);
container.bind<MyOtherServiceInterface>(TYPE.MyOtherService).to(MyOtherService);

@@ -218,11 +288,11 @@ export {container, TYPE, inject};

import {container, TYPE, inject} from "./services/container";
import {IMyService} from "./service/my-service";
import {IMyOtherService} from "./service/my-other-service";
import {MyServiceInterface} from "./service/my-service";
import {MyOtherServiceInterface} from "./service/my-other-service";
class Example {
@inject(TYPE.MyService)
readonly myService!: IMyService;
readonly myService!: MyServiceInterface;
@inject(TYPE.MyOtherSerice)
readonly myOtherService!: IMyOtherService;
readonly myOtherService!: MyOtherServiceInterface;
}

@@ -250,6 +320,6 @@

@inject(TYPE.MyService, NOCACHE)
readonly myService!: IMyService;
readonly myService!: MyServiceInterface;
@inject(TYPE.MyOtherSerice, NOCACHE)
readonly myOtherService!: IMyOtherService;
readonly myOtherService!: MyOtherServiceInterface;
}

@@ -256,0 +326,0 @@

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