Skip to content

Commit

Permalink
refactor(infra): memento use undefined
Browse files Browse the repository at this point in the history
  • Loading branch information
EYHN committed Jul 12, 2024
1 parent 5ede985 commit dcae15a
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 21 deletions.
8 changes: 4 additions & 4 deletions packages/common/infra/src/modules/workspace/impls/storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 19,15 @@ export class WorkspaceLocalStateImpl implements WorkspaceLocalState {
return this.wrapped.keys();
}

get<T>(key: string): T | null {
get<T>(key: string): T | undefined {
return this.wrapped.get<T>(key);
}

watch<T>(key: string) {
return this.wrapped.watch<T>(key);
}

set<T>(key: string, value: T | null): void {
set<T>(key: string, value: T): void {
return this.wrapped.set<T>(key, value);
}

Expand All @@ -53,15 53,15 @@ export class WorkspaceLocalCacheImpl implements WorkspaceLocalCache {
return this.wrapped.keys();
}

get<T>(key: string): T | null {
get<T>(key: string): T | undefined {
return this.wrapped.get<T>(key);
}

watch<T>(key: string) {
return this.wrapped.watch<T>(key);
}

set<T>(key: string, value: T | null): void {
set<T>(key: string, value: T): void {
return this.wrapped.set<T>(key, value);
}

Expand Down
18 changes: 9 additions & 9 deletions packages/common/infra/src/storage/memento.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 6,9 @@ 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;
get<T>(key: string): T | undefined;
watch<T>(key: string): Observable<T | undefined>;
set<T>(key: string, value: T | undefined): void;
del(key: string): void;
clear(): void;
keys(): string[];
Expand All @@ -23,19 23,19 @@ export class MemoryMemento implements Memento {
private getLiveData(key: string): LiveData<any> {
let data$ = this.data.get(key);
if (!data$) {
data$ = new LiveData<any>(null);
data$ = new LiveData<any>(undefined);
this.data.set(key, data$);
}
return data$;
}

get<T>(key: string): T | null {
get<T>(key: string): T | undefined {
return this.getLiveData(key).value;
}
watch<T>(key: string): Observable<T | null> {
watch<T>(key: string): Observable<T | undefined> {
return this.getLiveData(key).asObservable();
}
set<T>(key: string, value: T | null): void {
set<T>(key: string, value: T): void {
this.getLiveData(key).next(value);
}
keys(): string[] {
Expand All @@ -51,13 51,13 @@ export class MemoryMemento implements Memento {

export function wrapMemento(memento: Memento, prefix: string): Memento {
return {
get<T>(key: string): T | null {
get<T>(key: string): T | undefined {
return memento.get(prefix key);
},
watch(key: string) {
return memento.watch(prefix key);
},
set<T>(key: string, value: T | null): void {
set<T>(key: string, value: T): void {
memento.set(prefix key, value);
},
keys(): string[] {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 84,8 @@ export function AffinePageReference({
const t = useI18n();

const docsService = useService(DocsService);
const mode$ = LiveData.from(docsService.list.observeMode(pageId), null);
const docMode = useLiveData(mode$);
const mode$ = LiveData.from(docsService.list.observeMode(pageId), undefined);
const docMode = useLiveData(mode$) ?? null;
const el = pageReferenceRenderer({
docMode,
pageId,
Expand Down
12 changes: 6 additions & 6 deletions packages/frontend/core/src/modules/storage/impls/storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 15,14 @@ export class LocalStorageMemento implements Memento {
return keys;
}

get<T>(key: string): T | null {
get<T>(key: string): T | undefined {
const json = localStorage.getItem(this.prefix key);
return json ? JSON.parse(json) : null;
return json ? JSON.parse(json) : undefined;
}
watch<T>(key: string): Observable<T | null> {
return new Observable<T | null>(subscriber => {
watch<T>(key: string): Observable<T | undefined> {
return new Observable<T | undefined>(subscriber => {
const json = localStorage.getItem(this.prefix key);
const first = json ? JSON.parse(json) : null;
const first = json ? JSON.parse(json) : undefined;
subscriber.next(first);

const channel = new BroadcastChannel(this.prefix key);
Expand All @@ -34,7 34,7 @@ export class LocalStorageMemento implements Memento {
};
});
}
set<T>(key: string, value: T | null): void {
set<T>(key: string, value: T): void {
localStorage.setItem(this.prefix key, JSON.stringify(value));
const channel = new BroadcastChannel(this.prefix key);
channel.postMessage(value);
Expand Down

0 comments on commit dcae15a

Please sign in to comment.