mirror of
https://github.com/toeverything/AFFiNE.git
synced 2026-02-14 05:14:54 +00:00
feat(electron): shared storage (#7492)
This commit is contained in:
@@ -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);
|
||||
}
|
||||
|
||||
58
packages/frontend/core/src/modules/storage/impls/electron.ts
Normal file
58
packages/frontend/core/src/modules/storage/impls/electron.ts
Normal 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();
|
||||
}
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user