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
@@ -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());
}
}