diff --git a/example/web/tdweb/package.json b/example/web/tdweb/package.json index 7de91c8fe..f124934bd 100644 --- a/example/web/tdweb/package.json +++ b/example/web/tdweb/package.json @@ -1,6 +1,6 @@ { "name": "@arseny30/tdweb", - "version": "0.2.30", + "version": "0.2.34", "description": "Javascript interface for TDLib (telegram library)", "main": "dist/tdweb.js", "files": [ diff --git a/example/web/tdweb/src/index.js b/example/web/tdweb/src/index.js index 87eab7666..2573fc132 100644 --- a/example/web/tdweb/src/index.js +++ b/example/web/tdweb/src/index.js @@ -50,9 +50,9 @@ class TdClient { 'receive from worker: ', JSON.parse( JSON.stringify(response, (key, value) => { - if (key === 'arr') { - return undefined; - } + //if (key === 'arr') { + //return undefined; + //} return value; }) ) diff --git a/example/web/tdweb/src/worker.js b/example/web/tdweb/src/worker.js index 1829d9047..cefff256b 100644 --- a/example/web/tdweb/src/worker.js +++ b/example/web/tdweb/src/worker.js @@ -190,17 +190,14 @@ class InboundFileSystem { this.pids.delete(pid); } - async persist(pid, path) { - var arr; + async persist(pid, path, arr) { try { - arr = this.FS.readFile(path); await this.store.setItem(pid, new Blob([arr])); this.pids.add(pid); this.FS.unlink(path); } catch (e) { log.error('Failed persist ' + path + ' ', e); } - return arr; } } @@ -645,20 +642,30 @@ class TdClient { this.callback({ '@type': 'updateFatalError', error: error }); } - async saveFile(pid, file) { + saveFile(pid, file) { let isSaving = this.savingFiles.has(pid); this.savingFiles.set(pid, file); if (isSaving) { - return; + return file; } - let arr = await this.tdfs.inboundFileSystem.persist(pid, file.local.path); + try { + var arr = this.FS.readFile(file.local.path); + if (arr) { + file.arr = arr; + this.doSaveFile(pid, file, arr); + } + } catch (e) { + log.error('Failed to readFile: ', e); + } + return file; + } + + async doSaveFile(pid, file, arr) { + await this.tdfs.inboundFileSystem.persist(pid, file.local.path, arr); file = this.savingFiles.get(pid); file.idb_key = pid; - if (arr) { - file.arr = arr; - } - this.callback({ '@type': 'updateFile', file: file }, [arr.buffer]); - delete file.arr; + this.callback({ '@type': 'updateFile', file: file }); + this.savingFiles.delete(pid); } @@ -676,7 +683,7 @@ class TdClient { } if (file.local.is_downloading_completed) { - this.saveFile(pid, file); + file = this.saveFile(pid, file); } return file; }