diff options
| author | syuilo <4439005+syuilo@users.noreply.github.com> | 2025-06-28 12:00:15 +0900 |
|---|---|---|
| committer | syuilo <4439005+syuilo@users.noreply.github.com> | 2025-06-28 12:00:15 +0900 |
| commit | bf6e21835504a8fb67d1c245626d72e28335608e (patch) | |
| tree | 0903db91f746cf171b9d3f6e34b2723da1eb3580 /packages/frontend/src/preferences/manager.ts | |
| parent | Update about-misskey.vue (diff) | |
| download | misskey-bf6e21835504a8fb67d1c245626d72e28335608e.tar.gz misskey-bf6e21835504a8fb67d1c245626d72e28335608e.tar.bz2 misskey-bf6e21835504a8fb67d1c245626d72e28335608e.zip | |
refactor
Diffstat (limited to 'packages/frontend/src/preferences/manager.ts')
| -rw-r--r-- | packages/frontend/src/preferences/manager.ts | 40 |
1 files changed, 26 insertions, 14 deletions
diff --git a/packages/frontend/src/preferences/manager.ts b/packages/frontend/src/preferences/manager.ts index 51272f45f4..0389cf612a 100644 --- a/packages/frontend/src/preferences/manager.ts +++ b/packages/frontend/src/preferences/manager.ts @@ -155,7 +155,7 @@ function normalizePreferences(preferences: PossiblyNonNormalizedPreferencesProfi } continue; } else { - if (account && isAccountDependentKey(key as keyof typeof PREF_DEF) && !records.some(([scope]) => parseScope(scope).server === host && parseScope(scope).account === account!.id)) { + if (account && isAccountDependentKey(key as keyof typeof PREF_DEF) && !records.some(([scope]) => parseScope(scope).server === host && parseScope(scope).account === account.id)) { data[key] = records.concat([[makeScope({ server: host, account: account.id, @@ -233,6 +233,7 @@ export class PreferencesManager { // TODO: desync対策 cloudの値のfetchが正常に完了していない状態でcommitすると多分値が上書きされる public commit<K extends keyof PREF>(key: K, value: ValueOf<K>) { + const currentAccount = this.currentAccount; // TSを黙らせるため const v = JSON.parse(JSON.stringify(value)); // deep copy 兼 vueのプロキシ解除 if (deepEqual(this.s[key], v)) { @@ -246,10 +247,10 @@ export class PreferencesManager { const record = this.getMatchedRecordOf(key); - if (parseScope(record[0]).account == null && isAccountDependentKey(key)) { + if (parseScope(record[0]).account == null && isAccountDependentKey(key) && currentAccount != null) { this.profile.preferences[key].push([makeScope({ server: host, - account: this.currentAccount!.id, + account: currentAccount.id, }), v, {}]); this.save(); return; @@ -360,11 +361,20 @@ export class PreferencesManager { } public getMatchedRecordOf<K extends keyof PREF>(key: K): PrefRecord<K> { + const currentAccount = this.currentAccount; // TSを黙らせるため + const records = this.profile.preferences[key]; - if (this.currentAccount == null) return records.find(([scope, v]) => parseScope(scope).account == null)!; + if (currentAccount == null) { + const record = records.find(([scope, v]) => parseScope(scope).account == null); + + // 設計上あり得ないけどTSに怒られるため + if (record == null) throw new Error(`no record found for key: ${key}`); - const accountOverrideRecord = records.find(([scope, v]) => parseScope(scope).server === host && parseScope(scope).account === this.currentAccount!.id); + return record; + } + + const accountOverrideRecord = records.find(([scope, v]) => parseScope(scope).server === host && parseScope(scope).account === currentAccount.id); if (accountOverrideRecord) return accountOverrideRecord; const serverOverrideRecord = records.find(([scope, v]) => parseScope(scope).server === host && parseScope(scope).account == null); @@ -372,39 +382,41 @@ export class PreferencesManager { const record = records.find(([scope, v]) => parseScope(scope).account == null); - if (record == null) { // 設計上あり得ないけどTSに怒られるため - throw new Error(`no record found for key: ${key}`); - } + // 設計上あり得ないけどTSに怒られるため + if (record == null) throw new Error(`no record found for key: ${key}`); return record; } public isAccountOverrided<K extends keyof PREF>(key: K): boolean { - if (this.currentAccount == null) return false; - return this.profile.preferences[key].some(([scope, v]) => parseScope(scope).server === host && parseScope(scope).account === this.currentAccount!.id); + const currentAccount = this.currentAccount; // TSを黙らせるため + if (currentAccount == null) return false; + return this.profile.preferences[key].some(([scope, v]) => parseScope(scope).server === host && parseScope(scope).account === currentAccount.id); } public setAccountOverride<K extends keyof PREF>(key: K) { - if (this.currentAccount == null) return; + const currentAccount = this.currentAccount; // TSを黙らせるため + if (currentAccount == null) return; if (isAccountDependentKey(key)) throw new Error('already account-dependent'); if (this.isAccountOverrided(key)) return; const records = this.profile.preferences[key]; records.push([makeScope({ server: host, - account: this.currentAccount!.id, + account: currentAccount.id, }), this.s[key], {}]); this.save(); } public clearAccountOverride<K extends keyof PREF>(key: K) { - if (this.currentAccount == null) return; + const currentAccount = this.currentAccount; // TSを黙らせるため + if (currentAccount == null) return; if (isAccountDependentKey(key)) throw new Error('cannot clear override for this account-dependent property'); const records = this.profile.preferences[key]; - const index = records.findIndex(([scope, v]) => parseScope(scope).server === host && parseScope(scope).account === this.currentAccount!.id); + const index = records.findIndex(([scope, v]) => parseScope(scope).server === host && parseScope(scope).account === currentAccount.id); if (index === -1) return; records.splice(index, 1); |