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

4
src/lib/idb.ts

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

29
src/lib/storage.ts

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

7
src/pages/pageSignIn.ts

@ -329,7 +329,12 @@ let onFirstMount = () => { @@ -329,7 +329,12 @@ let onFirstMount = () => {
});
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'});

Loading…
Cancel
Save