Browse Source

Fix Firefox storage again

Disable keep signed checkbox if storage is unavailable
master
Eduard Kuzmenko 4 years ago
parent
commit
8e383f7649
  1. 2
      src/lib/appManagers/appStateManager.ts
  2. 4
      src/lib/idb.ts
  3. 29
      src/lib/storage.ts
  4. 7
      src/pages/pageSignIn.ts

2
src/lib/appManagers/appStateManager.ts

@ -177,6 +177,8 @@ export class AppStateManager extends EventListenerBase<{
public storagesResults: {[key in keyof AppStateManager['storages']]: any[]} = {} as any; public storagesResults: {[key in keyof AppStateManager['storages']]: any[]} = {} as any;
public storage = sessionStorage;
constructor() { constructor() {
super(); super();
this.loadSavedState(); this.loadSavedState();

4
src/lib/idb.ts

@ -35,7 +35,7 @@ export type IDBOptions = {
version?: number version?: number
}; };
const DEBUG = false; const DEBUG = true;
export default class IDBStorage { export default class IDBStorage {
private static STORAGES: IDBStorage[] = []; private static STORAGES: IDBStorage[] = [];
@ -110,7 +110,7 @@ export default class IDBStorage {
var request = indexedDB.open(this.name, this.version); var request = indexedDB.open(this.name, this.version);
if(!request) { if(!request) {
throw new Error(); return Promise.reject();
} }
} catch(error) { } catch(error) {
this.log.error('error opening db', error.message) this.log.error('error opening db', error.message)

29
src/lib/storage.ts

@ -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);
} }
} }

7
src/pages/pageSignIn.ts

@ -329,7 +329,12 @@ let onFirstMount = () => {
}); });
appStateManager.getState().then(state => { appStateManager.getState().then(state => {
signedCheckboxField.checked = state.keepSigned; if(!appStateManager.storage.isAvailable()) {
signedCheckboxField.checked = false;
signedCheckboxField.label.classList.add('checkbox-disabled');
} else {
signedCheckboxField.checked = state.keepSigned;
}
}); });
btnNext = Button('btn-primary btn-color-primary', {text: 'Login.Next'}); btnNext = Button('btn-primary btn-color-primary', {text: 'Login.Next'});

Loading…
Cancel
Save