Skip to content

Commit

Permalink
tokens injection
Browse files Browse the repository at this point in the history
  • Loading branch information
zheksoon committed Mar 30, 2024
1 parent bad10c5 commit b166e68
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 7 deletions.
51 changes: 50 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,55 @@ pet.pet(); // Fluffy petted
Only transient and resolution scopes support argument injection.
Resolution scope instances are cached for the entire resolution, so the arguments are passed only once.

## Class registration

By default, `Scopes.Container` class injection is "sticky" - the class sticks to the container where it was first injected.

If you want to make a class save its instance in some specific parent container (see [Child containers](#Child-containers)), you can use class registration:

```typescript
const container = new Container();

const child = container.childContainer();

class Earth {
static scope = Scopes.Container();
}

// Register the World class in the parent container
container.register({ class: Earth });

// Creates instance on the parent container
const ourEarth = container.inject(Earth);

// Returns the Earth instance from the parent container
const theirEarth = child.inject(Earth);

ourEarth === theirEarth; // true
```

## Injection tokens

Instead of classes, you can use tokens to inject dependencies:

```typescript
import { Container, Token } from "dioma";

const container = new Container();

const token = new Token("Land token");

class Land {
static scope = Scopes.Container();
}

container.register({ token, class: Land });

container.inject(token) === container.inject(Land); // true
```

Tokens are useful when you need to inject a class that is registered later or has various implementations. Tokens also override parent registrations.

## Child containers

You can create child containers to isolate the scope of the classes.
Expand All @@ -239,7 +288,7 @@ class Land {
}

// Register the Land class in the parent container
container.inject(Land);
container.register({ class: Land });

class Garage {
// Land resolves from the parent container
Expand Down
6 changes: 1 addition & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,7 @@
"package.json"
],
"scripts": {
"clear": "rm -rf dist",
"build:es6": "tsc --outDir dist/es6 --module es6 --target es6 --declaration --declarationDir dist",
"build:common": "tsc --outDir dist/common --module commonjs --target es6",
"build:umd": "tsc --outDir dist/umd --module umd --target es5",
"build": "npm run clear && npm run build:es6 && npm run build:common && npm run build:umd",
"build": "vite build --minify=false --sourcemap=true",
"test": "vitest",
"test:coverage": "vitest --coverage"
},
Expand Down
4 changes: 3 additions & 1 deletion src/token.ts
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
export class Token<T extends new (...args: any[]) => any> {}
export class Token<T extends new (...args: any[]) => any> {
constructor(public readonly name?: string) {}
}

0 comments on commit b166e68

Please sign in to comment.