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:
Patrik Oldsberg
2023-05-25 17:43:48 +02:00
parent f26a927f48
commit 5f28097f5d
3 changed files with 22 additions and 0 deletions
@@ -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 });
}
@@ -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');
});
});
@@ -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 });
}