From a4ea13064c798a44bfbeebcb2991a2ffd92d6efa Mon Sep 17 00:00:00 2001 From: Igor Savin Date: Tue, 13 Jun 2023 12:44:52 +0300 Subject: [PATCH] Make eagerInject more configurable (#3) --- README.md | 2 +- lib/awilixManager.ts | 7 ++- test/awilixManager.spec.ts | 110 +++++++++++++++++++++++-------------- 3 files changed, 75 insertions(+), 44 deletions(-) diff --git a/README.md b/README.md index 7f7392b..001d087 100644 --- a/README.md +++ b/README.md @@ -42,7 +42,7 @@ diContainer.register( asyncDisposePriority: 10, // lower value means its disposed earlier asyncInit: 'init', asyncDispose: 'dispose', - eagerInject: true, // this will be constructed and cached immediately. Redundant for resolves with `asyncInit` parameter set, as that is always resolved eagerly. + eagerInject: true, // this will be constructed and cached immediately. Redundant for resolves with `asyncInit` parameter set, as that is always resolved eagerly. If a string is passed, then additional synchronous method will be invoked in addition to constructor on injection. }), ) diff --git a/lib/awilixManager.ts b/lib/awilixManager.ts index 1647548..2cd3cde 100644 --- a/lib/awilixManager.ts +++ b/lib/awilixManager.ts @@ -7,7 +7,7 @@ declare module 'awilix' { asyncInitPriority?: number // lower means it gets initted earlier asyncDispose?: boolean | string asyncDisposePriority?: number // lower means it gets disposed earlier - eagerInject?: boolean + eagerInject?: boolean | string enabled?: boolean } } @@ -76,7 +76,10 @@ export function eagerInject(diContainer: AwilixContainer) { }) for (const entry of dependenciesWithEagerInject) { - diContainer.resolve(entry[0]) + const resolvedComponent = diContainer.resolve(entry[0]) + if (typeof entry[1].eagerInject === 'string') { + resolvedComponent[entry[1].eagerInject]() + } } } diff --git a/test/awilixManager.spec.ts b/test/awilixManager.spec.ts index a1a0eca..03d92b6 100644 --- a/test/awilixManager.spec.ts +++ b/test/awilixManager.spec.ts @@ -24,6 +24,7 @@ class AsyncDisposeClass { } let isInittedGlobal = false +let isInittedCustom = false let isDisposedGlobal = false class AsyncInitSetClass { @@ -64,6 +65,10 @@ class InitSetClass { constructor() { isInittedGlobal = true } + + init() { + isInittedCustom = true + } } describe('awilixManager', () => { @@ -107,28 +112,28 @@ describe('awilixManager', () => { injectionMode: 'PROXY', }) diContainer.register( - 'dependency1', - asClass(AsyncInitClass, { - lifetime: 'SINGLETON', - asyncInit: true, - enabled: false, - }), + 'dependency1', + asClass(AsyncInitClass, { + lifetime: 'SINGLETON', + asyncInit: true, + enabled: false, + }), ) diContainer.register( - 'dependency2', - asClass(AsyncInitClass, { - lifetime: 'SINGLETON', - asyncInit: true, - eagerInject: true, - }), + 'dependency2', + asClass(AsyncInitClass, { + lifetime: 'SINGLETON', + asyncInit: true, + eagerInject: true, + }), ) diContainer.register( - 'dependency3', - asClass(AsyncInitClass, { - lifetime: 'SINGLETON', - asyncInit: 'asyncInit', - enabled: false, - }), + 'dependency3', + asClass(AsyncInitClass, { + lifetime: 'SINGLETON', + asyncInit: 'asyncInit', + enabled: false, + }), ) await asyncInit(diContainer) @@ -250,27 +255,27 @@ describe('awilixManager', () => { injectionMode: 'PROXY', }) diContainer.register( - 'dependency1', - asClass(AsyncDisposeClass, { - lifetime: 'SINGLETON', - asyncDispose: true, - enabled: false, - }), + 'dependency1', + asClass(AsyncDisposeClass, { + lifetime: 'SINGLETON', + asyncDispose: true, + enabled: false, + }), ) diContainer.register( - 'dependency2', - asClass(AsyncDisposeClass, { - lifetime: 'SINGLETON', - asyncDispose: true, - }), + 'dependency2', + asClass(AsyncDisposeClass, { + lifetime: 'SINGLETON', + asyncDispose: true, + }), ) diContainer.register( - 'dependency3', - asClass(AsyncDisposeClass, { - lifetime: 'SINGLETON', - asyncDispose: 'asyncDispose', - enabled: false, - }), + 'dependency3', + asClass(AsyncDisposeClass, { + lifetime: 'SINGLETON', + asyncDispose: 'asyncDispose', + enabled: false, + }), ) const manager = new AwilixManager({ @@ -367,18 +372,41 @@ describe('awilixManager', () => { expect(isInittedGlobal).toBe(true) }) + it('injects dependencies eagerly and calls given init method', () => { + isInittedGlobal = false + const diContainer = createContainer({ + injectionMode: 'PROXY', + }) + diContainer.register( + 'dependency1', + asClass(InitSetClass, { + lifetime: 'SINGLETON', + eagerInject: 'init', + }), + ) + + const manager = new AwilixManager({ + diContainer, + eagerInject: true, + }) + manager.executeInit() + + expect(isInittedGlobal).toBe(true) + expect(isInittedCustom).toBe(true) + }) + it('does not inject dependencies eagerly if disabled', () => { isInittedGlobal = false const diContainer = createContainer({ injectionMode: 'PROXY', }) diContainer.register( - 'dependency1', - asClass(InitSetClass, { - lifetime: 'SINGLETON', - eagerInject: true, - enabled: false, - }), + 'dependency1', + asClass(InitSetClass, { + lifetime: 'SINGLETON', + eagerInject: true, + enabled: false, + }), ) const manager = new AwilixManager({