refactor(core): refactor atom to use di (#5831)

To support multiple instances, this PR removes some atoms and implements them using the new DI system.

removed atom

- `pageSettingsAtom`
- `currentPageIdAtom`
- `currentModeAtom`
This commit is contained in:
EYHN
2024-02-27 03:50:53 +00:00
parent 0dabb08217
commit ad9b0303c4
60 changed files with 602 additions and 626 deletions

View File

@@ -1,8 +1,8 @@
import type { GlobalCache } from '@toeverything/infra';
import type { GlobalCache, GlobalState, Memento } from '@toeverything/infra';
import { Observable } from 'rxjs';
export class LocalStorageGlobalCache implements GlobalCache {
prefix = 'cache:';
export class LocalStorageMemento implements Memento {
constructor(private readonly prefix: string) {}
get<T>(key: string): T | null {
const json = localStorage.getItem(this.prefix + key);
@@ -30,3 +30,21 @@ export class LocalStorageGlobalCache implements GlobalCache {
channel.close();
}
}
export class LocalStorageGlobalCache
extends LocalStorageMemento
implements GlobalCache
{
constructor() {
super('global-cache:');
}
}
export class LocalStorageGlobalState
extends LocalStorageMemento
implements GlobalState
{
constructor() {
super('global-state:');
}
}

View File

@@ -1,12 +1,16 @@
import {
GlobalCache,
GlobalState,
type ServiceCollection,
Workspace,
WorkspaceScope,
} from '@toeverything/infra';
import { CollectionService } from './collection';
import { LocalStorageGlobalCache } from './infra-web/storage';
import {
LocalStorageGlobalCache,
LocalStorageGlobalState,
} from './infra-web/storage';
import { CurrentPageService } from './page';
import {
CurrentWorkspaceService,
@@ -25,5 +29,7 @@ export function configureBusinessServices(services: ServiceCollection) {
}
export function configureWebInfraServices(services: ServiceCollection) {
services.addImpl(GlobalCache, LocalStorageGlobalCache);
services
.addImpl(GlobalCache, LocalStorageGlobalCache)
.addImpl(GlobalState, LocalStorageGlobalState);
}