refactor(infra): memento use undefined (#7491)

This commit is contained in:
EYHN
2024-07-15 02:48:20 +00:00
parent 2f784ae539
commit 0f1409756e
5 changed files with 22 additions and 22 deletions

View File

@@ -19,7 +19,7 @@ export class WorkspaceLocalStateImpl implements WorkspaceLocalState {
return this.wrapped.keys(); return this.wrapped.keys();
} }
get<T>(key: string): T | null { get<T>(key: string): T | undefined {
return this.wrapped.get<T>(key); return this.wrapped.get<T>(key);
} }
@@ -27,7 +27,7 @@ export class WorkspaceLocalStateImpl implements WorkspaceLocalState {
return this.wrapped.watch<T>(key); 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); return this.wrapped.set<T>(key, value);
} }
@@ -53,7 +53,7 @@ export class WorkspaceLocalCacheImpl implements WorkspaceLocalCache {
return this.wrapped.keys(); return this.wrapped.keys();
} }
get<T>(key: string): T | null { get<T>(key: string): T | undefined {
return this.wrapped.get<T>(key); return this.wrapped.get<T>(key);
} }
@@ -61,7 +61,7 @@ export class WorkspaceLocalCacheImpl implements WorkspaceLocalCache {
return this.wrapped.watch<T>(key); 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); return this.wrapped.set<T>(key, value);
} }

View File

@@ -6,7 +6,7 @@ describe('memento', () => {
test('memory', () => { test('memory', () => {
const memento = new MemoryMemento(); const memento = new MemoryMemento();
expect(memento.get('foo')).toBeNull(); expect(memento.get('foo')).toBeUndefined();
memento.set('foo', 'bar'); memento.set('foo', 'bar');
expect(memento.get('foo')).toEqual('bar'); expect(memento.get('foo')).toEqual('bar');

View File

@@ -6,9 +6,9 @@ import { LiveData } from '../livedata';
* A memento represents a storage utility. It can store and retrieve values, and observe changes. * A memento represents a storage utility. It can store and retrieve values, and observe changes.
*/ */
export interface Memento { export interface Memento {
get<T>(key: string): T | null; get<T>(key: string): T | undefined;
watch<T>(key: string): Observable<T | null>; watch<T>(key: string): Observable<T | undefined>;
set<T>(key: string, value: T | null): void; set<T>(key: string, value: T | undefined): void;
del(key: string): void; del(key: string): void;
clear(): void; clear(): void;
keys(): string[]; keys(): string[];
@@ -23,19 +23,19 @@ export class MemoryMemento implements Memento {
private getLiveData(key: string): LiveData<any> { private getLiveData(key: string): LiveData<any> {
let data$ = this.data.get(key); let data$ = this.data.get(key);
if (!data$) { if (!data$) {
data$ = new LiveData<any>(null); data$ = new LiveData<any>(undefined);
this.data.set(key, data$); this.data.set(key, data$);
} }
return data$; return data$;
} }
get<T>(key: string): T | null { get<T>(key: string): T | undefined {
return this.getLiveData(key).value; return this.getLiveData(key).value;
} }
watch<T>(key: string): Observable<T | null> { watch<T>(key: string): Observable<T | undefined> {
return this.getLiveData(key).asObservable(); 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); this.getLiveData(key).next(value);
} }
keys(): string[] { keys(): string[] {
@@ -51,13 +51,13 @@ export class MemoryMemento implements Memento {
export function wrapMemento(memento: Memento, prefix: string): Memento { export function wrapMemento(memento: Memento, prefix: string): Memento {
return { return {
get<T>(key: string): T | null { get<T>(key: string): T | undefined {
return memento.get(prefix + key); return memento.get(prefix + key);
}, },
watch(key: string) { watch(key: string) {
return memento.watch(prefix + key); 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); memento.set(prefix + key, value);
}, },
keys(): string[] { keys(): string[] {

View File

@@ -84,8 +84,8 @@ export function AffinePageReference({
const t = useI18n(); const t = useI18n();
const docsService = useService(DocsService); const docsService = useService(DocsService);
const mode$ = LiveData.from(docsService.list.observeMode(pageId), null); const mode$ = LiveData.from(docsService.list.observeMode(pageId), undefined);
const docMode = useLiveData(mode$); const docMode = useLiveData(mode$) ?? null;
const el = pageReferenceRenderer({ const el = pageReferenceRenderer({
docMode, docMode,
pageId, pageId,

View File

@@ -15,14 +15,14 @@ export class LocalStorageMemento implements Memento {
return keys; return keys;
} }
get<T>(key: string): T | null { get<T>(key: string): T | undefined {
const json = localStorage.getItem(this.prefix + key); 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> { watch<T>(key: string): Observable<T | undefined> {
return new Observable<T | null>(subscriber => { return new Observable<T | undefined>(subscriber => {
const json = localStorage.getItem(this.prefix + key); const json = localStorage.getItem(this.prefix + key);
const first = json ? JSON.parse(json) : null; const first = json ? JSON.parse(json) : undefined;
subscriber.next(first); subscriber.next(first);
const channel = new BroadcastChannel(this.prefix + key); const channel = new BroadcastChannel(this.prefix + key);
@@ -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)); localStorage.setItem(this.prefix + key, JSON.stringify(value));
const channel = new BroadcastChannel(this.prefix + key); const channel = new BroadcastChannel(this.prefix + key);
channel.postMessage(value); channel.postMessage(value);