@znight/core 0.6.3 → 0.6.4
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/README.md +2 -1
- package/dist/index.mjs +36 -4
- package/dist/index.mjs.map +1 -1
- package/package.json +1 -1
- package/src/application.ts +54 -5
package/package.json
CHANGED
package/src/application.ts
CHANGED
|
@@ -116,6 +116,13 @@ export class Application {
|
|
|
116
116
|
#startAborted = false
|
|
117
117
|
/** Releases a `stop()` waiting for the start, when a start hook asks to stop in turn. */
|
|
118
118
|
#releaseStopWait: (() => void) | undefined
|
|
119
|
+
/** Owner of the `onStart` hook in progress. */
|
|
120
|
+
#starting: StartOwner | undefined
|
|
121
|
+
/**
|
|
122
|
+
* Owner of the `onStart` hook still in flight when the wait of `stop()` timed out: the shutdown
|
|
123
|
+
* skips its `onDispose`, and it is stopped and disposed when that hook settles.
|
|
124
|
+
*/
|
|
125
|
+
#lateStart: StartOwner | undefined
|
|
119
126
|
|
|
120
127
|
private constructor(options: ApplicationOptions) {
|
|
121
128
|
const plugins = [...(options.plugins ?? [])]
|
|
@@ -292,11 +299,11 @@ export class Application {
|
|
|
292
299
|
async #runStartHooks(): Promise<boolean> {
|
|
293
300
|
const providers = this.container.instances()
|
|
294
301
|
for (const provider of providers) {
|
|
295
|
-
if (!(await this.#startHook(() => this.#callProvider(provider, 'onStart')))) return false
|
|
302
|
+
if (!(await this.#startHook(() => this.#callProvider(provider, 'onStart'), { provider }))) return false
|
|
296
303
|
this.#startedProviders.add(provider.token)
|
|
297
304
|
}
|
|
298
305
|
for (const plugin of this.#plugins) {
|
|
299
|
-
if (!(await this.#startHook(() => this.#callPlugin(plugin, 'onStart')))) return false
|
|
306
|
+
if (!(await this.#startHook(() => this.#callPlugin(plugin, 'onStart'), { plugin }))) return false
|
|
300
307
|
this.#startedPlugins.push(plugin)
|
|
301
308
|
}
|
|
302
309
|
for (const provider of providers) {
|
|
@@ -311,18 +318,51 @@ export class Application {
|
|
|
311
318
|
/**
|
|
312
319
|
* Runs one start hook in an async context carrying a fresh marker, so that a `stop()` called from
|
|
313
320
|
* it (or from work it awaits) is recognised. Returns `false` when the hook stopped the application.
|
|
321
|
+
* `owner` is given for `onStart` hooks: one that settles after `stop()` stopped waiting for it is
|
|
322
|
+
* stopped and disposed then (see `#stopLateStart`).
|
|
314
323
|
*/
|
|
315
|
-
async #startHook(hook: () => Promise<void
|
|
324
|
+
async #startHook(hook: () => Promise<void>, owner?: StartOwner): Promise<boolean> {
|
|
316
325
|
const marker = {}
|
|
317
326
|
this.#startHookMarker = marker
|
|
327
|
+
this.#starting = owner
|
|
328
|
+
let started = false
|
|
318
329
|
try {
|
|
319
330
|
await startHookContext.run(marker, hook)
|
|
331
|
+
started = true
|
|
320
332
|
} finally {
|
|
321
333
|
this.#startHookMarker = undefined
|
|
334
|
+
this.#starting = undefined
|
|
335
|
+
if (owner !== undefined && this.#lateStart === owner) {
|
|
336
|
+
await this.#stopLateStart(owner, started)
|
|
337
|
+
}
|
|
322
338
|
}
|
|
323
339
|
return !this.#startAborted
|
|
324
340
|
}
|
|
325
341
|
|
|
342
|
+
/**
|
|
343
|
+
* Stops what an `onStart` hook settling after the shutdown started (best effort, failures logged):
|
|
344
|
+
* a plugin gets `onStop` (when its `onStart` succeeded) then the `onDispose` the shutdown skipped;
|
|
345
|
+
* a provider, already disposed by the shutdown, gets `onStop` when its `onStart` succeeded.
|
|
346
|
+
*/
|
|
347
|
+
async #stopLateStart(owner: StartOwner, started: boolean): Promise<void> {
|
|
348
|
+
const { plugin, provider } = owner
|
|
349
|
+
const label = plugin === undefined ? `${provider?.displayName}.onStart()` : `Plugin "${plugin.name}" onStart()`
|
|
350
|
+
this.#logger.warn(`${label} settled after the shutdown stopped waiting for it: stopping it now`)
|
|
351
|
+
const errors: unknown[] = []
|
|
352
|
+
const step = this.#stepRunner(errors)
|
|
353
|
+
if (plugin !== undefined) {
|
|
354
|
+
if (started && typeof plugin.onStop === 'function') {
|
|
355
|
+
await step(`Plugin "${plugin.name}" onStop()`, () => plugin.onStop?.(this.#context))
|
|
356
|
+
}
|
|
357
|
+
if (typeof plugin.onDispose === 'function') {
|
|
358
|
+
await step(`Plugin "${plugin.name}" onDispose()`, () => plugin.onDispose?.(this.#context))
|
|
359
|
+
}
|
|
360
|
+
} else if (provider !== undefined && started && hasLifecycleHook(provider.instance, 'onStop')) {
|
|
361
|
+
await step(`${provider.displayName}.onStop()`, () => callLifecycleHook(provider.instance, 'onStop'))
|
|
362
|
+
}
|
|
363
|
+
for (const error of errors) this.#logger.error('Failed to stop a late start', { err: error })
|
|
364
|
+
}
|
|
365
|
+
|
|
326
366
|
/** Whether the caller runs in the async context of the start hook in progress. */
|
|
327
367
|
#calledFromStartHook(): boolean {
|
|
328
368
|
const marker = this.#startHookMarker
|
|
@@ -366,9 +406,11 @@ export class Application {
|
|
|
366
406
|
// a start hook may never settle: waiting for the start is a step of the shutdown
|
|
367
407
|
await step('Waiting for start()', () => this.#waitForStart())
|
|
368
408
|
if (this.#state === 'starting') {
|
|
369
|
-
// timed out: the remaining start hooks are skipped, what completed its onStart is stopped
|
|
409
|
+
// timed out: the remaining start hooks are skipped, what completed its onStart is stopped,
|
|
410
|
+
// and the owner of the onStart still in flight is stopped and disposed once it settles
|
|
370
411
|
this.#startAborted = true
|
|
371
412
|
this.#releaseStopWait = undefined
|
|
413
|
+
this.#lateStart = this.#starting
|
|
372
414
|
}
|
|
373
415
|
}
|
|
374
416
|
if (this.#state === 'failed' || this.#state === 'stopped') {
|
|
@@ -430,7 +472,8 @@ export class Application {
|
|
|
430
472
|
errors.push(...flatten(error))
|
|
431
473
|
}
|
|
432
474
|
for (const plugin of [...this.#registered].reverse()) {
|
|
433
|
-
|
|
475
|
+
// a plugin whose onStart is still in flight is disposed when it settles (#stopLateStart)
|
|
476
|
+
if (typeof plugin.onDispose === 'function' && plugin !== this.#lateStart?.plugin) {
|
|
434
477
|
await step(`Plugin "${plugin.name}" onDispose()`, () => plugin.onDispose?.(this.#context))
|
|
435
478
|
}
|
|
436
479
|
}
|
|
@@ -509,6 +552,12 @@ export class Application {
|
|
|
509
552
|
}
|
|
510
553
|
}
|
|
511
554
|
|
|
555
|
+
/** The plugin or provider an `onStart` hook belongs to. */
|
|
556
|
+
interface StartOwner {
|
|
557
|
+
readonly plugin?: Plugin
|
|
558
|
+
readonly provider?: ManagedInstance
|
|
559
|
+
}
|
|
560
|
+
|
|
512
561
|
function entriesOf(values: ProviderValues): Iterable<readonly [ProviderKey, unknown]> {
|
|
513
562
|
if (typeof (values as Partial<Iterable<unknown>>)[Symbol.iterator] === 'function') {
|
|
514
563
|
return values as Iterable<readonly [ProviderKey, unknown]>
|