@travetto/di
Advanced tools
Comparing version 3.3.1 to 3.3.2
{ | ||
"name": "@travetto/di", | ||
"version": "3.3.1", | ||
"version": "3.3.2", | ||
"description": "Dependency registration/management and injection support.", | ||
@@ -30,6 +30,6 @@ "keywords": [ | ||
"dependencies": { | ||
"@travetto/registry": "^3.3.1" | ||
"@travetto/registry": "^3.3.2" | ||
}, | ||
"peerDependencies": { | ||
"@travetto/transformer": "^3.3.0" | ||
"@travetto/transformer": "^3.3.1" | ||
}, | ||
@@ -36,0 +36,0 @@ "peerDependenciesMeta": { |
@@ -101,2 +101,29 @@ <!-- This file was generated by @travetto/doc and should not be modified directly --> | ||
**Code: Example Conditional Dependency** | ||
```typescript | ||
import { GlobalEnv } from '@travetto/base'; | ||
import { Inject, Injectable } from '@travetto/di'; | ||
@Injectable({ enabled: !GlobalEnv.devMode }) | ||
class ProductionLogger { | ||
async log() { | ||
console.log('This will only run in production'); | ||
} | ||
} | ||
@Injectable() | ||
class RuntimeService { | ||
@Inject() | ||
logger?: ProductionLogger; | ||
action(): void { | ||
// Only injected when available, in prod | ||
this.logger?.log(); | ||
// Do work | ||
} | ||
} | ||
``` | ||
In this example, the enabled flag is specified in relationship to the deployment environment. When coupled with optional properties, and optional chaining, allows for seamless inclusion of optional dependencies at runtime. | ||
**Note**: Other modules are able to provide aliases to [@Injectable](https://github.com/travetto/travetto/tree/main/module/di/src/decorator.ts#L31) that also provide additional functionality. For example, the [Configuration](https://github.com/travetto/travetto/tree/main/module/config#readme "Configuration support") module @Config or the [RESTful API](https://github.com/travetto/travetto/tree/main/module/rest#readme "Declarative api for RESTful APIs with support for the dependency injection module.") module @Controller decorator registers the associated class as an injectable element. | ||
@@ -103,0 +130,0 @@ |
@@ -274,2 +274,3 @@ import { Class, ClassInstance, ConcreteClass, GlobalEnv } from '@travetto/base'; | ||
class: cls, | ||
enabled: true, | ||
target: cls, | ||
@@ -301,3 +302,3 @@ interfaces: [], | ||
*/ | ||
getCandidateTypes<T>(target: Class<T>): InjectableConfig[] { | ||
getCandidateTypes<T>(target: Class<T>): InjectableConfig<T>[] { | ||
const targetId = target.Ⲑid; | ||
@@ -311,2 +312,11 @@ const qualifiers = this.targetToClass.get(targetId)!; | ||
/** | ||
* Get candidate instances by target type, with an optional filter | ||
*/ | ||
getCandidateInstances<T>(target: Class, predicate?: (cfg: InjectableConfig<T>) => boolean): Promise<T[]> { | ||
// eslint-disable-next-line @typescript-eslint/consistent-type-assertions | ||
const inputs = this.getCandidateTypes<T>(target as Class<T>).filter(x => !predicate || predicate(x)); | ||
return Promise.all(inputs.map(l => this.getInstance<T>(l.class, l.qualifier))); | ||
} | ||
/** | ||
* Register a constructor with dependencies | ||
@@ -333,2 +343,3 @@ */ | ||
config.enabled = pConfig.enabled ?? config.enabled; | ||
config.class = cls; | ||
@@ -367,2 +378,3 @@ config.qualifier = pConfig.qualifier ?? config.qualifier ?? Symbol.for(cls.Ⲑid); | ||
finalConfig.enabled = config.enabled ?? true; | ||
finalConfig.factory = config.fn; | ||
@@ -423,2 +435,6 @@ finalConfig.target = config.target; | ||
if (!(typeof config.enabled === 'boolean' ? config.enabled : config.enabled())) { | ||
return config; // Do not setup if disabled | ||
} | ||
// Allow for the factory to fulfill the target | ||
@@ -549,15 +565,2 @@ let parentClass = config.factory ? config.target : Object.getPrototypeOf(cls); | ||
/** | ||
* Reset registry | ||
*/ | ||
override onReset(): void { | ||
super.onReset(); | ||
this.pendingFinalize = []; | ||
this.instances.clear(); | ||
this.instancePromises.clear(); | ||
this.targetToClass.clear(); | ||
this.classToTarget.clear(); | ||
this.factories.clear(); | ||
} | ||
/** | ||
* Inject fields into instance | ||
@@ -564,0 +567,0 @@ */ |
@@ -40,2 +40,6 @@ import { Class } from '@travetto/base'; | ||
/** | ||
* Is this injectable enabled | ||
*/ | ||
enabled: boolean | (() => boolean); | ||
/** | ||
* Reference for the class | ||
@@ -70,2 +74,6 @@ */ | ||
/** | ||
* Is this injectable enabled | ||
*/ | ||
enabled: boolean | (() => boolean); | ||
/** | ||
* Src of the factory method | ||
@@ -72,0 +80,0 @@ */ |
@@ -17,3 +17,2 @@ import { RetargettingProxy, Class, ClassInstance } from '@travetto/base'; | ||
#registryDestroyInstance: <T>(target: Class<T>, qualifier: symbol) => void; | ||
#registryOnReset: () => void; | ||
@@ -90,7 +89,2 @@ /** | ||
onReset(): void { | ||
this.#registryOnReset(); | ||
this.#proxies.clear(); | ||
} | ||
register(registry: typeof DependencyRegistry): void { | ||
@@ -102,7 +96,5 @@ this.#registry = registry; | ||
this.#registryDestroyInstance = registry['destroyInstance'].bind(registry); | ||
this.#registryOnReset = registry['onReset'].bind(registry); | ||
this.#registry['createInstance'] = this.createInstance.bind(this); | ||
this.#registry['destroyInstance'] = this.destroyInstance.bind(this); | ||
this.#registry['onReset'] = this.onReset.bind(this); | ||
this.#registry['onInstallFinalize'] = this.onInstallFinalize.bind(this); | ||
@@ -109,0 +101,0 @@ } |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
48663
922
317
Updated@travetto/registry@^3.3.2