|
|
@ -14,6 +14,8 @@ import { CancellablePromise, deferredPromise } from "../helpers/cancellablePromi |
|
|
|
import { throttle } from "../helpers/schedulers"; |
|
|
|
import { throttle } from "../helpers/schedulers"; |
|
|
|
import IDBStorage, { IDBOptions } from "./idb"; |
|
|
|
import IDBStorage, { IDBOptions } from "./idb"; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function noop() {} |
|
|
|
|
|
|
|
|
|
|
|
export default class AppStorage<Storage extends Record<string, any>/* Storage extends {[name: string]: any} *//* Storage extends Record<string, any> */> { |
|
|
|
export default class AppStorage<Storage extends Record<string, any>/* Storage extends {[name: string]: any} *//* Storage extends Record<string, any> */> { |
|
|
|
private static STORAGES: AppStorage<any>[] = []; |
|
|
|
private static STORAGES: AppStorage<any>[] = []; |
|
|
|
private storage: IDBStorage;//new CacheStorageController('session');
|
|
|
|
private storage: IDBStorage;//new CacheStorageController('session');
|
|
|
@ -55,6 +57,10 @@ export default class AppStorage<Storage extends Record<string, any>/* Storage ex |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
deferred.resolve(); |
|
|
|
deferred.resolve(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if(this.keysToSet.size) { |
|
|
|
|
|
|
|
this.saveThrottled(); |
|
|
|
|
|
|
|
} |
|
|
|
}, 16, false); |
|
|
|
}, 16, false); |
|
|
|
|
|
|
|
|
|
|
|
this.getThrottled = throttle(async() => { |
|
|
|
this.getThrottled = throttle(async() => { |
|
|
@ -73,21 +79,30 @@ export default class AppStorage<Storage extends Record<string, any>/* Storage ex |
|
|
|
}, (error) => { |
|
|
|
}, (error) => { |
|
|
|
if(!['NO_ENTRY_FOUND', 'STORAGE_OFFLINE'].includes(error)) { |
|
|
|
if(!['NO_ENTRY_FOUND', 'STORAGE_OFFLINE'].includes(error)) { |
|
|
|
this.useStorage = false; |
|
|
|
this.useStorage = false; |
|
|
|
console.error('[AS]: get error:', error, keys); |
|
|
|
console.error('[AS]: get error:', error, keys, storageOptions.storeName); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
for(let i = 0, length = keys.length; i < length; ++i) { |
|
|
|
for(let i = 0, length = keys.length; i < length; ++i) { |
|
|
|
const key = keys[i]; |
|
|
|
const key = keys[i]; |
|
|
|
const deferred = this.getPromises.get(key); |
|
|
|
const deferred = this.getPromises.get(key); |
|
|
|
if(deferred) { |
|
|
|
if(deferred) { |
|
|
|
deferred.reject(error); |
|
|
|
//deferred.reject(error);
|
|
|
|
|
|
|
|
deferred.resolve(); |
|
|
|
this.getPromises.delete(key); |
|
|
|
this.getPromises.delete(key); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
}).finally(() => { |
|
|
|
|
|
|
|
if(this.getPromises.size) { |
|
|
|
|
|
|
|
this.getThrottled(); |
|
|
|
|
|
|
|
} |
|
|
|
}); |
|
|
|
}); |
|
|
|
}, 16, false); |
|
|
|
}, 16, false); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public isAvailable() { |
|
|
|
|
|
|
|
return this.useStorage; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public getCache() { |
|
|
|
public getCache() { |
|
|
|
return this.cache; |
|
|
|
return this.cache; |
|
|
|
} |
|
|
|
} |
|
|
@ -119,7 +134,7 @@ export default class AppStorage<Storage extends Record<string, any>/* Storage ex |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public getAll() { |
|
|
|
public getAll() { |
|
|
|
return this.storage.getAll(); |
|
|
|
return this.storage.getAll().catch(() => []); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public set(obj: Partial<Storage>, onlyLocal = false) { |
|
|
|
public set(obj: Partial<Storage>, onlyLocal = false) { |
|
|
@ -151,7 +166,7 @@ export default class AppStorage<Storage extends Record<string, any>/* Storage ex |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
return this.saveDeferred; |
|
|
|
return this.useStorage ? this.saveDeferred : Promise.resolve(); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public async delete(key: keyof Storage, saveLocal = false) { |
|
|
|
public async delete(key: keyof Storage, saveLocal = false) { |
|
|
@ -179,7 +194,7 @@ export default class AppStorage<Storage extends Record<string, any>/* Storage ex |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public clear() { |
|
|
|
public clear() { |
|
|
|
return this.storage.deleteAll(); |
|
|
|
return this.storage.deleteAll().catch(noop); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public static toggleStorage(enabled: boolean) { |
|
|
|
public static toggleStorage(enabled: boolean) { |
|
|
@ -194,10 +209,10 @@ export default class AppStorage<Storage extends Record<string, any>/* Storage ex |
|
|
|
} else { |
|
|
|
} else { |
|
|
|
return storage.set(storage.cache); |
|
|
|
return storage.set(storage.cache); |
|
|
|
} |
|
|
|
} |
|
|
|
})); |
|
|
|
})).catch(noop); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public deleteDatabase() { |
|
|
|
public deleteDatabase() { |
|
|
|
return IDBStorage.deleteDatabase(); |
|
|
|
return IDBStorage.deleteDatabase().catch(noop); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|