Skip to content

Commit

Permalink
feat(infra): standard lifecycle service
Browse files Browse the repository at this point in the history
  • Loading branch information
EYHN committed Jan 24, 2024
1 parent a732461 commit 07ce708
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 0 deletions.
1 change: 1 addition & 0 deletions packages/common/infra/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
14 changes: 14 additions & 0 deletions packages/common/infra/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
15 changes: 15 additions & 0 deletions packages/common/infra/src/lifecycle/__test__/lifecycle.spec.ts
Original file line number Diff line number Diff line change
@@ -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 changes: 10 additions & 0 deletions packages/common/infra/src/lifecycle/index.ts
Original file line number Diff line number Diff line change
@@ -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());
}
}

0 comments on commit 07ce708

Please sign in to comment.