Skip to content

Commit

Permalink
Make eagerInject more configurable (#3)
Browse files Browse the repository at this point in the history
  • Loading branch information
kibertoad authored Jun 13, 2023
1 parent 16fd7bd commit a4ea130
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 44 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
}),
)

Expand Down
7 changes: 5 additions & 2 deletions lib/awilixManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
Expand Down Expand Up @@ -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]()
}
}
}

Expand Down
110 changes: 69 additions & 41 deletions test/awilixManager.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ class AsyncDisposeClass {
}

let isInittedGlobal = false
let isInittedCustom = false
let isDisposedGlobal = false

class AsyncInitSetClass {
Expand Down Expand Up @@ -64,6 +65,10 @@ class InitSetClass {
constructor() {
isInittedGlobal = true
}

init() {
isInittedCustom = true
}
}

describe('awilixManager', () => {
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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({
Expand Down Expand Up @@ -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({
Expand Down

0 comments on commit a4ea130

Please sign in to comment.