feat(infra): standard lifecycle service (#5564)

This commit is contained in:
EYHN
2024-01-30 06:31:18 +00:00
parent b3a8e62984
commit 88cd83fed1
4 changed files with 40 additions and 0 deletions

View File

@@ -10,6 +10,7 @@
"./di": "./src/di/index.ts",
"./livedata": "./src/livedata/index.ts",
"./storage": "./src/storage/index.ts",
"./lifecycle": "./src/lifecycle/index.ts",
".": "./src/index.ts"
},
"dependencies": {

View File

@@ -5,3 +5,17 @@ export * from './command';
export * from './di';
export * from './livedata';
export * from './storage';
import type { ServiceCollection } from './di';
import { CleanupService } from './lifecycle';
import { GlobalCache, GlobalState, MemoryMemento } from './storage';
export function configureInfraServices(services: ServiceCollection) {
services.add(CleanupService);
}
export function configureTestingInfraServices(services: ServiceCollection) {
configureInfraServices(services);
services.addImpl(GlobalCache, MemoryMemento);
services.addImpl(GlobalState, MemoryMemento);
}

View File

@@ -0,0 +1,15 @@
import { describe, expect, test } from 'vitest';
import { CleanupService } from '..';
describe('lifecycle', () => {
test('cleanup', () => {
const cleanup = new CleanupService();
let cleaned = false;
cleanup.add(() => {
cleaned = true;
});
cleanup.cleanup();
expect(cleaned).toBe(true);
});
});

View File

@@ -0,0 +1,10 @@
export class CleanupService {
private readonly cleanupCallbacks: (() => void)[] = [];
constructor() {}
add(fn: () => void) {
this.cleanupCallbacks.push(fn);
}
cleanup() {
this.cleanupCallbacks.forEach(fn => fn());
}
}