Browse Source

Replaced deleting IDB with clearing

master
morethanwords 3 years ago
parent
commit
a87f0f42db
  1. 34
      src/lib/idb.ts
  2. 5
      src/lib/mtproto/mtprotoworker.ts
  3. 21
      src/lib/storage.ts

34
src/lib/idb.ts

@ -49,7 +49,7 @@ export default class IDBStorage<T extends Database<any>> {
private name: string; private name: string;
private version: number; private version: number;
private stores: IDBStore[]; private stores: IDBStore[];
private storeName: string; private storeName: T['stores'][0]['name'];
constructor(db: T, storeName: typeof db['stores'][0]['name']) { constructor(db: T, storeName: typeof db['stores'][0]['name']) {
safeAssign(this, db); safeAssign(this, db);
@ -67,8 +67,12 @@ export default class IDBStorage<T extends Database<any>> {
IDBStorage.STORAGES.push(this); IDBStorage.STORAGES.push(this);
} }
public static closeDatabases() { public static closeDatabases(preserve?: IDBStorage<Database<any>>) {
this.STORAGES.forEach(storage => { this.STORAGES.forEach(storage => {
if(preserve && preserve === storage) {
return;
}
const db = storage.db; const db = storage.db;
if(db) { if(db) {
db.onclose = () => {}; db.onclose = () => {};
@ -77,7 +81,19 @@ export default class IDBStorage<T extends Database<any>> {
}); });
} }
public static deleteDatabase() { /**
* ! WARNING ! function requires at least one opened connection
*/
/* public static clearObjectStores() {
const storage = this.STORAGES[0];
this.closeDatabases(storage);
const names = Array.from(storage.db.objectStoreNames);
const promises = names.map(name => storage.clear(name));
return Promise.all(promises);
} */
/* public static deleteDatabase() {
this.closeDatabases(); this.closeDatabases();
const storages = this.STORAGES; const storages = this.STORAGES;
@ -97,7 +113,7 @@ export default class IDBStorage<T extends Database<any>> {
}); });
return Promise.all(promises); return Promise.all(promises);
} } */
public isAvailable() { public isAvailable() {
return this.storageIsAvailable; return this.storageIsAvailable;
@ -215,8 +231,8 @@ export default class IDBStorage<T extends Database<any>> {
}, DEBUG ? 'delete: ' + entryName.join(', ') : ''); }, DEBUG ? 'delete: ' + entryName.join(', ') : '');
} }
public deleteAll() { public clear(storeName?: IDBStorage<T>['storeName']) {
return this.getObjectStore('readwrite', (objectStore) => objectStore.clear(), DEBUG ? 'deleteAll' : ''); return this.getObjectStore('readwrite', (objectStore) => objectStore.clear(), DEBUG ? 'clear' : '', storeName);
} }
public save(entryName: string | string[], value: any | any[]) { public save(entryName: string | string[], value: any | any[]) {
@ -328,7 +344,7 @@ export default class IDBStorage<T extends Database<any>> {
}, DEBUG ? 'get: ' + entryName.join(', ') : ''); }, DEBUG ? 'get: ' + entryName.join(', ') : '');
} }
private getObjectStore<T>(mode: IDBTransactionMode, objectStore: (objectStore: IDBObjectStore) => IDBRequest | IDBRequest[], log?: string) { private getObjectStore<T>(mode: IDBTransactionMode, objectStore: (objectStore: IDBObjectStore) => IDBRequest | IDBRequest[], log?: string, storeName = this.storeName) {
let perf: number; let perf: number;
if(log) { if(log) {
@ -338,7 +354,7 @@ export default class IDBStorage<T extends Database<any>> {
return this.openDatabase().then((db) => { return this.openDatabase().then((db) => {
return new Promise<T>((resolve, reject) => { return new Promise<T>((resolve, reject) => {
const transaction = db.transaction([this.storeName], mode); const transaction = db.transaction([storeName], mode);
transaction.onerror = (e) => { transaction.onerror = (e) => {
clearTimeout(timeout); clearTimeout(timeout);
@ -365,7 +381,7 @@ export default class IDBStorage<T extends Database<any>> {
this.log.error('IndexedDB: transaction abort!', transaction.error); this.log.error('IndexedDB: transaction abort!', transaction.error);
}); */ }); */
const requests = objectStore(transaction.objectStore(this.storeName)); const requests = objectStore(transaction.objectStore(storeName));
const isArray = Array.isArray(requests); const isArray = Array.isArray(requests);
const r: IDBRequest[] = isArray ? requests : [].concat(requests) as any; const r: IDBRequest[] = isArray ? requests : [].concat(requests) as any;

5
src/lib/mtproto/mtprotoworker.ts

@ -21,10 +21,10 @@ import { UserAuth } from './mtproto_config';
import type { MTMessage } from './networker'; import type { MTMessage } from './networker';
import DEBUG, { MOUNT_CLASS_TO } from '../../config/debug'; import DEBUG, { MOUNT_CLASS_TO } from '../../config/debug';
import Socket from './transports/websocket'; import Socket from './transports/websocket';
import IDBStorage from '../idb';
import singleInstance from './singleInstance'; import singleInstance from './singleInstance';
import sessionStorage from '../sessionStorage'; import sessionStorage from '../sessionStorage';
import webPushApiManager from './webPushApiManager'; import webPushApiManager from './webPushApiManager';
import AppStorage from '../storage';
type Task = { type Task = {
taskId: number, taskId: number,
@ -98,7 +98,7 @@ export class ApiManagerProxy extends CryptoWorkerMethods {
this.addTaskListener('clear', () => { this.addTaskListener('clear', () => {
Promise.all([ Promise.all([
IDBStorage.deleteDatabase(), AppStorage.toggleStorage(false),
sessionStorage.clear(), sessionStorage.clear(),
webPushApiManager.forceUnsubscribe() webPushApiManager.forceUnsubscribe()
]).finally(() => { ]).finally(() => {
@ -509,6 +509,7 @@ export class ApiManagerProxy extends CryptoWorkerMethods {
} }
public logOut(): Promise<void> { public logOut(): Promise<void> {
// AppStorage.toggleStorage(false);
return this.performTaskWorker('logOut'); return this.performTaskWorker('logOut');
} }

21
src/lib/storage.ts

@ -54,7 +54,7 @@ export default class AppStorage<Storage extends Record<string, any>, T extends D
private deleteThrottled: () => void; private deleteThrottled: () => void;
private deleteDeferred = deferredPromise<void>(); private deleteDeferred = deferredPromise<void>();
constructor(private db: T, storeName: typeof db['stores'][number]['name']) { constructor(private db: T, private storeName: typeof db['stores'][number]['name']) {
this.storage = new IDBStorage<T>(db, storeName); this.storage = new IDBStorage<T>(db, storeName);
if(AppStorage.STORAGES.length) { if(AppStorage.STORAGES.length) {
@ -266,7 +266,7 @@ export default class AppStorage<Storage extends Record<string, any>, T extends D
} }
public clear() { public clear() {
return this.storage.deleteAll().catch(noop); return this.storage.clear().catch(noop);
} }
public static toggleStorage(enabled: boolean) { public static toggleStorage(enabled: boolean) {
@ -278,27 +278,14 @@ export default class AppStorage<Storage extends Record<string, any>, T extends D
storage.keysToDelete.clear(); storage.keysToDelete.clear();
storage.getPromises.forEach((deferred) => deferred.resolve()); storage.getPromises.forEach((deferred) => deferred.resolve());
storage.getPromises.clear(); storage.getPromises.clear();
/* if(storage.db === DATABASE_SESSION && 'localStorage' in self) { // * support legacy Webogram's localStorage
localStorage.clear();
} */
return storage.clear(); return storage.clear();
} else { } else {
/* if(storage.db === DATABASE_SESSION && 'localStorage' in self) { // * support legacy Webogram's localStorage
for(const i in storage.cache) {
if(storage.cache[i] !== undefined) {
localStorage.setItem(i, JSON.stringify(storage.cache[i]));
}
}
} */
return storage.set(storage.cache); return storage.set(storage.cache);
} }
})).catch(noop); })).catch(noop);
} }
public deleteDatabase() { /* public deleteDatabase() {
return IDBStorage.deleteDatabase().catch(noop); return IDBStorage.deleteDatabase().catch(noop);
} } */
} }

Loading…
Cancel
Save