Skip to content

Commit

Permalink
Detect premature worker load error
Browse files Browse the repository at this point in the history
Rejects the ready promise when the worker fails to load, e.g. due to
network errors.
  • Loading branch information
Rob--W committed Mar 23, 2016
1 parent e1ac574 commit 48b94c7
Showing 1 changed file with 19 additions and 6 deletions.
25 changes: 19 additions & 6 deletions src/display/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -1302,11 +1302,25 @@ var PDFWorker = (function PDFWorkerClosure() {
// https://bugzilla.mozilla.org/show_bug.cgi?id=683280
var worker = new Worker(workerSrc);
var messageHandler = new MessageHandler('main', 'worker', worker);
var terminateEarly = function() {
this._readyCapability.reject(new Error('Worker was destroyed'));
messageHandler.destroy();
worker.removeEventListener('error', onWorkerError);
worker.terminate();
}.bind(this);

var onWorkerError = function(event) {
if (!event.message && !this.destroyed && !this._webWorker) {
// Worker failed to initialize due to a load error.
terminateEarly();
}
}.bind(this);
worker.addEventListener('error', onWorkerError);

messageHandler.on('test', function PDFWorker_test(data) {
worker.removeEventListener('error', onWorkerError);
if (this.destroyed) {
this._readyCapability.reject(new Error('Worker was destroyed'));
messageHandler.destroy();
worker.terminate();
terminateEarly();
return; // worker was destroyed
}
var supportTypedArray = data && data.supportTypedArray;
Expand All @@ -1333,10 +1347,9 @@ var PDFWorker = (function PDFWorkerClosure() {
});

messageHandler.on('ready', function (data) {
worker.removeEventListener('error', onWorkerError);
if (this.destroyed) {
this._readyCapability.reject(new Error('Worker was destroyed'));
messageHandler.destroy();
worker.terminate();
terminateEarly();
return; // worker was destroyed
}
try {
Expand Down

0 comments on commit 48b94c7

Please sign in to comment.