There can be more than Notion and Miro. AFFiNE(pronounced [ə‘fain]) is a next-gen knowledge base that brings planning, sorting and creating all together. Privacy first, open-source, customizable and ready to use.
TypeScript appcrdteditorelectronknowledge-basemarkdownmironotesnotionnotion-alternativerustrust-langrust-languagetabletableviewwhiteboardwikiworkspace
feat(infra): standard storage service (#5563)
EYHNb3a8e6298439
diff --git a/packages/common/infra/package.json b/packages/common/infra/package.jsonindex 03486b0bad..6303208aa5 100644--- a/packages/common/infra/package.json+++ b/packages/common/infra/package.json@@ -9,6 +9,7 @@"./app-config-storage": "./src/app-config-storage.ts","./di": "./src/di/index.ts","./livedata": "./src/livedata/index.ts",+ "./storage": "./src/storage/index.ts",".": "./src/index.ts"},"dependencies": {diff --git a/packages/common/infra/src/index.ts b/packages/common/infra/src/index.tsindex 6e94b1bdc7..fe3362374d 100644--- a/packages/common/infra/src/index.ts+++ b/packages/common/infra/src/index.ts@@ -2,3 +2,6 @@ export * from './app-config-storage';export * from './atom';export * from './blocksuite';export * from './command';+export * from './di';+export * from './livedata';+export * from './storage';diff --git a/packages/common/infra/src/storage/__tests__/memento.spec.ts b/packages/common/infra/src/storage/__tests__/memento.spec.tsnew file mode 100644index 0000000000..b8cfe96db5--- /dev/null+++ b/packages/common/infra/src/storage/__tests__/memento.spec.ts@@ -0,0 +1,40 @@+import { describe, expect, test } from 'vitest';++import { ServiceCollection } from '../../di';+import { GlobalCache, GlobalState, MemoryMemento } from '..';++describe('memento', () => {+ test('memory', () => {+ const memento = new MemoryMemento();++ expect(memento.get('foo')).toBeNull();+ memento.set('foo', 'bar');+ expect(memento.get('foo')).toEqual('bar');++ let subscribed = null;+ const subscription = memento.watch('foo').subscribe(v => {+ subscribed = v;+ });+ expect(subscribed).toEqual('bar');+ memento.set('foo', 'baz');+ expect(subscribed).toEqual('baz');++ subscription.unsubscribe();+ memento.set('foo', 'hello');+ expect(subscribed).toEqual('baz');+ });++ test('service', () => {+ const services = new ServiceCollection();++ services+ .addImpl(GlobalCache, MemoryMemento)+ .addImpl(GlobalState, MemoryMemento);++ const provider = services.provider();+ const cache = provider.get(GlobalCache);+ expect(cache).toBeInstanceOf(MemoryMemento);+ const state = provider.get(GlobalState);+ expect(state).toBeInstanceOf(MemoryMemento);+ });+});diff --git a/packages/common/infra/src/storage/index.ts b/packages/common/infra/src/storage/index.tsnew file mode 100644index 0000000000..482b604413--- /dev/null+++ b/packages/common/infra/src/storage/index.ts@@ -0,0 +1 @@+export * from './memento';diff --git a/packages/common/infra/src/storage/memento.ts b/packages/common/infra/src/storage/memento.tsnew file mode 100644index 0000000000..73b34c893c--- /dev/null+++ b/packages/common/infra/src/storage/memento.ts@@ -0,0 +1,57 @@+import type { Observable } from 'rxjs';++import { createIdentifier } from '../di';+import { LiveData } from '../livedata';++/**+ * A memento represents a storage utility. It can store and retrieve values, and observe changes.+ */+export interface Memento {+ get<T>(key: string): T | null;+ watch<T>(key: string): Observable<T | null>;+ set<T>(key: string, value: T | null): void;+}++/**+ * A memento object that stores the entire application state.+ *+ * State is persisted, even the application is closed.+ */+export interface GlobalState extends Memento {}++export const GlobalState = createIdentifier<GlobalState>('GlobalState');++/**+ * A memento object that stores the entire application cache.+ *+ * Cache may be deleted from time to time, business logic should not rely on cache.+ */+export interface GlobalCache extends Memento {}++export const GlobalCache = createIdentifier<GlobalCache>('GlobalCache');++/**+ * A simple implementation of Memento. Used for testing.+ */+export class MemoryMemento implements Memento {+ private readonly data = new Map<string, LiveData<any>>();++ private getLiveData(key: string): LiveData<any> {+ let data = this.data.get(key);+ if (!data) {+ data = new LiveData<any>(null);+ this.data.set(key, data);+ }+ return data;+ }++ get<T>(key: string): T | null {+ return this.getLiveData(key).value;+ }+ watch<T>(key: string): Observable<T | null> {+ return this.getLiveData(key).asObservable();+ }+ set<T>(key: string, value: T | null): void {+ this.getLiveData(key).next(value);+ }+}