This commit is contained in:
tamaina 2021-12-30 02:03:39 +09:00
parent 9e6d13ce46
commit 14e74beba1
1 changed files with 9 additions and 7 deletions

View File

@ -117,35 +117,37 @@ export class Storage<T extends StateDef> {
} }
public set<K extends keyof T>(key: K, value: T[K]['default']): Promise<void> { public set<K extends keyof T>(key: K, value: T[K]['default']): Promise<void> {
if (_DEV_) console.log('set', key, value); const rawValue = JSON.parse(JSON.stringify(value));
this.state[key] = value; if (_DEV_) console.log('set', key, rawValue, value);
this.reactiveState[key].value = value;
this.state[key] = rawValue;
this.reactiveState[key].value = rawValue;
return this.addIdbSetJob(async () => { return this.addIdbSetJob(async () => {
switch (this.def[key].where) { switch (this.def[key].where) {
case 'device': { case 'device': {
const deviceState = await get(this.deviceStateKeyName) || {}; const deviceState = await get(this.deviceStateKeyName) || {};
deviceState[key] = value; deviceState[key] = rawValue;
await set(this.deviceStateKeyName, deviceState); await set(this.deviceStateKeyName, deviceState);
break; break;
} }
case 'deviceAccount': { case 'deviceAccount': {
if ($i == null) break; if ($i == null) break;
const deviceAccountState = await get(this.deviceAccountStateKeyName) || {}; const deviceAccountState = await get(this.deviceAccountStateKeyName) || {};
deviceAccountState[key] = value; deviceAccountState[key] = rawValue;
await set(this.deviceAccountStateKeyName, deviceAccountState); await set(this.deviceAccountStateKeyName, deviceAccountState);
break; break;
} }
case 'account': { case 'account': {
if ($i == null) break; if ($i == null) break;
const cache = await get(this.registryCacheKeyName) || {}; const cache = await get(this.registryCacheKeyName) || {};
cache[key] = value; cache[key] = rawValue;
await set(this.registryCacheKeyName, cache); await set(this.registryCacheKeyName, cache);
await api('i/registry/set', { await api('i/registry/set', {
scope: ['client', this.key], scope: ['client', this.key],
key: key, key: key,
value: value value: rawValue
}); });
break; break;
} }