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

Add enabled flag #2

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
10 changes: 5 additions & 5 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
version: 2
updates:
- package-ecosystem: github-actions
directory: "/"
directory: '/'
schedule:
interval: daily
open-pull-requests-limit: 10
- package-ecosystem: npm
directory: "/"
directory: '/'
schedule:
interval: daily
open-pull-requests-limit: 10
ignore:
- dependency-name: "@typescript-eslint/eslint-plugin"
- dependency-name: "@typescript-eslint/parser"
- dependency-name: "@types/node"
- dependency-name: '@typescript-eslint/eslint-plugin'
- dependency-name: '@typescript-eslint/parser'
- dependency-name: '@types/node'
1 change: 0 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ jobs:
- name: Run Tests
run: npm run test:coverage


automerge:
needs: build
runs-on: ubuntu-latest
Expand Down
1 change: 0 additions & 1 deletion .github/workflows/coverage.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
---

name: coverage

on:
Expand Down
1 change: 0 additions & 1 deletion .github/workflows/linting.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
---

name: linting

on:
Expand Down
84 changes: 67 additions & 17 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,32 +21,82 @@ import { AwilixManager } from 'awilix-manager'
import { asClass, createContainer } from 'awilix'

class AsyncClass {
async init() {
// init logic
}
async init() {
// init logic
}

async dispose() {
// dispose logic
}
async dispose() {
// dispose logic
}
}

const diContainer = createContainer({
injectionMode: 'PROXY',
injectionMode: 'PROXY',
})

diContainer.register(
'dependency1',
asClass(AsyncClass, {
lifetime: 'SINGLETON',
asyncInitPriority: 10, // lower value means its initted earlier
asyncDisposePriority: 10, // lower value means its disposed earlier
asyncInit: 'init',
asyncDispose: 'dispose',
eagerInject: true, // this will be constructed and cached immediately
}),
'dependency1',
asClass(AsyncClass, {
lifetime: 'SINGLETON',
asyncInitPriority: 10, // lower value means its initted earlier
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.
}),
)

const awilixManager = new AwilixManager()
const awilixManager = new AwilixManager({
diContainer,
asyncInit: true,
asyncDispose: true,
})
await awilixManager.executeInit() // this will execute eagerInject and asyncInit
await awilixManager.executeDispose() // this will execute asyncDispose
```

## Disabling eager injection conditionally

In some cases you may want to prevent eager injection and async disposal of some of your dependencies - e. g. when you want to disable all of your background jobs or message consumers in some of your integration tests.
You can use `enabled` resolver parameter for that:

```js
import { AwilixManager } from 'awilix-manager'
import { asClass, createContainer } from 'awilix'

class QueueConsumerClass {
async consume() {
// consumer registration logic
}

async destroy() {
// dispose logic
}
}

const diContainer = createContainer({
injectionMode: 'PROXY',
})

const isAMQPEnabled = false // disable consumers, e. g. for tests

diContainer.register(
'dependency1',
asClass(QueueConsumerClass, {
lifetime: 'SINGLETON',
asyncInitPriority: 10, // lower value means its initted earlier
asyncDisposePriority: 10, // lower value means its disposed earlier
asyncInit: 'consume',
asyncDispose: 'destroy',
enabled: isAMQPEnabled, // default is true
}),
)

const awilixManager = new AwilixManager({
diContainer,
asyncInit: true,
asyncDispose: 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
```
7 changes: 4 additions & 3 deletions lib/awilixManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ declare module 'awilix' {
asyncDispose?: boolean | string
asyncDisposePriority?: number // lower means it gets disposed earlier
eagerInject?: boolean
enabled?: boolean
}
}

Expand Down Expand Up @@ -43,7 +44,7 @@ export class AwilixManager {
export async function asyncInit(diContainer: AwilixContainer) {
const dependenciesWithAsyncInit = Object.entries(diContainer.registrations)
.filter((entry) => {
return entry[1].asyncInit
return entry[1].asyncInit && entry[1].enabled !== false
})
.sort((entry1, entry2) => {
const [key1, resolver1] = entry1
Expand Down Expand Up @@ -71,7 +72,7 @@ export async function asyncInit(diContainer: AwilixContainer) {

export function eagerInject(diContainer: AwilixContainer) {
const dependenciesWithEagerInject = Object.entries(diContainer.registrations).filter((entry) => {
return entry[1].eagerInject
return entry[1].eagerInject && entry[1].enabled !== false
})

for (const entry of dependenciesWithEagerInject) {
Expand All @@ -82,7 +83,7 @@ export function eagerInject(diContainer: AwilixContainer) {
export async function asyncDispose(diContainer: AwilixContainer) {
const dependenciesWithAsyncDispose = Object.entries(diContainer.registrations)
.filter((entry) => {
return entry[1].asyncDispose
return entry[1].asyncDispose && entry[1].enabled !== false
})
.sort((entry1, entry2) => {
const [key1, resolver1] = entry1
Expand Down
8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@
"devDependencies": {
"@rollup/plugin-terser": "^0.4.3",
"@rollup/plugin-typescript": "^11.1.1",
"@types/node": "^20.2.6",
"@typescript-eslint/eslint-plugin": "^5.59.9",
"@typescript-eslint/parser": "^5.59.9",
"@types/node": "^20.3.1",
"@typescript-eslint/eslint-plugin": "^5.59.11",
"@typescript-eslint/parser": "^5.59.11",
"@vitest/coverage-v8": "^0.32.0",
"del-cli": "^5.0.0",
"eslint": "^8.42.0",
Expand All @@ -36,7 +36,7 @@
"eslint-plugin-prettier": "^4.2.1",
"eslint-plugin-vitest": "^0.2.6",
"prettier": "^2.8.8",
"rollup": "^3.24.1",
"rollup": "^3.25.1",
"typescript": "^5.1.3",
"vitest": "^0.32.0"
},
Expand Down
2 changes: 1 addition & 1 deletion rollup.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,6 @@ export default [
},
],
plugins: [typescript()],
external: ['awilix-manager']
external: ['awilix-manager'],
},
]
Loading