-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(electron): shared storage (#7492)
- Loading branch information
Showing
22 changed files
with
411 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
58 changes: 58 additions & 0 deletions
58
packages/frontend/core/src/modules/storage/impls/electron.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(); | ||
} | ||
} |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3 changes: 3 additions & 0 deletions
3
packages/frontend/electron/src/main/config-storage/handlers.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
25 changes: 25 additions & 0 deletions
25
packages/frontend/electron/src/main/shared-storage/events.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import type { MainEventRegister } from '../type'; | ||
import { globalCacheStorage, globalStateStorage } from './storage'; | ||
|
||
export const sharedStorageEvents = { | ||
onGlobalStateChanged: ( | ||
fn: (state: Record<string, unknown | undefined>) => void | ||
) => { | ||
const subscription = globalStateStorage.watchAll().subscribe(updates => { | ||
fn(updates); | ||
}); | ||
return () => { | ||
subscription.unsubscribe(); | ||
}; | ||
}, | ||
onGlobalCacheChanged: ( | ||
fn: (state: Record<string, unknown | undefined>) => void | ||
) => { | ||
const subscription = globalCacheStorage.watchAll().subscribe(updates => { | ||
fn(updates); | ||
}); | ||
return () => { | ||
subscription.unsubscribe(); | ||
}; | ||
}, | ||
} satisfies Record<string, MainEventRegister>; |
29 changes: 29 additions & 0 deletions
29
packages/frontend/electron/src/main/shared-storage/handlers.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import type { NamespaceHandlers } from '../type'; | ||
import { globalCacheStorage, globalStateStorage } from './storage'; | ||
|
||
export const sharedStorageHandlers = { | ||
getAllGlobalState: async () => { | ||
return globalStateStorage.all(); | ||
}, | ||
getAllGlobalCache: async () => { | ||
return globalCacheStorage.all(); | ||
}, | ||
setGlobalState: async (_, key: string, value: any) => { | ||
return globalStateStorage.set(key, value); | ||
}, | ||
delGlobalState: async (_, key: string) => { | ||
return globalStateStorage.del(key); | ||
}, | ||
clearGlobalState: async () => { | ||
return globalStateStorage.clear(); | ||
}, | ||
setGlobalCache: async (_, key: string, value: any) => { | ||
return globalCacheStorage.set(key, value); | ||
}, | ||
delGlobalCache: async (_, key: string) => { | ||
return globalCacheStorage.del(key); | ||
}, | ||
clearGlobalCache: async () => { | ||
return globalCacheStorage.clear(); | ||
}, | ||
} satisfies NamespaceHandlers; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export { sharedStorageEvents } from './events'; | ||
export { sharedStorageHandlers } from './handlers'; |
Oops, something went wrong.