refactor
This commit is contained in:
parent
19ef6c0b14
commit
bf6e218355
|
@ -155,7 +155,7 @@ function normalizePreferences(preferences: PossiblyNonNormalizedPreferencesProfi
|
||||||
}
|
}
|
||||||
continue;
|
continue;
|
||||||
} else {
|
} 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({
|
data[key] = records.concat([[makeScope({
|
||||||
server: host,
|
server: host,
|
||||||
account: account.id,
|
account: account.id,
|
||||||
|
@ -233,6 +233,7 @@ export class PreferencesManager {
|
||||||
|
|
||||||
// TODO: desync対策 cloudの値のfetchが正常に完了していない状態でcommitすると多分値が上書きされる
|
// TODO: desync対策 cloudの値のfetchが正常に完了していない状態でcommitすると多分値が上書きされる
|
||||||
public commit<K extends keyof PREF>(key: K, value: ValueOf<K>) {
|
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のプロキシ解除
|
const v = JSON.parse(JSON.stringify(value)); // deep copy 兼 vueのプロキシ解除
|
||||||
|
|
||||||
if (deepEqual(this.s[key], v)) {
|
if (deepEqual(this.s[key], v)) {
|
||||||
|
@ -246,10 +247,10 @@ export class PreferencesManager {
|
||||||
|
|
||||||
const record = this.getMatchedRecordOf(key);
|
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({
|
this.profile.preferences[key].push([makeScope({
|
||||||
server: host,
|
server: host,
|
||||||
account: this.currentAccount!.id,
|
account: currentAccount.id,
|
||||||
}), v, {}]);
|
}), v, {}]);
|
||||||
this.save();
|
this.save();
|
||||||
return;
|
return;
|
||||||
|
@ -360,11 +361,20 @@ export class PreferencesManager {
|
||||||
}
|
}
|
||||||
|
|
||||||
public getMatchedRecordOf<K extends keyof PREF>(key: K): PrefRecord<K> {
|
public getMatchedRecordOf<K extends keyof PREF>(key: K): PrefRecord<K> {
|
||||||
|
const currentAccount = this.currentAccount; // TSを黙らせるため
|
||||||
|
|
||||||
const records = this.profile.preferences[key];
|
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);
|
||||||
|
|
||||||
const accountOverrideRecord = records.find(([scope, v]) => parseScope(scope).server === host && parseScope(scope).account === this.currentAccount!.id);
|
// 設計上あり得ないけどTSに怒られるため
|
||||||
|
if (record == null) throw new Error(`no record found for key: ${key}`);
|
||||||
|
|
||||||
|
return record;
|
||||||
|
}
|
||||||
|
|
||||||
|
const accountOverrideRecord = records.find(([scope, v]) => parseScope(scope).server === host && parseScope(scope).account === currentAccount.id);
|
||||||
if (accountOverrideRecord) return accountOverrideRecord;
|
if (accountOverrideRecord) return accountOverrideRecord;
|
||||||
|
|
||||||
const serverOverrideRecord = records.find(([scope, v]) => parseScope(scope).server === host && parseScope(scope).account == null);
|
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);
|
const record = records.find(([scope, v]) => parseScope(scope).account == null);
|
||||||
|
|
||||||
if (record == null) { // 設計上あり得ないけどTSに怒られるため
|
// 設計上あり得ないけどTSに怒られるため
|
||||||
throw new Error(`no record found for key: ${key}`);
|
if (record == null) throw new Error(`no record found for key: ${key}`);
|
||||||
}
|
|
||||||
|
|
||||||
return record;
|
return record;
|
||||||
}
|
}
|
||||||
|
|
||||||
public isAccountOverrided<K extends keyof PREF>(key: K): boolean {
|
public isAccountOverrided<K extends keyof PREF>(key: K): boolean {
|
||||||
if (this.currentAccount == null) return false;
|
const currentAccount = this.currentAccount; // TSを黙らせるため
|
||||||
return this.profile.preferences[key].some(([scope, v]) => parseScope(scope).server === host && parseScope(scope).account === this.currentAccount!.id);
|
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) {
|
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 (isAccountDependentKey(key)) throw new Error('already account-dependent');
|
||||||
if (this.isAccountOverrided(key)) return;
|
if (this.isAccountOverrided(key)) return;
|
||||||
|
|
||||||
const records = this.profile.preferences[key];
|
const records = this.profile.preferences[key];
|
||||||
records.push([makeScope({
|
records.push([makeScope({
|
||||||
server: host,
|
server: host,
|
||||||
account: this.currentAccount!.id,
|
account: currentAccount.id,
|
||||||
}), this.s[key], {}]);
|
}), this.s[key], {}]);
|
||||||
|
|
||||||
this.save();
|
this.save();
|
||||||
}
|
}
|
||||||
|
|
||||||
public clearAccountOverride<K extends keyof PREF>(key: K) {
|
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');
|
if (isAccountDependentKey(key)) throw new Error('cannot clear override for this account-dependent property');
|
||||||
|
|
||||||
const records = this.profile.preferences[key];
|
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;
|
if (index === -1) return;
|
||||||
|
|
||||||
records.splice(index, 1);
|
records.splice(index, 1);
|
||||||
|
|
Loading…
Reference in New Issue