|
|
@ -31,6 +31,10 @@ export default class AppStorage<Storage extends Record<string, any>/* Storage ex |
|
|
|
private saveThrottled: () => void; |
|
|
|
private saveThrottled: () => void; |
|
|
|
private saveDeferred = deferredPromise<void>(); |
|
|
|
private saveDeferred = deferredPromise<void>(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private keysToDelete: Set<keyof Storage> = new Set(); |
|
|
|
|
|
|
|
private deleteThrottled: () => void; |
|
|
|
|
|
|
|
private deleteDeferred = deferredPromise<void>(); |
|
|
|
|
|
|
|
|
|
|
|
constructor(storageOptions: Omit<IDBOptions, 'storeName' | 'stores'> & {stores?: DatabaseStore[], storeName: DatabaseStoreName}) { |
|
|
|
constructor(storageOptions: Omit<IDBOptions, 'storeName' | 'stores'> & {stores?: DatabaseStore[], storeName: DatabaseStoreName}) { |
|
|
|
this.storage = new IDBStorage(storageOptions); |
|
|
|
this.storage = new IDBStorage(storageOptions); |
|
|
|
|
|
|
|
|
|
|
@ -40,9 +44,10 @@ export default class AppStorage<Storage extends Record<string, any>/* Storage ex |
|
|
|
const deferred = this.saveDeferred; |
|
|
|
const deferred = this.saveDeferred; |
|
|
|
this.saveDeferred = deferredPromise<void>(); |
|
|
|
this.saveDeferred = deferredPromise<void>(); |
|
|
|
|
|
|
|
|
|
|
|
if(this.keysToSet.size) { |
|
|
|
const set = this.keysToSet; |
|
|
|
const keys = Array.from(this.keysToSet.values()) as string[]; |
|
|
|
if(set.size) { |
|
|
|
this.keysToSet.clear(); |
|
|
|
const keys = Array.from(set.values()) as string[]; |
|
|
|
|
|
|
|
set.clear(); |
|
|
|
|
|
|
|
|
|
|
|
try { |
|
|
|
try { |
|
|
|
//console.log('setItem: will set', key/* , value */);
|
|
|
|
//console.log('setItem: will set', key/* , value */);
|
|
|
@ -58,11 +63,34 @@ export default class AppStorage<Storage extends Record<string, any>/* Storage ex |
|
|
|
|
|
|
|
|
|
|
|
deferred.resolve(); |
|
|
|
deferred.resolve(); |
|
|
|
|
|
|
|
|
|
|
|
if(this.keysToSet.size) { |
|
|
|
if(set.size) { |
|
|
|
this.saveThrottled(); |
|
|
|
this.saveThrottled(); |
|
|
|
} |
|
|
|
} |
|
|
|
}, 16, false); |
|
|
|
}, 16, false); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
this.deleteThrottled = throttle(async() => { |
|
|
|
|
|
|
|
const deferred = this.deleteDeferred; |
|
|
|
|
|
|
|
this.deleteDeferred = deferredPromise<void>(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const set = this.keysToDelete; |
|
|
|
|
|
|
|
if(set.size) { |
|
|
|
|
|
|
|
const keys = Array.from(set.values()) as string[]; |
|
|
|
|
|
|
|
set.clear(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
try { |
|
|
|
|
|
|
|
await this.storage.delete(keys); |
|
|
|
|
|
|
|
} catch(e) { |
|
|
|
|
|
|
|
console.error('[AS]: delete error:', e, keys); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
deferred.resolve(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if(set.size) { |
|
|
|
|
|
|
|
this.deleteThrottled(); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
}, 16, false); |
|
|
|
|
|
|
|
|
|
|
|
this.getThrottled = throttle(async() => { |
|
|
|
this.getThrottled = throttle(async() => { |
|
|
|
const keys = Array.from(this.getPromises.keys()); |
|
|
|
const keys = Array.from(this.getPromises.keys()); |
|
|
|
|
|
|
|
|
|
|
@ -161,6 +189,7 @@ export default class AppStorage<Storage extends Record<string, any>/* Storage ex |
|
|
|
|
|
|
|
|
|
|
|
if(this.useStorage && !onlyLocal) { |
|
|
|
if(this.useStorage && !onlyLocal) { |
|
|
|
this.keysToSet.add(key); |
|
|
|
this.keysToSet.add(key); |
|
|
|
|
|
|
|
this.keysToDelete.delete(key); |
|
|
|
this.saveThrottled(); |
|
|
|
this.saveThrottled(); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
@ -169,7 +198,7 @@ export default class AppStorage<Storage extends Record<string, any>/* Storage ex |
|
|
|
return this.useStorage ? this.saveDeferred : Promise.resolve(); |
|
|
|
return this.useStorage ? this.saveDeferred : Promise.resolve(); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public async delete(key: keyof Storage, saveLocal = false) { |
|
|
|
public delete(key: keyof Storage, saveLocal = false) { |
|
|
|
/* if(!this.cache.hasOwnProperty(key)) { |
|
|
|
/* if(!this.cache.hasOwnProperty(key)) { |
|
|
|
return; |
|
|
|
return; |
|
|
|
} */ |
|
|
|
} */ |
|
|
@ -183,14 +212,11 @@ export default class AppStorage<Storage extends Record<string, any>/* Storage ex |
|
|
|
|
|
|
|
|
|
|
|
if(this.useStorage) { |
|
|
|
if(this.useStorage) { |
|
|
|
this.keysToSet.delete(key); |
|
|
|
this.keysToSet.delete(key); |
|
|
|
|
|
|
|
this.keysToDelete.add(key); |
|
|
|
try { |
|
|
|
this.deleteThrottled(); |
|
|
|
await this.storage.delete(key as string); |
|
|
|
|
|
|
|
} catch(e) { |
|
|
|
|
|
|
|
this.useStorage = false; |
|
|
|
|
|
|
|
console.error('[AS]: remove error:', e); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return this.useStorage ? this.deleteDeferred : Promise.resolve(); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public clear() { |
|
|
|
public clear() { |
|
|
@ -203,6 +229,7 @@ export default class AppStorage<Storage extends Record<string, any>/* Storage ex |
|
|
|
|
|
|
|
|
|
|
|
if(!enabled) { |
|
|
|
if(!enabled) { |
|
|
|
storage.keysToSet.clear(); |
|
|
|
storage.keysToSet.clear(); |
|
|
|
|
|
|
|
storage.keysToDelete.clear(); |
|
|
|
storage.getPromises.forEach((deferred) => deferred.resolve()); |
|
|
|
storage.getPromises.forEach((deferred) => deferred.resolve()); |
|
|
|
storage.getPromises.clear(); |
|
|
|
storage.getPromises.clear(); |
|
|
|
return storage.clear(); |
|
|
|
return storage.clear(); |
|
|
|