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

5
src/lib/mtproto/mtprotoworker.ts

@ -21,10 +21,10 @@ import { UserAuth } from './mtproto_config'; @@ -21,10 +21,10 @@ import { UserAuth } from './mtproto_config';
import type { MTMessage } from './networker';
import DEBUG, { MOUNT_CLASS_TO } from '../../config/debug';
import Socket from './transports/websocket';
import IDBStorage from '../idb';
import singleInstance from './singleInstance';
import sessionStorage from '../sessionStorage';
import webPushApiManager from './webPushApiManager';
import AppStorage from '../storage';
type Task = {
taskId: number,
@ -98,7 +98,7 @@ export class ApiManagerProxy extends CryptoWorkerMethods { @@ -98,7 +98,7 @@ export class ApiManagerProxy extends CryptoWorkerMethods {
this.addTaskListener('clear', () => {
Promise.all([
IDBStorage.deleteDatabase(),
AppStorage.toggleStorage(false),
sessionStorage.clear(),
webPushApiManager.forceUnsubscribe()
]).finally(() => {
@ -509,6 +509,7 @@ export class ApiManagerProxy extends CryptoWorkerMethods { @@ -509,6 +509,7 @@ export class ApiManagerProxy extends CryptoWorkerMethods {
}
public logOut(): Promise<void> {
// AppStorage.toggleStorage(false);
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 @@ -54,7 +54,7 @@ export default class AppStorage<Storage extends Record<string, any>, T extends D
private deleteThrottled: () => 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);
if(AppStorage.STORAGES.length) {
@ -266,7 +266,7 @@ export default class AppStorage<Storage extends Record<string, any>, T extends D @@ -266,7 +266,7 @@ export default class AppStorage<Storage extends Record<string, any>, T extends D
}
public clear() {
return this.storage.deleteAll().catch(noop);
return this.storage.clear().catch(noop);
}
public static toggleStorage(enabled: boolean) {
@ -278,27 +278,14 @@ export default class AppStorage<Storage extends Record<string, any>, T extends D @@ -278,27 +278,14 @@ export default class AppStorage<Storage extends Record<string, any>, T extends D
storage.keysToDelete.clear();
storage.getPromises.forEach((deferred) => deferred.resolve());
storage.getPromises.clear();
/* if(storage.db === DATABASE_SESSION && 'localStorage' in self) { // * support legacy Webogram's localStorage
localStorage.clear();
} */
return storage.clear();
} 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);
}
})).catch(noop);
}
public deleteDatabase() {
/* public deleteDatabase() {
return IDBStorage.deleteDatabase().catch(noop);
}
} */
}

Loading…
Cancel
Save