Browse Source

Fix #207, perormance improvements

master
Igor Zhukov 11 years ago
parent
commit
fd0c4a69e3
  1. 2
      app/css/app.css
  2. 4
      app/js/directives.js
  3. 68
      app/js/lib/mtproto.js
  4. 18
      app/partials/welcome.html

2
app/css/app.css

@ -423,7 +423,7 @@ input[type="number"]::-webkit-inner-spin-button {
display: block; display: block;
width: 112px; width: 112px;
height: 112px; height: 112px;
margin: 100px auto 0; margin: 0 auto;
} }
.welcome_box_wrap { .welcome_box_wrap {
max-width: 310px; max-width: 310px;

4
app/js/directives.js

@ -651,13 +651,13 @@ angular.module('myApp.directives', ['myApp.filters'])
element.attr('src', $scope.thumb.placeholder || 'img/blank.gif'); element.attr('src', $scope.thumb.placeholder || 'img/blank.gif');
} }
MtpApiFileManager.downloadSmallFile($scope.thumb.location, $scope.thumb.size).then(function (url) { MtpApiFileManager.downloadSmallFile($scope.thumb.location).then(function (url) {
if (counterSaved == counter) { if (counterSaved == counter) {
element.attr('src', url); element.attr('src', url);
cleanup(); cleanup();
} }
}, function (e) { }, function (e) {
console.log('Download image failed', e, $scope.thumb.location); console.log('Download image failed', e, $scope.thumb.location, element[0]);
if (counterSaved == counter) { if (counterSaved == counter) {
element.attr('src', $scope.thumb.placeholder || 'img/blank.gif'); element.attr('src', $scope.thumb.placeholder || 'img/blank.gif');
cleanup(); cleanup();

68
app/js/lib/mtproto.js

@ -285,7 +285,7 @@ function pqPrimeFactorization (pqBytes) {
}; };
// console.timeEnd('PQ long'); // console.timeEnd('PQ long');
} }
console.log(result); // console.log(result);
if (result === false) { if (result === false) {
// console.time('pq BigInt'); // console.time('pq BigInt');
@ -373,22 +373,23 @@ function gcdLong(a, b) {
} }
function pqPrimeLong(what) { function pqPrimeLong(what) {
// console.log('start long');
var it = 0, var it = 0,
g; g;
for (var i = 0; i < 3; i++) { for (var i = 0; i < 3; i++) {
var q = (nextRandomInt(128) & 15) + 17, var q = goog.math.Long.fromInt((nextRandomInt(128) & 15) + 17),
x = goog.math.Long.fromInt(nextRandomInt(1000000000) + 1), x = goog.math.Long.fromInt(nextRandomInt(1000000000) + 1),
y = new goog.math.Long(x.getLowBits(), x.getHighBits()), y = x,
lim = 1 << (i + 18); lim = 1 << (i + 18);
// console.log(x);
for (var j = 1; j < lim; j++) { for (var j = 1; j < lim; j++) {
++it; ++it;
var a = new goog.math.Long(x.getLowBits(), x.getHighBits()), // if (!(it % 100)) {
b = new goog.math.Long(x.getLowBits(), x.getHighBits()), // console.log(dT(), 'it', it, i, j, x.toString());
c = goog.math.Long.fromInt(q); // }
var a = x,
// console.log(a, b, c); b = x,
c = q;
while (b.notEquals(goog.math.Long.ZERO)) { while (b.notEquals(goog.math.Long.ZERO)) {
if (b.and(goog.math.Long.ONE).notEquals(goog.math.Long.ZERO)) { if (b.and(goog.math.Long.ONE).notEquals(goog.math.Long.ZERO)) {
@ -404,14 +405,14 @@ function pqPrimeLong(what) {
b = b.shiftRight(1); b = b.shiftRight(1);
} }
x = new goog.math.Long(c.getLowBits(), c.getHighBits()); x = c;
var z = x.compare(y) < 0 ? y.subtract(x) : x.subtract(y); var z = x.compare(y) < 0 ? y.subtract(x) : x.subtract(y);
g = gcdLong(z, what); g = gcdLong(z, what);
if (g.notEquals(goog.math.Long.ONE)) { if (g.notEquals(goog.math.Long.ONE)) {
break; break;
} }
if ((j & (j - 1)) == 0) { if ((j & (j - 1)) == 0) {
y = new goog.math.Long(x.getLowBits(), x.getHighBits()); y = x;
} }
} }
if (g.compare(goog.math.Long.ONE) > 0) { if (g.compare(goog.math.Long.ONE) > 0) {
@ -1226,7 +1227,7 @@ factory('MtpAuthorizer', function (MtpDcConfigurator, MtpRsaKeysManager, MtpSecu
} }
console.log(dT(), 'PQ factorization start'); console.log(dT(), 'PQ factorization start');
if (!!window.Worker) { if (!!window.Worker/* && false*/) {
var worker = new Worker('js/lib/pq_worker.js'); var worker = new Worker('js/lib/pq_worker.js');
worker.onmessage = function (e) { worker.onmessage = function (e) {
@ -2005,7 +2006,7 @@ factory('MtpNetworkerFactory', function (MtpDcConfigurator, MtpMessageIdGenerato
if (hasApiCall && !hasHttpWait) { if (hasApiCall && !hasHttpWait) {
var serializer = new TLSerialization({mtproto: true}); var serializer = new TLSerialization({mtproto: true});
serializer.storeMethod('http_wait', {max_delay: 0, wait_after: 0, max_wait: 25000}); serializer.storeMethod('http_wait', {max_delay: 0, wait_after: 0, max_wait: 1000});
messages.push({ messages.push({
msg_id: MtpMessageIdGenerator.generateID(), msg_id: MtpMessageIdGenerator.generateID(),
seq_no: this.generateSeqNo(), seq_no: this.generateSeqNo(),
@ -2650,7 +2651,8 @@ factory('MtpApiManager', function (AppConfigManager, MtpAuthorizer, MtpNetworker
factory('MtpApiFileManager', function (MtpApiManager, $q, $window) { factory('MtpApiFileManager', function (MtpApiManager, $q, $window) {
var cachedFS = false; var cachedFs = false;
var cachedFsPromise = false;
var apiUploadPromise = $q.when(); var apiUploadPromise = $q.when();
var cachedSavePromises = {}; var cachedSavePromises = {};
var cachedDownloadPromises = {}; var cachedDownloadPromises = {};
@ -2703,27 +2705,27 @@ factory('MtpApiFileManager', function (MtpApiManager, $q, $window) {
}) })
}; };
function requestFS (argument) { function requestFS () {
if (cachedFS) { if (cachedFsPromise) {
return $q.when(cachedFS); return cachedFsPromise;
} }
$window.requestFileSystem = $window.requestFileSystem || $window.webkitRequestFileSystem; $window.requestFileSystem = $window.requestFileSystem || $window.webkitRequestFileSystem;
if (!$window.requestFileSystem/* || true*/) { if (!$window.requestFileSystem || true) {
return $q.reject({type: 'FS_BROWSER_UNSUPPORTED', description: 'requestFileSystem not present'}); return cachedFsPromise = $q.reject({type: 'FS_BROWSER_UNSUPPORTED', description: 'requestFileSystem not present'});
} }
var deferred = $q.defer(); var deferred = $q.defer();
$window.requestFileSystem($window.TEMPORARY, 5*1024*1024, function (fs) { $window.requestFileSystem($window.TEMPORARY, 5*1024*1024, function (fs) {
cachedFS = fs; cachedFs = fs;
deferred.resolve(); deferred.resolve();
}, function (e) { }, function (e) {
deferred.reject(e); deferred.reject(e);
}); });
return deferred.promise; return cachedFsPromise = deferred.promise;
}; };
function fileWriteBytes(fileWriter, bytes) { function fileWriteBytes(fileWriter, bytes) {
@ -2794,10 +2796,10 @@ factory('MtpApiFileManager', function (MtpApiManager, $q, $window) {
}; };
requestFS().then(function () { requestFS().then(function () {
cachedFS.root.getFile(fileName, {create: false}, function(fileEntry) { cachedFs.root.getFile(fileName, {create: false}, function(fileEntry) {
deferred.resolve(cachedDownloads[fileName] = fileEntry.toURL()); deferred.resolve(cachedDownloads[fileName] = fileEntry.toURL());
}, function () { }, function () {
cachedFS.root.getFile(fileName, {create: true}, function(fileEntry) { cachedFs.root.getFile(fileName, {create: true}, function(fileEntry) {
fileEntry.createWriter(function (fileWriter) { fileEntry.createWriter(function (fileWriter) {
cacheFileWriter = fileWriter; cacheFileWriter = fileWriter;
fileWriteBytes(fileWriter, bytes).then(function () { fileWriteBytes(fileWriter, bytes).then(function () {
@ -2806,7 +2808,9 @@ factory('MtpApiFileManager', function (MtpApiManager, $q, $window) {
}, errorHandler); }, errorHandler);
}, errorHandler); }, errorHandler);
}); });
}, errorHandler); }, function () {
deferred.resolve('data:image/jpeg;base64,' + bytesToBase64(bytes))
});
return cachedSavePromises[fileName] = deferred.promise; return cachedSavePromises[fileName] = deferred.promise;
} }
@ -2828,7 +2832,7 @@ factory('MtpApiFileManager', function (MtpApiManager, $q, $window) {
errorHandler = angular.noop; errorHandler = angular.noop;
}, },
doDownload = function () { doDownload = function () {
cachedFS.root.getFile(fileName, {create: true}, function(fileEntry) { cachedFs.root.getFile(fileName, {create: true}, function(fileEntry) {
var downloadPromise = downloadRequest(location.dc_id, function () { var downloadPromise = downloadRequest(location.dc_id, function () {
// console.log('next small promise'); // console.log('next small promise');
return MtpApiManager.invokeApi('upload.getFile', { return MtpApiManager.invokeApi('upload.getFile', {
@ -2855,7 +2859,7 @@ factory('MtpApiFileManager', function (MtpApiManager, $q, $window) {
}; };
requestFS().then(function () { requestFS().then(function () {
cachedFS.root.getFile(fileName, {create: false}, function(fileEntry) { cachedFs.root.getFile(fileName, {create: false}, function(fileEntry) {
fileEntry.file(function(file) { fileEntry.file(function(file) {
if (file.size) { if (file.size) {
deferred.resolve(cachedDownloads[fileName] = fileEntry.toURL()); deferred.resolve(cachedDownloads[fileName] = fileEntry.toURL());
@ -2866,9 +2870,7 @@ factory('MtpApiFileManager', function (MtpApiManager, $q, $window) {
}, errorHandler); }, errorHandler);
}, doDownload); }, doDownload);
}, function (error) { }, function (error) {
downloadRequest(location.dc_id, function () { downloadRequest(location.dc_id, function () {
// console.log('next small promise');
return MtpApiManager.invokeApi('upload.getFile', { return MtpApiManager.invokeApi('upload.getFile', {
location: angular.extend({}, location, {_: 'inputFileLocation'}), location: angular.extend({}, location, {_: 'inputFileLocation'}),
offset: 0, offset: 0,
@ -2970,18 +2972,18 @@ factory('MtpApiFileManager', function (MtpApiManager, $q, $window) {
saveToFileEntry(fileEntry); saveToFileEntry(fileEntry);
} else { } else {
requestFS().then(function () { requestFS().then(function () {
cachedFS.root.getFile(fileName, {create: false}, function(fileEntry) { cachedFs.root.getFile(fileName, {create: false}, function(fileEntry) {
fileEntry.file(function(file) { fileEntry.file(function(file) {
// console.log(dT(), 'Check size', file.size, size); // console.log(dT(), 'Check size', file.size, size);
if (file.size >= size/* && false*/) { if (file.size >= size/* && false*/) {
deferred.resolve(cachedDownloads[fileName] = fileEntry.toURL()); deferred.resolve(cachedDownloads[fileName] = fileEntry.toURL());
} else { } else {
console.log('File bad size', file, size); console.log('File bad size', file, size);
cachedFS.root.getFile(fileName, {create: true}, saveToFileEntry, errorHandler) cachedFs.root.getFile(fileName, {create: true}, saveToFileEntry, errorHandler)
} }
}, errorHandler); }, errorHandler);
}, function () { }, function () {
cachedFS.root.getFile(fileName, {create: true}, saveToFileEntry, errorHandler) cachedFs.root.getFile(fileName, {create: true}, saveToFileEntry, errorHandler)
}); });
}, function () { }, function () {
@ -3059,10 +3061,10 @@ factory('MtpApiFileManager', function (MtpApiManager, $q, $window) {
}; };
requestFS().then(function () { requestFS().then(function () {
cachedFS.root.getFile(fileName, {create: false}, function(fileEntry) { cachedFs.root.getFile(fileName, {create: false}, function(fileEntry) {
deferred.resolve(fileEntry); deferred.resolve(fileEntry);
}, function () { }, function () {
cachedFS.root.getFile(fileName, {create: true}, function(fileEntry) { cachedFs.root.getFile(fileName, {create: true}, function(fileEntry) {
fileEntry.createWriter(function (fileWriter) { fileEntry.createWriter(function (fileWriter) {
cacheFileWriter = fileWriter; cacheFileWriter = fileWriter;
fileWriteBytes(fileWriter, file).then(function () { fileWriteBytes(fileWriter, file).then(function () {

18
app/partials/welcome.html

@ -1,17 +1,19 @@
<div ng-include="'partials/head.html'"></div> <div ng-include="'partials/head.html'"></div>
<div ng-show="showWelcome"> <div ng-if="showWelcome">
<div class="welcome_logo"></div> <div my-vertical-position="0.4">
<div class="welcome_logo"></div>
<div class="welcome_box_wrap"> <div class="welcome_box_wrap">
<div class="welcome_text"> <div class="welcome_text">
<p>Welcome to an unofficial web-client of mobile <strong>Telegram</strong> messenger.</p> <p>Welcome to an unofficial web-client of mobile <strong>Telegram</strong> messenger.</p>
<p>It is an alpha, which means it is still not 200% secure and functional.</p> <p>It is an alpha, which means it is still not 200% secure and functional.</p>
<p>Visit <a target="_blank" href="https://telegram.org/">telegram.org</a> to learn more.</p> <p>Visit <a target="_blank" href="https://telegram.org/">telegram.org</a> to learn more.</p>
</div>
<a href="#/login" class="btn btn-success btn-block">Start Messaging</a>
</div> </div>
<a href="#/login" class="btn btn-success btn-block">Start Messaging</a>
</div> </div>
</div> </div>

Loading…
Cancel
Save