mirror of
https://github.com/toeverything/AFFiNE.git
synced 2026-07-22 12:36:24 +08:00
feat(infra): standard lifecycle service (#5564)
This commit is contained in:
@@ -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);
|
||||
});
|
||||
});
|
||||
@@ -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