1
0
forked from mirror/misskey
This commit is contained in:
tamaina 2021-12-30 00:34:23 +09:00
parent 7b7368b815
commit 41a7ab5024

View File

@ -30,6 +30,7 @@ export class Storage<T extends StateDef> {
public readonly state = {} as State<T>; public readonly state = {} as State<T>;
public readonly reactiveState = {} as ReactiveState<T>; public readonly reactiveState = {} as ReactiveState<T>;
// indexedDB保存を重複させないために簡易的にキューイング
private nextIdbJob: Promise<any> = Promise.resolve(); private nextIdbJob: Promise<any> = Promise.resolve();
private addIdbSetJob<T>(job: () => Promise<T>) { private addIdbSetJob<T>(job: () => Promise<T>) {
const promise = this.nextIdbJob.then(job, e => { const promise = this.nextIdbJob.then(job, e => {
@ -52,6 +53,8 @@ export class Storage<T extends StateDef> {
private init(): Promise<void> { private init(): Promise<void> {
return new Promise(async (resolve, reject) => { return new Promise(async (resolve, reject) => {
await this.migrate();
const deviceState: State<T> = await get(this.deviceStateKeyName); const deviceState: State<T> = await get(this.deviceStateKeyName);
const deviceAccountState = $i ? await get(this.deviceAccountStateKeyName) : {}; const deviceAccountState = $i ? await get(this.deviceAccountStateKeyName) : {};
const registryCache = $i ? await get(this.registryCacheKeyName) : {}; const registryCache = $i ? await get(this.registryCacheKeyName) : {};
@ -95,6 +98,7 @@ export class Storage<T extends StateDef> {
}) })
.then(() => resolve()); .then(() => resolve());
}, 1); }, 1);
// streamingのuser storage updateイベントを監視して更新 // streamingのuser storage updateイベントを監視して更新
connection?.on('registryUpdated', async ({ scope, key, value }: { scope: string[], key: keyof T, value: T[typeof key]['default'] }) => { connection?.on('registryUpdated', async ({ scope, key, value }: { scope: string[], key: keyof T, value: T[typeof key]['default'] }) => {
if (scope[1] !== this.key || this.state[key] === value) return; if (scope[1] !== this.key || this.state[key] === value) return;
@ -190,4 +194,25 @@ export class Storage<T extends StateDef> {
} }
}; };
} }
// localStorage => indexedDBのマイグレーション
private async migrate() {
const deviceState = localStorage.getItem(this.deviceStateKeyName);
if (deviceState) {
await set(this.deviceStateKeyName, JSON.parse(deviceState));
localStorage.removeItem(this.deviceStateKeyName);
}
const deviceAccountState = $i && localStorage.getItem(this.deviceAccountStateKeyName);
if ($i && deviceAccountState) {
await set(this.deviceAccountStateKeyName, JSON.parse(deviceAccountState));
localStorage.removeItem(this.deviceAccountStateKeyName);
}
const registryCache = $i && localStorage.getItem(this.registryCacheKeyName);
if ($i && registryCache) {
await set(this.registryCacheKeyName, JSON.parse(registryCache));
localStorage.removeItem(this.registryCacheKeyName);
}
}
} }