feat(electron): shared storage (#7492)

This commit is contained in:
EYHN
2024-07-15 03:21:08 +00:00
parent 0f1409756e
commit dca88e24fe
22 changed files with 411 additions and 15 deletions

View File

@@ -12,7 +12,6 @@ import { configurePermissionsModule } from './permissions';
import { configureWorkspacePropertiesModule } from './properties';
import { configureQuickSearchModule } from './quicksearch';
import { configureShareDocsModule } from './share-doc';
import { configureStorageImpls } from './storage';
import { configureTagModule } from './tag';
import { configureTelemetryModule } from './telemetry';
import { configureWorkbenchModule } from './workbench';
@@ -35,7 +34,3 @@ export function configureCommonModules(framework: Framework) {
configureDocsSearchModule(framework);
configureDocLinksModule(framework);
}
export function configureImpls(framework: Framework) {
configureStorageImpls(framework);
}

View File

@@ -0,0 +1,58 @@
import { sharedStorage } from '@affine/electron-api';
import type { GlobalCache, GlobalState } from '@toeverything/infra';
import { Observable } from 'rxjs';
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
const ensureSharedStorage = sharedStorage!;
export class ElectronGlobalState implements GlobalState {
keys(): string[] {
return ensureSharedStorage.globalState.keys();
}
get<T>(key: string): T | undefined {
return ensureSharedStorage.globalState.get(key);
}
watch<T>(key: string) {
return new Observable<T | undefined>(subscriber => {
const unsubscribe = ensureSharedStorage.globalState.watch<T>(key, i => {
subscriber.next(i);
});
return () => unsubscribe();
});
}
set<T>(key: string, value: T): void {
ensureSharedStorage.globalState.set(key, value);
}
del(key: string): void {
ensureSharedStorage.globalState.del(key);
}
clear(): void {
ensureSharedStorage.globalState.clear();
}
}
export class ElectronGlobalCache implements GlobalCache {
keys(): string[] {
return ensureSharedStorage.globalCache.keys();
}
get<T>(key: string): T | undefined {
return ensureSharedStorage.globalCache.get(key);
}
watch<T>(key: string) {
return new Observable<T | undefined>(subscriber => {
const unsubscribe = ensureSharedStorage.globalCache.watch<T>(key, i => {
subscriber.next(i);
});
return () => unsubscribe();
});
}
set<T>(key: string, value: T): void {
ensureSharedStorage.globalCache.set(key, value);
}
del(key: string): void {
ensureSharedStorage.globalCache.del(key);
}
clear(): void {
ensureSharedStorage.globalCache.clear();
}
}

View File

@@ -1,11 +1,17 @@
import { type Framework, GlobalCache, GlobalState } from '@toeverything/infra';
import { ElectronGlobalCache, ElectronGlobalState } from './impls/electron';
import {
LocalStorageGlobalCache,
LocalStorageGlobalState,
} from './impls/storage';
} from './impls/local-storage';
export function configureStorageImpls(framework: Framework) {
export function configureLocalStorageStateStorageImpls(framework: Framework) {
framework.impl(GlobalCache, LocalStorageGlobalCache);
framework.impl(GlobalState, LocalStorageGlobalState);
}
export function configureElectronStateStorageImpls(framework: Framework) {
framework.impl(GlobalCache, ElectronGlobalCache);
framework.impl(GlobalState, ElectronGlobalState);
}