Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/tripolskypetr/di-kit

Minimal ioc container which allow circular dependency injection
https://github.com/tripolskypetr/di-kit

circular-dependencies circular-dependency dependency-injection ioc javascript nodejs typescript

Last synced: 14 days ago
JSON representation

Minimal ioc container which allow circular dependency injection

Awesome Lists containing this project

README

        

# di-kit

> Minimal ioc container which allow circular dependency injection

## Installation

```bash
npm install di-kit
```

## Code sample

```tsx
import { inject, provide, init } from './index.mjs';

const TYPES = {
serviceFoo: Symbol('serviceFoo'),
serviceBar: Symbol('serviceBar'),
serviceBaz: Symbol('serviceBaz'),
};

provide(
TYPES.serviceFoo,
() => new class {
bar = inject(TYPES.serviceBar);
}
);

provide(
TYPES.serviceBar,
() => new class {
foo = inject(TYPES.serviceFoo);
baz = inject(TYPES.serviceBaz);
}
);

provide(
TYPES.serviceBaz,
() => new class { }
);

const ioc = {
serviceFoo: inject(TYPES.serviceFoo),
serviceBar: inject(TYPES.serviceBar),
serviceBaz: inject(TYPES.serviceBaz),
};

init();

console.log(ioc.serviceFoo.bar.name);
console.log(ioc.serviceBar.foo.name);
console.log(ioc.serviceBar.baz.name);

```