@cslug/core 0.11.0 → 0.11.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/docs/api.md +2 -1
- package/package.json +2 -2
- package/src/application.ts +3 -1
- package/src/events.ts +17 -0
- package/src/index.ts +1 -0
package/docs/api.md
CHANGED
|
@@ -168,7 +168,8 @@ Immutable; integer minor units (`bigint`) + ISO 4217 currency; no floating point
|
|
|
168
168
|
| `@listener(X \| [X, Y], { queued?, queue?, tries?, backoff? })` | Class listener with `handle(payload, event)`, built by the container (scoped). Combine with `@inject(...)` in any order |
|
|
169
169
|
| queued listeners | Run **after the transaction commits** (`afterCommit`), never on rollback. Without `@cslug/queue`: in-process in a fresh scope, errors logged. With it: `EventBus.dispatchQueued` sends them as jobs |
|
|
170
170
|
| `afterCommit(fn)` | Run once the current transaction commits; outside a transaction, soon after the current task. `@cslug/database` provides the hook (`TransactionHooks`) |
|
|
171
|
-
| `registeredListeners()` |
|
|
171
|
+
| `registeredListeners()` | The listener classes registered so far, by event |
|
|
172
|
+
| `loadListeners(rootDir)` | Imports `app/listeners/**` (not `*.test.ts`); `Application.start()` calls it before `boot()`, so `@listener` classes are registered in every process |
|
|
172
173
|
|
|
173
174
|
## Logger
|
|
174
175
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@cslug/core",
|
|
3
|
-
"version": "0.11.
|
|
3
|
+
"version": "0.11.1",
|
|
4
4
|
"description": "Application, container and @inject, providers, config, events, logger, errors, context, Clock, Money, i18n, contracts.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"type": "module",
|
|
@@ -28,6 +28,6 @@
|
|
|
28
28
|
"bun": ">=1.3.14"
|
|
29
29
|
},
|
|
30
30
|
"peerDependencies": {
|
|
31
|
-
"@cslug/validation": "0.11.
|
|
31
|
+
"@cslug/validation": "0.11.1"
|
|
32
32
|
}
|
|
33
33
|
}
|
package/src/application.ts
CHANGED
|
@@ -16,7 +16,7 @@ import {
|
|
|
16
16
|
} from './contracts'
|
|
17
17
|
import { Encrypter, parseKeys } from './encrypter'
|
|
18
18
|
import { ProviderError } from './errors'
|
|
19
|
-
import { EventBus } from './events'
|
|
19
|
+
import { EventBus, loadListeners } from './events'
|
|
20
20
|
import { type I18nConfig, Translator } from './i18n'
|
|
21
21
|
import { Logger, type LoggerOptions } from './logger'
|
|
22
22
|
import { dependenciesOf, type ProviderClass, type ProviderHooks, type ServiceProvider } from './provider'
|
|
@@ -145,6 +145,8 @@ export class Application extends Container {
|
|
|
145
145
|
this.registerProviders()
|
|
146
146
|
if (this.state !== 'registered') throw new ProviderError(`Cannot start an application that is ${this.state}.`)
|
|
147
147
|
this.state = 'starting'
|
|
148
|
+
// @listener classes register when their module is imported: do it here, so every process has them
|
|
149
|
+
await loadListeners(this.rootDir)
|
|
148
150
|
for (const p of this.instances_) {
|
|
149
151
|
try {
|
|
150
152
|
await p.boot?.()
|
package/src/events.ts
CHANGED
|
@@ -162,3 +162,20 @@ export class EventBus {
|
|
|
162
162
|
}
|
|
163
163
|
}
|
|
164
164
|
}
|
|
165
|
+
|
|
166
|
+
/**
|
|
167
|
+
* Import every module under `<root>/app/listeners/` (subfolders included, not `*.test.ts`), so their `@listener`
|
|
168
|
+
* classes are registered in every process: the web server, workers, commands and tests. `Application.start()`
|
|
169
|
+
* calls it; a folder that does not exist is fine. Returns how many files were imported.
|
|
170
|
+
*/
|
|
171
|
+
export async function loadListeners(rootDir: string): Promise<number> {
|
|
172
|
+
const { existsSync, readdirSync } = await import('node:fs')
|
|
173
|
+
const { join } = await import('node:path')
|
|
174
|
+
const dir = join(rootDir, 'app/listeners')
|
|
175
|
+
if (!existsSync(dir)) return 0
|
|
176
|
+
const files = (readdirSync(dir, { recursive: true }) as string[])
|
|
177
|
+
.filter((f) => f.endsWith('.ts') && !f.endsWith('.test.ts') && !f.endsWith('.d.ts'))
|
|
178
|
+
.sort()
|
|
179
|
+
for (const file of files) await import(join(dir, file))
|
|
180
|
+
return files.length
|
|
181
|
+
}
|