|
|
|
@@ -84,6 +84,12 @@ export type StorageProvider = {
|
|
|
|
|
cloudSet: <K extends keyof PREF>(ctx: { key: K; cond: Cond; value: ValueOf<K>; }) => Promise<void>;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export type PreferencesDefinition = Record<string, {
|
|
|
|
|
default: any;
|
|
|
|
|
accountDependent?: boolean;
|
|
|
|
|
serverDependent?: boolean;
|
|
|
|
|
}>;
|
|
|
|
|
|
|
|
|
|
export class ProfileManager {
|
|
|
|
|
private storageProvider: StorageProvider;
|
|
|
|
|
public profile: PreferencesProfile;
|
|
|
|
@@ -118,6 +124,10 @@ export class ProfileManager {
|
|
|
|
|
// TODO: 定期的にクラウドの値をフェッチ
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private isAccountDependentKey<K extends keyof PREF>(key: K): boolean {
|
|
|
|
|
return (PREF_DEF as PreferencesDefinition)[key].accountDependent === true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private rewriteRawState<K extends keyof PREF>(key: K, value: ValueOf<K>) {
|
|
|
|
|
const v = JSON.parse(JSON.stringify(value)); // deep copy 兼 vueのプロキシ解除
|
|
|
|
|
this.r[key].value = this.s[key] = v;
|
|
|
|
@@ -129,7 +139,7 @@ export class ProfileManager {
|
|
|
|
|
this.rewriteRawState(key, value);
|
|
|
|
|
|
|
|
|
|
const record = this.getMatchedRecordOf(key);
|
|
|
|
|
if (parseCond(record[0]).account == null && PREF_DEF[key].accountDependent) {
|
|
|
|
|
if (parseCond(record[0]).account == null && this.isAccountDependentKey(key)) {
|
|
|
|
|
this.profile.preferences[key].push([makeCond({
|
|
|
|
|
account: `${host}/${$i!.id}`,
|
|
|
|
|
}), value, {}]);
|
|
|
|
@@ -186,9 +196,10 @@ export class ProfileManager {
|
|
|
|
|
|
|
|
|
|
private genStates() {
|
|
|
|
|
const states = {} as { [K in keyof PREF]: ValueOf<K> };
|
|
|
|
|
for (const key in PREF_DEF) {
|
|
|
|
|
for (const _key in PREF_DEF) {
|
|
|
|
|
const key = _key as keyof PREF;
|
|
|
|
|
const record = this.getMatchedRecordOf(key);
|
|
|
|
|
states[key] = record[1];
|
|
|
|
|
(states[key] as any) = record[1];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return states;
|
|
|
|
@@ -196,7 +207,8 @@ export class ProfileManager {
|
|
|
|
|
|
|
|
|
|
private async fetchCloudValues() {
|
|
|
|
|
const needs = [] as { key: keyof PREF; cond: Cond; }[];
|
|
|
|
|
for (const key in PREF_DEF) {
|
|
|
|
|
for (const _key in PREF_DEF) {
|
|
|
|
|
const key = _key as keyof PREF;
|
|
|
|
|
const record = this.getMatchedRecordOf(key);
|
|
|
|
|
if (record[2].sync) {
|
|
|
|
|
needs.push({
|
|
|
|
@@ -208,7 +220,8 @@ export class ProfileManager {
|
|
|
|
|
|
|
|
|
|
const cloudValues = await this.storageProvider.cloudGets({ needs });
|
|
|
|
|
|
|
|
|
|
for (const key in PREF_DEF) {
|
|
|
|
|
for (const _key in PREF_DEF) {
|
|
|
|
|
const key = _key as keyof PREF;
|
|
|
|
|
const record = this.getMatchedRecordOf(key);
|
|
|
|
|
if (record[2].sync && Object.hasOwn(cloudValues, key) && cloudValues[key] !== undefined) {
|
|
|
|
|
const cloudValue = cloudValues[key];
|
|
|
|
@@ -290,7 +303,7 @@ export class ProfileManager {
|
|
|
|
|
|
|
|
|
|
public setAccountOverride<K extends keyof PREF>(key: K) {
|
|
|
|
|
if ($i == null) return;
|
|
|
|
|
if (PREF_DEF[key].accountDependent) throw new Error('already account-dependent');
|
|
|
|
|
if (this.isAccountDependentKey(key)) throw new Error('already account-dependent');
|
|
|
|
|
if (this.isAccountOverrided(key)) return;
|
|
|
|
|
|
|
|
|
|
const records = this.profile.preferences[key];
|
|
|
|
@@ -303,7 +316,7 @@ export class ProfileManager {
|
|
|
|
|
|
|
|
|
|
public clearAccountOverride<K extends keyof PREF>(key: K) {
|
|
|
|
|
if ($i == null) return;
|
|
|
|
|
if (PREF_DEF[key].accountDependent) throw new Error('cannot clear override for this account-dependent property');
|
|
|
|
|
if (this.isAccountDependentKey(key)) throw new Error('cannot clear override for this account-dependent property');
|
|
|
|
|
|
|
|
|
|
const records = this.profile.preferences[key];
|
|
|
|
|
|
|
|
|
@@ -377,7 +390,8 @@ export class ProfileManager {
|
|
|
|
|
public rewriteProfile(profile: PreferencesProfile) {
|
|
|
|
|
this.profile = profile;
|
|
|
|
|
const states = this.genStates();
|
|
|
|
|
for (const key in states) {
|
|
|
|
|
for (const _key in states) {
|
|
|
|
|
const key = _key as keyof PREF;
|
|
|
|
|
this.rewriteRawState(key, states[key]);
|
|
|
|
|
}
|
|
|
|
|
|