refactor(client): typed localStorage

This commit is contained in:
syuilo
2023-01-07 10:13:02 +09:00
parent a42b03c154
commit 91503405b4
25 changed files with 157 additions and 88 deletions

View File

@@ -86,6 +86,14 @@ export const defaultStore = markRaw(new Storage('base', {
where: 'account',
default: [] as string[],
},
latestDonateDialogShowAt: {
where: 'account',
default: null,
},
neverShowDonateDialog: {
where: 'account',
default: false,
},
menu: {
where: 'deviceAccount',
@@ -274,7 +282,7 @@ export const defaultStore = markRaw(new Storage('base', {
// TODO: 他のタブと永続化されたstateを同期
const PREFIX = 'miux:';
const PREFIX = 'miux:' as const;
type Plugin = {
id: string;
@@ -296,6 +304,7 @@ interface Watcher {
import lightTheme from '@/themes/l-light.json5';
import darkTheme from '@/themes/d-green-lime.json5';
import { Note, UserDetailed } from 'misskey-js/built/entities';
import { miLocalStorage } from './local-storage';
export class ColdDeviceStorage {
public static default = {
@@ -320,7 +329,7 @@ export class ColdDeviceStorage {
// TODO: indexedDBにする
// ただしその際はnullチェックではなくキー存在チェックにしないとダメ
// (indexedDBはnullを保存できるため、ユーザーが意図してnullを格納した可能性がある)
const value = localStorage.getItem(PREFIX + key);
const value = miLocalStorage.getItem(`${PREFIX}${key}`);
if (value == null) {
return ColdDeviceStorage.default[key];
} else {
@@ -330,14 +339,14 @@ export class ColdDeviceStorage {
public static set<T extends keyof typeof ColdDeviceStorage.default>(key: T, value: typeof ColdDeviceStorage.default[T]): void {
// 呼び出し側のバグ等で undefined が来ることがある
// undefined を文字列として localStorage に入れると参照する際の JSON.parse でコケて不具合の元になるため無視
// undefined を文字列として miLocalStorage に入れると参照する際の JSON.parse でコケて不具合の元になるため無視
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
if (value === undefined) {
console.error(`attempt to store undefined value for key '${key}'`);
return;
}
localStorage.setItem(PREFIX + key, JSON.stringify(value));
miLocalStorage.setItem(`${PREFIX}${key}`, JSON.stringify(value));
for (const watcher of this.watchers) {
if (watcher.key === key) watcher.callback(value);