backend-app-api: attempting to add a lifecycle hook after trigger now throws
Signed-off-by: Patrik Oldsberg <poldsberg@gmail.com>
This commit is contained in:
+3
@@ -44,6 +44,9 @@ export class BackendPluginLifecycleImpl implements LifecycleService {
|
||||
hook: LifecycleServiceStartupHook,
|
||||
options?: LifecycleServiceStartupOptions,
|
||||
): void {
|
||||
if (this.#hasStarted) {
|
||||
throw new Error('Attempted to add startup hook after startup');
|
||||
}
|
||||
this.#startupTasks.push({ hook, options });
|
||||
}
|
||||
|
||||
|
||||
+13
@@ -44,4 +44,17 @@ describe('lifecycleService', () => {
|
||||
});
|
||||
await expect(service.shutdown()).resolves.toBeUndefined();
|
||||
});
|
||||
|
||||
it('should reject hooks after trigger', async () => {
|
||||
const service = new BackendLifecycleImpl(getVoidLogger());
|
||||
await service.startup();
|
||||
expect(() => {
|
||||
service.addStartupHook(() => {});
|
||||
}).toThrow('Attempted to add startup hook after startup');
|
||||
|
||||
await service.shutdown();
|
||||
expect(() => {
|
||||
service.addShutdownHook(() => {});
|
||||
}).toThrow('Attempted to add shutdown hook after shutdown');
|
||||
});
|
||||
});
|
||||
|
||||
+6
@@ -39,6 +39,9 @@ export class BackendLifecycleImpl implements RootLifecycleService {
|
||||
hook: LifecycleServiceStartupHook,
|
||||
options?: LifecycleServiceStartupOptions,
|
||||
): void {
|
||||
if (this.#hasStarted) {
|
||||
throw new Error('Attempted to add startup hook after startup');
|
||||
}
|
||||
this.#startupTasks.push({ hook, options });
|
||||
}
|
||||
|
||||
@@ -72,6 +75,9 @@ export class BackendLifecycleImpl implements RootLifecycleService {
|
||||
hook: LifecycleServiceShutdownHook,
|
||||
options?: LifecycleServiceShutdownOptions,
|
||||
): void {
|
||||
if (this.#hasShutdown) {
|
||||
throw new Error('Attempted to add shutdown hook after shutdown');
|
||||
}
|
||||
this.#shutdownTasks.push({ hook, options });
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user