From b166e68ea740e571de16178e2e1d988bb5356c61 Mon Sep 17 00:00:00 2001 From: Eugene Daragan Date: Sat, 30 Mar 2024 17:11:01 +0100 Subject: [PATCH] tokens injection --- README.md | 51 ++++++++++++++++++++++++++++++++++++++++++++++++++- package.json | 6 +----- src/token.ts | 4 +++- 3 files changed, 54 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index 2cdce6e..dc73c10 100644 --- a/README.md +++ b/README.md @@ -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. @@ -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 diff --git a/package.json b/package.json index a931f86..afe6c33 100644 --- a/package.json +++ b/package.json @@ -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" }, diff --git a/src/token.ts b/src/token.ts index f4a2304..fa2561d 100644 --- a/src/token.ts +++ b/src/token.ts @@ -1 +1,3 @@ -export class Token any> {} +export class Token any> { + constructor(public readonly name?: string) {} +}