Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
kibertoad committed Feb 19, 2024
2 parents 1d2ce88 + d0a243e commit 1eb1fcd
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 0 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ const awilixManager = new AwilixManager({
diContainer,
asyncInit: true,
asyncDispose: true,
strictBooleanEnforced: true,
})
await awilixManager.executeInit() // this will execute eagerInject and asyncInit
await awilixManager.executeDispose() // this will execute asyncDispose
Expand Down Expand Up @@ -96,11 +97,14 @@ const awilixManager = new AwilixManager({
diContainer,
asyncInit: true,
asyncDispose: true,
strictBooleanEnforced: true,
})
await awilixManager.executeInit() // this will not execute asyncInit, because consumer is disabled
await awilixManager.executeDispose() // this will not execute asyncDispose, because consumer is disabled
```

Note that passing `undefined` or `null` as a value for the `enabled` parameter counts as a default, which is `true`. That may lead to hard-to-debug errors, as it may be erroneously assumed that passing falsy value should equal to passing `false`. In order to prevent this, it is recommended to set `strictBooleanEnforced` flag to `true`, which would throw an error if a non-boolean value is explicitly set to the `enabled` field. In future semver major release this will become a default behaviour.

## Fetching dependencies based on tags

In some cases you may want to get dependencies based on a supplied list of tags.
Expand Down
11 changes: 11 additions & 0 deletions lib/awilixManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,24 @@ export type AwilixManagerConfig = {
asyncInit?: boolean
asyncDispose?: boolean
eagerInject?: boolean
strictBooleanEnforced?: boolean
}

export class AwilixManager {
public readonly config: AwilixManagerConfig

constructor(config: AwilixManagerConfig) {
this.config = config
if (config.strictBooleanEnforced) {
for (const entry of Object.entries(config.diContainer.registrations)) {
const [dependencyName, config] = entry
if ('enabled' in config && config.enabled !== true && config.enabled !== false) {
throw new Error(
`Invalid config for ${dependencyName}. "enabled" field can only be set to true or false, or omitted`,
)
}
}
}
}

async executeInit() {
Expand Down
58 changes: 58 additions & 0 deletions test/awilixManager.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,64 @@ class InitSetClass {
}

describe('awilixManager', () => {
describe('constructor', () => {
it('throws an error if strictBooleanEnforced is set and undefined is passed', () => {
const diContainer = createContainer({
injectionMode: 'PROXY',
})
diContainer.register(
'dependency1',
asClass(AsyncInitClass, {
lifetime: 'SINGLETON',
asyncInit: true,
enabled: undefined,
}),
)
expect(
() =>
new AwilixManager({
diContainer,
strictBooleanEnforced: true,
}),
).toThrow(
/Invalid config for dependency1. "enabled" field can only be set to true or false, or omitted/,
)
})
it('does not throw an error if strictBooleanEnforced is not set and undefined is passed', () => {
const diContainer = createContainer({
injectionMode: 'PROXY',
})
diContainer.register(
'dependency1',
asClass(AsyncInitClass, {
lifetime: 'SINGLETON',
asyncInit: true,
enabled: undefined,
}),
)
new AwilixManager({
diContainer,
})
})
it('does not throw an error if strictBooleanEnforced is set and no undefined is passed', () => {
const diContainer = createContainer({
injectionMode: 'PROXY',
})
diContainer.register(
'dependency1',
asClass(AsyncInitClass, {
lifetime: 'SINGLETON',
asyncInit: true,
enabled: true,
}),
)
new AwilixManager({
diContainer,
strictBooleanEnforced: true,
})
})
})

describe('asyncInit', () => {
it('execute asyncInit on registered dependencies', async () => {
const diContainer = createContainer({
Expand Down

0 comments on commit 1eb1fcd

Please sign in to comment.