Skip to content

Commit

Permalink
docs: document typed usage
Browse files Browse the repository at this point in the history
  • Loading branch information
nikku authored Nov 22, 2023
1 parent d7b84d2 commit c3bb747
Showing 1 changed file with 13 additions and 4 deletions.
17 changes: 13 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ Using [`didi`](https://github.com/nikku/didi) you follow the [dependency injecti

## Example

```js
```ts
import { Injector } from 'didi';

function Car(engine) {
Expand All @@ -29,13 +29,16 @@ function createPetrolEngine(power) {
};
}

// define a (didi) module
// it declares available components by name and specifies how these are provided
// define a (didi) module - it declares available
// components by name and specifies how these are provided
const carModule = {

// asked for 'car', the injector will call new Car(...) to produce it
'car': ['type', Car],

// asked for 'engine', the injector will call createPetrolEngine(...) to produce it
'engine': ['factory', createPetrolEngine],

// asked for 'power', the injector will give it number 1184
'power': ['value', 1184] // probably Bugatti Veyron
};
Expand All @@ -48,10 +51,16 @@ const injector = new Injector([
// use the injector API to retrieve components
injector.get('car').start();

// ...or invoke a function, injecting the arguments
// alternatively invoke a function, injecting the arguments
injector.invoke(function(car) {
console.log('started', car);
});

// if you work with a TypeScript code base, retrieve
// a typed instance of a component
const car: Car = injector.get<Car>('car');

car.start();
```

For real-world examples, check out [Karma](https://github.com/karma-runner/karma) or [diagram-js](https://github.com/bpmn-io/diagram-js), two libraries that heavily use dependency injection at their core. You can also check out [the tests](https://github.com/nikku/didi/blob/master/test/injector.spec.js) to learn about all supported use cases.
Expand Down

0 comments on commit c3bb747

Please sign in to comment.