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 19, 2024
1 parent 4e7d740 commit e0c4b4f
Show file tree
Hide file tree
Showing 5 changed files with 45 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());
}
}
5 changes: 5 additions & 0 deletions packages/common/infra/src/livedata/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,12 @@ const logger = new DebugLogger('livedata');
* NOTICE: LiveData.from will not complete when the observable completes, you can use `spreadComplete` option to change
* this behavior.
*
* ## Why is it called LiveData
*
* This API is very similar to LiveData in Android, as both are based on Observable, so I named it LiveData.
*
* @see {@link https://rxjs.dev/api/index/class/BehaviorSubject}
* @see {@link https://developer.android.com/topic/libraries/architecture/livedata}
*/
export class LiveData<T = unknown> extends BehaviorSubject<T> {
static from<T>(
Expand Down

0 comments on commit e0c4b4f

Please sign in to comment.