mirror of
https://github.com/toeverything/AFFiNE.git
synced 2026-02-12 12:28:42 +00:00
feat(infra): standard lifecycle service (#5564)
This commit is contained in:
@@ -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": {
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
});
|
||||
});
|
||||
10
packages/common/infra/src/lifecycle/index.ts
Normal file
10
packages/common/infra/src/lifecycle/index.ts
Normal 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());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user