Move base64 conversions off of the main thread
This commit is contained in:
parent
911bc63c67
commit
02fbea96c0
7 changed files with 167 additions and 5 deletions
|
@ -9,9 +9,112 @@
|
|||
/* global _: false */
|
||||
/* global ContactBuffer: false */
|
||||
/* global GroupBuffer: false */
|
||||
/* global Worker: false */
|
||||
|
||||
/* eslint-disable more/no-then */
|
||||
|
||||
const WORKER_TIMEOUT = 60 * 1000; // one minute
|
||||
|
||||
const _utilWorker = new Worker('js/util_worker.js');
|
||||
const _jobs = Object.create(null);
|
||||
const _DEBUG = false;
|
||||
let _jobCounter = 0;
|
||||
|
||||
function _makeJob(fnName) {
|
||||
_jobCounter += 1;
|
||||
const id = _jobCounter;
|
||||
|
||||
if (_DEBUG) {
|
||||
window.log.info(`Worker job ${id} (${fnName}) started`);
|
||||
}
|
||||
_jobs[id] = {
|
||||
fnName,
|
||||
start: Date.now(),
|
||||
};
|
||||
|
||||
return id;
|
||||
}
|
||||
|
||||
function _updateJob(id, data) {
|
||||
const { resolve, reject } = data;
|
||||
const { fnName, start } = _jobs[id];
|
||||
|
||||
_jobs[id] = {
|
||||
..._jobs[id],
|
||||
...data,
|
||||
resolve: value => {
|
||||
_removeJob(id);
|
||||
const end = Date.now();
|
||||
window.log.info(
|
||||
`Worker job ${id} (${fnName}) succeeded in ${end - start}ms`
|
||||
);
|
||||
return resolve(value);
|
||||
},
|
||||
reject: error => {
|
||||
_removeJob(id);
|
||||
const end = Date.now();
|
||||
window.log.info(
|
||||
`Worker job ${id} (${fnName}) failed in ${end - start}ms`
|
||||
);
|
||||
return reject(error);
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
function _removeJob(id) {
|
||||
if (_DEBUG) {
|
||||
_jobs[id].complete = true;
|
||||
} else {
|
||||
delete _jobs[id];
|
||||
}
|
||||
}
|
||||
|
||||
function _getJob(id) {
|
||||
return _jobs[id];
|
||||
}
|
||||
|
||||
async function callWorker(fnName, ...args) {
|
||||
const jobId = _makeJob(fnName);
|
||||
|
||||
return new Promise((resolve, reject) => {
|
||||
_utilWorker.postMessage([jobId, fnName, ...args]);
|
||||
|
||||
_updateJob(jobId, {
|
||||
resolve,
|
||||
reject,
|
||||
args: _DEBUG ? args : null,
|
||||
});
|
||||
|
||||
setTimeout(
|
||||
() => reject(new Error(`Worker job ${jobId} (${fnName}) timed out`)),
|
||||
WORKER_TIMEOUT
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
_utilWorker.onmessage = e => {
|
||||
const [jobId, errorForDisplay, result] = e.data;
|
||||
|
||||
const job = _getJob(jobId);
|
||||
if (!job) {
|
||||
throw new Error(
|
||||
`Received worker reply to job ${jobId}, but did not have it in our registry!`
|
||||
);
|
||||
}
|
||||
|
||||
const { resolve, reject, fnName } = job;
|
||||
|
||||
if (errorForDisplay) {
|
||||
return reject(
|
||||
new Error(
|
||||
`Error received from worker job ${jobId} (${fnName}): ${errorForDisplay}`
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
return resolve(result);
|
||||
};
|
||||
|
||||
function MessageReceiver(username, password, signalingKey, options = {}) {
|
||||
this.count = 0;
|
||||
|
||||
|
@ -35,10 +138,11 @@ MessageReceiver.stringToArrayBuffer = string =>
|
|||
Promise.resolve(dcodeIO.ByteBuffer.wrap(string, 'binary').toArrayBuffer());
|
||||
MessageReceiver.arrayBufferToString = arrayBuffer =>
|
||||
Promise.resolve(dcodeIO.ByteBuffer.wrap(arrayBuffer).toString('binary'));
|
||||
|
||||
MessageReceiver.stringToArrayBufferBase64 = string =>
|
||||
Promise.resolve(dcodeIO.ByteBuffer.wrap(string, 'base64').toArrayBuffer());
|
||||
callWorker('stringToArrayBufferBase64', string);
|
||||
MessageReceiver.arrayBufferToStringBase64 = arrayBuffer =>
|
||||
Promise.resolve(dcodeIO.ByteBuffer.wrap(arrayBuffer).toString('base64'));
|
||||
callWorker('arrayBufferToStringBase64', arrayBuffer);
|
||||
|
||||
MessageReceiver.prototype = new textsecure.EventTarget();
|
||||
MessageReceiver.prototype.extend({
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue