GitOrigin-RevId: 96a5ee58db835d45cc8e0856811951048820f22e
This commit is contained in:
Arseny Smirnov 2019-03-14 11:47:50 +11:00
parent 6390bc9e0f
commit 5b62ac0ea8
3 changed files with 24 additions and 17 deletions

View File

@ -1,6 +1,6 @@
{ {
"name": "@arseny30/tdweb", "name": "@arseny30/tdweb",
"version": "0.2.30", "version": "0.2.34",
"description": "Javascript interface for TDLib (telegram library)", "description": "Javascript interface for TDLib (telegram library)",
"main": "dist/tdweb.js", "main": "dist/tdweb.js",
"files": [ "files": [

View File

@ -50,9 +50,9 @@ class TdClient {
'receive from worker: ', 'receive from worker: ',
JSON.parse( JSON.parse(
JSON.stringify(response, (key, value) => { JSON.stringify(response, (key, value) => {
if (key === 'arr') { //if (key === 'arr') {
return undefined; //return undefined;
} //}
return value; return value;
}) })
) )

View File

@ -190,17 +190,14 @@ class InboundFileSystem {
this.pids.delete(pid); this.pids.delete(pid);
} }
async persist(pid, path) { async persist(pid, path, arr) {
var arr;
try { try {
arr = this.FS.readFile(path);
await this.store.setItem(pid, new Blob([arr])); await this.store.setItem(pid, new Blob([arr]));
this.pids.add(pid); this.pids.add(pid);
this.FS.unlink(path); this.FS.unlink(path);
} catch (e) { } catch (e) {
log.error('Failed persist ' + path + ' ', e); log.error('Failed persist ' + path + ' ', e);
} }
return arr;
} }
} }
@ -645,20 +642,30 @@ class TdClient {
this.callback({ '@type': 'updateFatalError', error: error }); this.callback({ '@type': 'updateFatalError', error: error });
} }
async saveFile(pid, file) { saveFile(pid, file) {
let isSaving = this.savingFiles.has(pid); let isSaving = this.savingFiles.has(pid);
this.savingFiles.set(pid, file); this.savingFiles.set(pid, file);
if (isSaving) { if (isSaving) {
return; return file;
} }
let arr = await this.tdfs.inboundFileSystem.persist(pid, file.local.path); try {
file = this.savingFiles.get(pid); var arr = this.FS.readFile(file.local.path);
file.idb_key = pid;
if (arr) { if (arr) {
file.arr = arr; file.arr = arr;
this.doSaveFile(pid, file, arr);
} }
this.callback({ '@type': 'updateFile', file: file }, [arr.buffer]); } catch (e) {
delete file.arr; 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;
this.callback({ '@type': 'updateFile', file: file });
this.savingFiles.delete(pid); this.savingFiles.delete(pid);
} }
@ -676,7 +683,7 @@ class TdClient {
} }
if (file.local.is_downloading_completed) { if (file.local.is_downloading_completed) {
this.saveFile(pid, file); file = this.saveFile(pid, file);
} }
return file; return file;
} }