tdweb 0.2.48: bugfixes

GitOrigin-RevId: 3f3444ef235352e840ebc55b375a681de1ad3fad
This commit is contained in:
Arseny Smirnov 2019-04-25 21:50:26 +03:00
parent d280e6a5cc
commit 80ab5e666a
3 changed files with 34 additions and 8 deletions

View File

@ -1,6 +1,6 @@
{ {
"name": "@arseny30/tdweb", "name": "@arseny30/tdweb",
"version": "0.2.44", "version": "0.2.48",
"description": "Javascript interface for TDLib (telegram library)", "description": "Javascript interface for TDLib (telegram library)",
"main": "dist/tdweb.js", "main": "dist/tdweb.js",
"repository": { "repository": {

View File

@ -167,6 +167,10 @@ class TdClient {
this.onInited(); this.onInited();
return; return;
} }
if (response['@type'] === 'fsInited') {
this.onFsInited();
return;
}
if ( if (
response['@type'] === 'updateAuthorizationState' && response['@type'] === 'updateAuthorizationState' &&
response.authorization_state['@type'] === 'authorizationStateClosed' response.authorization_state['@type'] === 'authorizationStateClosed'
@ -199,6 +203,10 @@ class TdClient {
/** @private */ /** @private */
onBroadcastMessage(e) { onBroadcastMessage(e) {
var message = e.data; var message = e.data;
if (message.uid === this.uid) {
log.info('ignore self broadcast message: ', message);
return;
}
log.info('got broadcast message: ', message); log.info('got broadcast message: ', message);
if (message.isBackground && !this.isBackground) { if (message.isBackground && !this.isBackground) {
// continue // continue
@ -241,6 +249,11 @@ class TdClient {
// nop // nop
} }
/** @private */
onFsInited() {
this.fileManager.init();
}
/** @private */ /** @private */
onInited() { onInited() {
this.isInited = true; this.isInited = true;
@ -339,16 +352,23 @@ class TdClient {
/** @private */ /** @private */
class FileManager { class FileManager {
constructor(instanceName) { constructor(instanceName) {
this.instanceName = instanceName;
this.cache = new Map(); this.cache = new Map();
this.pending = [];
this.transaction_id = 0;
}
init() {
let self = this;
this.idb = new Promise((resolve, reject) => { this.idb = new Promise((resolve, reject) => {
const request = window.indexedDB.open(instanceName); const request = window.indexedDB.open(self.instanceName);
request.onsuccess = () => resolve(request.result); request.onsuccess = () => resolve(request.result);
request.onerror = () => reject(request.error); request.onerror = () => reject(request.error);
}); });
//this.store = localforage.createInstance({ //this.store = localforage.createInstance({
//name: instanceName //name: instanceName
//}); //});
this.pending = []; this.isInited = true;
} }
registerFile(file) { registerFile(file) {
@ -375,6 +395,7 @@ class FileManager {
let pending = this.pending; let pending = this.pending;
this.pending = []; this.pending = [];
let idb = await this.idb; let idb = await this.idb;
let transaction_id = this.transaction_id++;
let read = idb let read = idb
.transaction(['keyvaluepairs'], 'readonly') .transaction(['keyvaluepairs'], 'readonly')
.objectStore('keyvaluepairs'); .objectStore('keyvaluepairs');
@ -384,7 +405,7 @@ class FileManager {
request.onsuccess = event => { request.onsuccess = event => {
const blob = event.target.result; const blob = event.target.result;
if (blob) { if (blob) {
query.resolve(blob); query.resolve({ data: blob, transaction_id: transaction_id });
} else { } else {
query.reject(); query.reject();
} }
@ -405,7 +426,7 @@ class FileManager {
async doLoad(info) { async doLoad(info) {
if (info.arr) { if (info.arr) {
return new Blob([info.arr]); return { data: new Blob([info.arr]), transaction_id: -1 };
} }
let idb_key = info.idb_key; let idb_key = info.idb_key;
let self = this; let self = this;
@ -417,15 +438,19 @@ class FileManager {
async readFile(query) { async readFile(query) {
try { try {
if (!this.isInited) {
throw new Error('FileManager is not inited');
}
let info = this.cache.get(query.file_id); let info = this.cache.get(query.file_id);
if (!info) { if (!info) {
throw new Error('File is not loaded'); throw new Error('File is not loaded');
} }
let data = await this.doLoad(info); let response = await this.doLoad(info);
return { return {
'@type': 'Blob', '@type': 'blob',
'@extra': query['@extra'], '@extra': query['@extra'],
data: data data: response.data,
transaction_id: response.transaction_id
}; };
} catch (e) { } catch (e) {
return { return {

View File

@ -531,6 +531,7 @@ class TdClient {
log.info('FS start init'); log.info('FS start init');
this.tdfs = await tdfs_promise; this.tdfs = await tdfs_promise;
log.info('FS inited'); log.info('FS inited');
this.callback({ '@type': 'fsInited' });
// no async initialization after this point // no async initialization after this point
if (options.logVerbosityLevel === undefined) { if (options.logVerbosityLevel === undefined) {