From 4f95b8d9d2dec6dd1e9069372abd7cf605e63d9a Mon Sep 17 00:00:00 2001 From: かっこかり <67428053+kakkokari-gtyih@users.noreply.github.com> Date: Mon, 22 Jan 2024 09:20:56 +0900 Subject: fix(frontend/pizzax): デフォルト値が適用できないことがあるのを修正 (#13057) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * fix(frontend/pizzax): デフォルト値が適用できないことがあるのを修正 * fix * いらんプロパティをけす --- packages/frontend/src/pizzax.ts | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) (limited to 'packages/frontend/src/pizzax.ts') diff --git a/packages/frontend/src/pizzax.ts b/packages/frontend/src/pizzax.ts index 8723110b08..b3d2374899 100644 --- a/packages/frontend/src/pizzax.ts +++ b/packages/frontend/src/pizzax.ts @@ -7,6 +7,7 @@ import { onUnmounted, Ref, ref, watch } from 'vue'; import { BroadcastChannel } from 'broadcast-channel'; +import { defu } from 'defu'; import { $i } from '@/account.js'; import { misskeyApi } from '@/scripts/misskey-api.js'; import { get, set } from '@/scripts/idb-proxy.js'; @@ -80,6 +81,18 @@ export class Storage { this.loaded = this.ready.then(() => this.load()); } + private isPureObject(value: unknown): value is Record { + return typeof value === 'object' && value !== null && !Array.isArray(value); + } + + private mergeState(value: T, def: T): T { + if (this.isPureObject(value) && this.isPureObject(def)) { + if (_DEV_) console.log('Merging state. Incoming: ', value, ' Default: ', def); + return defu(value, def) as T; + } + return value; + } + private async init(): Promise { await this.migrate(); @@ -89,11 +102,11 @@ export class Storage { for (const [k, v] of Object.entries(this.def) as [keyof T, T[keyof T]['default']][]) { if (v.where === 'device' && Object.prototype.hasOwnProperty.call(deviceState, k)) { - this.reactiveState[k].value = this.state[k] = deviceState[k]; + this.reactiveState[k].value = this.state[k] = this.mergeState(deviceState[k], v.default); } else if (v.where === 'account' && $i && Object.prototype.hasOwnProperty.call(registryCache, k)) { - this.reactiveState[k].value = this.state[k] = registryCache[k]; + this.reactiveState[k].value = this.state[k] = this.mergeState(registryCache[k], v.default); } else if (v.where === 'deviceAccount' && Object.prototype.hasOwnProperty.call(deviceAccountState, k)) { - this.reactiveState[k].value = this.state[k] = deviceAccountState[k]; + this.reactiveState[k].value = this.state[k] = this.mergeState(deviceAccountState[k], v.default); } else { this.reactiveState[k].value = this.state[k] = v.default; if (_DEV_) console.log('Use default value', k, v.default); -- cgit v1.2.3-freya