Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make eagerInject more configurable #3

Merged
merged 1 commit into from
Jun 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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