From f37d86d109e7aeb9cca79bfde1a14a01f5788847 Mon Sep 17 00:00:00 2001 From: Arseny Smirnov Date: Thu, 14 Feb 2019 19:50:00 +0300 Subject: [PATCH] tdweb: handle 'destroy' after FatalError GitOrigin-RevId: 414200ea7fb79798aa9c8cd89c949e0f87204ac2 --- example/web/tdweb/src/worker.js | 52 ++++++++++++++++++++++++++++++++- 1 file changed, 51 insertions(+), 1 deletion(-) diff --git a/example/web/tdweb/src/worker.js b/example/web/tdweb/src/worker.js index c98ba4fd..b5cedff5 100644 --- a/example/web/tdweb/src/worker.js +++ b/example/web/tdweb/src/worker.js @@ -248,6 +248,28 @@ class DbFileSystem { clearInterval(this.syncfsInterval); await this.sync(); } + async destroy() { + clearInterval(this.syncfsInterval); + if (this.readOnly) { + return; + } + this.FS.unmount(this.root); + var req = indexedDB.deleteDatabase(this.root); + await new Promise((resolve, reject) => { + req.onsuccess = function (e) { + log.info('SUCCESS'); + resolve(e.result); + }; + req.onerror = function(e) { + log.info('ONERROR'); + reject(e.error); + } + req.onblocked = function(e) { + log.info('ONBLOCKED'); + reject('blocked'); + } + }); + } } class TdFileSystem { @@ -281,6 +303,9 @@ class TdFileSystem { log.error('Failed to init TdFileSystem: ', e); } } + async destroy() { + await this.dbFileSystem.destroy(); + } } class TdClient { @@ -451,7 +476,13 @@ class TdClient { } send(query) { - if (this.wasFatalError || this.isClosing) { + if (this.isClosing) { + return; + } + if (this.wasFatalError) { + if (query['@type'] === 'destroy') { + this.destroy({'@type': 'Ok', '@extra': query['@extra']}); + } return; } if (query['@type'] === 'init') { @@ -498,6 +529,7 @@ class TdClient { if (this.wasFatalError) { return; } + this.onFatalError('XXXX'); try { while (true) { let msg = this.td_functions.td_receive(this.client); @@ -567,6 +599,24 @@ class TdClient { this.callback(last_update); } + async destroy(result) { + try { + log.info('destroy tdfs ...'); + await this.tdfs.destroy(); + log.info('destroy tdfs ok'); + } catch (e) { + log.error('Failed destroy', e); + } + this.callback(result); + this.callback({ + '@type': 'updateAuthorizationState', + authorization_state: { + '@type': 'authorizationStateClosed' + } + } + ); + } + async asyncOnFatalError(error) { await this.tdfs.dbFileSystem.sync(); this.callback({ '@type': 'updateFatalError', error: error });