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 15, 2024
1 parent 1df5298 commit aefeea5
Show file tree
Hide file tree
Showing 3 changed files with 28 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
11 changes: 11 additions & 0 deletions packages/common/infra/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,14 @@ export * from './command';
export * from './di';
export * from './livedata';
export * from './storage';

import { cleanupServiceDecl } from './lifecycle';
import { memoryGlobalCacheDecl, memoryGlobalStateDecl } from './storage';

export const infraServicesDecl = [cleanupServiceDecl];

export const testInfraServicesDecl = [
...infraServicesDecl,
memoryGlobalCacheDecl,
memoryGlobalStateDecl,
];
16 changes: 16 additions & 0 deletions packages/common/infra/src/lifecycle/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { declareService } from '../di';

export class CleanupService {
private readonly cleanupCallbacks: (() => void)[] = [];
constructor() {}
add(fn: () => void) {
this.cleanupCallbacks.push(fn);
}
cleanup() {
this.cleanupCallbacks.forEach(fn => fn());
}
}

export const cleanupServiceDecl = declareService(CleanupService, {
factory: () => new CleanupService(),
});

0 comments on commit aefeea5

Please sign in to comment.