diff --git a/packages/client/src/pizzax.ts b/packages/client/src/pizzax.ts index 1ebff07fa7..3ac4c5c720 100644 --- a/packages/client/src/pizzax.ts +++ b/packages/client/src/pizzax.ts @@ -30,6 +30,7 @@ export class Storage { public readonly state = {} as State; public readonly reactiveState = {} as ReactiveState; + // indexedDB保存を重複させないために簡易的にキューイング private nextIdbJob: Promise = Promise.resolve(); private addIdbSetJob(job: () => Promise) { const promise = this.nextIdbJob.then(job, e => { @@ -52,6 +53,8 @@ export class Storage { private init(): Promise { return new Promise(async (resolve, reject) => { + await this.migrate(); + const deviceState: State = await get(this.deviceStateKeyName); const deviceAccountState = $i ? await get(this.deviceAccountStateKeyName) : {}; const registryCache = $i ? await get(this.registryCacheKeyName) : {}; @@ -95,6 +98,7 @@ export class Storage { }) .then(() => resolve()); }, 1); + // streamingのuser storage updateイベントを監視して更新 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; @@ -190,4 +194,25 @@ export class Storage { } }; } + + // 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); + } + } }