Remove support for the scope
parameter in the MessageHandler.on
method
At this point in time it's easy to convert the `MessageHandler.on` call-sites to use arrow functions, and thus let the JavaScript engine handle scopes for us, rather than having to manually keep references to the relevant scopes in `MessageHandler`.[1] An additional benefit of this is that a couple of `Function.prototype.call()` instances can now be converted into "normal" function calls, which should be a tiny bit more efficient. All in all, I don't see any compelling reason why it'd be necessary to keep supporting custom `scope`s in the `MessageHandler` implementation. --- [1] In the event that a custom scope is ever needed, simply using `bind` on the handler function when calling `MessageHandler.on` ought to work as well.
This commit is contained in:
parent
d1e6d427cd
commit
055f03938b
@ -1875,7 +1875,7 @@ class WorkerTransport {
|
||||
setupMessageHandler() {
|
||||
const { messageHandler, loadingTask, } = this;
|
||||
|
||||
messageHandler.on('GetReader', function(data, sink) {
|
||||
messageHandler.on('GetReader', (data, sink) => {
|
||||
assert(this._networkStream);
|
||||
this._fullReader = this._networkStream.getFullReader();
|
||||
this._fullReader.onProgress = (evt) => {
|
||||
@ -1902,9 +1902,9 @@ class WorkerTransport {
|
||||
sink.onCancel = (reason) => {
|
||||
this._fullReader.cancel(reason);
|
||||
};
|
||||
}, this);
|
||||
});
|
||||
|
||||
messageHandler.on('ReaderHeadersReady', function(data) {
|
||||
messageHandler.on('ReaderHeadersReady', (data) => {
|
||||
const headersCapability = createPromiseCapability();
|
||||
const fullReader = this._fullReader;
|
||||
fullReader.headersReady.then(() => {
|
||||
@ -1932,9 +1932,9 @@ class WorkerTransport {
|
||||
}, headersCapability.reject);
|
||||
|
||||
return headersCapability.promise;
|
||||
}, this);
|
||||
});
|
||||
|
||||
messageHandler.on('GetRangeReader', function(data, sink) {
|
||||
messageHandler.on('GetRangeReader', (data, sink) => {
|
||||
assert(this._networkStream);
|
||||
const rangeReader =
|
||||
this._networkStream.getRangeReader(data.begin, data.end);
|
||||
@ -1970,14 +1970,14 @@ class WorkerTransport {
|
||||
sink.onCancel = (reason) => {
|
||||
rangeReader.cancel(reason);
|
||||
};
|
||||
}, this);
|
||||
});
|
||||
|
||||
messageHandler.on('GetDoc', function({ pdfInfo, }) {
|
||||
messageHandler.on('GetDoc', ({ pdfInfo, }) => {
|
||||
this._numPages = pdfInfo.numPages;
|
||||
loadingTask._capability.resolve(new PDFDocumentProxy(pdfInfo, this));
|
||||
}, this);
|
||||
});
|
||||
|
||||
messageHandler.on('PasswordRequest', function(exception) {
|
||||
messageHandler.on('PasswordRequest', (exception) => {
|
||||
this._passwordCapability = createPromiseCapability();
|
||||
|
||||
if (loadingTask.onPassword) {
|
||||
@ -1996,34 +1996,34 @@ class WorkerTransport {
|
||||
new PasswordException(exception.message, exception.code));
|
||||
}
|
||||
return this._passwordCapability.promise;
|
||||
}, this);
|
||||
});
|
||||
|
||||
messageHandler.on('PasswordException', function(exception) {
|
||||
loadingTask._capability.reject(
|
||||
new PasswordException(exception.message, exception.code));
|
||||
}, this);
|
||||
});
|
||||
|
||||
messageHandler.on('InvalidPDF', function(exception) {
|
||||
loadingTask._capability.reject(
|
||||
new InvalidPDFException(exception.message));
|
||||
}, this);
|
||||
});
|
||||
|
||||
messageHandler.on('MissingPDF', function(exception) {
|
||||
loadingTask._capability.reject(
|
||||
new MissingPDFException(exception.message));
|
||||
}, this);
|
||||
});
|
||||
|
||||
messageHandler.on('UnexpectedResponse', function(exception) {
|
||||
loadingTask._capability.reject(
|
||||
new UnexpectedResponseException(exception.message, exception.status));
|
||||
}, this);
|
||||
});
|
||||
|
||||
messageHandler.on('UnknownError', function(exception) {
|
||||
loadingTask._capability.reject(
|
||||
new UnknownErrorException(exception.message, exception.details));
|
||||
}, this);
|
||||
});
|
||||
|
||||
messageHandler.on('DataLoaded', function(data) {
|
||||
messageHandler.on('DataLoaded', (data) => {
|
||||
// For consistency: Ensure that progress is always reported when the
|
||||
// entire PDF file has been loaded, regardless of how it was fetched.
|
||||
if (loadingTask.onProgress) {
|
||||
@ -2033,9 +2033,9 @@ class WorkerTransport {
|
||||
});
|
||||
}
|
||||
this.downloadInfoCapability.resolve(data);
|
||||
}, this);
|
||||
});
|
||||
|
||||
messageHandler.on('StartRenderPage', function(data) {
|
||||
messageHandler.on('StartRenderPage', (data) => {
|
||||
if (this.destroyed) {
|
||||
return; // Ignore any pending requests if the worker was terminated.
|
||||
}
|
||||
@ -2043,9 +2043,9 @@ class WorkerTransport {
|
||||
const page = this.pageCache[data.pageIndex];
|
||||
page._stats.timeEnd('Page Request');
|
||||
page._startRenderPage(data.transparency, data.intent);
|
||||
}, this);
|
||||
});
|
||||
|
||||
messageHandler.on('commonobj', function(data) {
|
||||
messageHandler.on('commonobj', (data) => {
|
||||
if (this.destroyed) {
|
||||
return; // Ignore any pending requests if the worker was terminated.
|
||||
}
|
||||
@ -2100,9 +2100,9 @@ class WorkerTransport {
|
||||
default:
|
||||
throw new Error(`Got unknown common object type ${type}`);
|
||||
}
|
||||
}, this);
|
||||
});
|
||||
|
||||
messageHandler.on('obj', function(data) {
|
||||
messageHandler.on('obj', (data) => {
|
||||
if (this.destroyed) {
|
||||
// Ignore any pending requests if the worker was terminated.
|
||||
return undefined;
|
||||
@ -2149,9 +2149,9 @@ class WorkerTransport {
|
||||
throw new Error(`Got unknown object type ${type}`);
|
||||
}
|
||||
return undefined;
|
||||
}, this);
|
||||
});
|
||||
|
||||
messageHandler.on('DocProgress', function(data) {
|
||||
messageHandler.on('DocProgress', (data) => {
|
||||
if (this.destroyed) {
|
||||
return; // Ignore any pending requests if the worker was terminated.
|
||||
}
|
||||
@ -2162,11 +2162,12 @@ class WorkerTransport {
|
||||
total: data.total,
|
||||
});
|
||||
}
|
||||
}, this);
|
||||
});
|
||||
|
||||
messageHandler.on('UnsupportedFeature', this._onUnsupportedFeature, this);
|
||||
messageHandler.on('UnsupportedFeature',
|
||||
this._onUnsupportedFeature.bind(this));
|
||||
|
||||
messageHandler.on('JpegDecode', function(data) {
|
||||
messageHandler.on('JpegDecode', (data) => {
|
||||
if (this.destroyed) {
|
||||
return Promise.reject(new Error('Worker was destroyed'));
|
||||
}
|
||||
@ -2227,16 +2228,14 @@ class WorkerTransport {
|
||||
};
|
||||
img.src = imageUrl;
|
||||
});
|
||||
}, this);
|
||||
});
|
||||
|
||||
messageHandler.on('FetchBuiltInCMap', function(data) {
|
||||
messageHandler.on('FetchBuiltInCMap', (data) => {
|
||||
if (this.destroyed) {
|
||||
return Promise.reject(new Error('Worker was destroyed'));
|
||||
}
|
||||
return this.CMapReaderFactory.fetch({
|
||||
name: data.name,
|
||||
});
|
||||
}, this);
|
||||
return this.CMapReaderFactory.fetch(data);
|
||||
});
|
||||
}
|
||||
|
||||
_onUnsupportedFeature({ featureId, }) {
|
||||
|
@ -30,11 +30,11 @@ const StreamKind = {
|
||||
START_COMPLETE: 8,
|
||||
};
|
||||
|
||||
async function resolveCall(fn, args, thisArg = null) {
|
||||
async function resolveCall(fn, args) {
|
||||
if (!fn) {
|
||||
return undefined;
|
||||
}
|
||||
return fn.apply(thisArg, args);
|
||||
return fn.apply(null, args);
|
||||
}
|
||||
|
||||
function wrapReason(reason) {
|
||||
@ -100,8 +100,8 @@ function MessageHandler(sourceName, targetName, comObj) {
|
||||
if (data.callbackId) {
|
||||
let sourceName = this.sourceName;
|
||||
let targetName = data.sourceName;
|
||||
Promise.resolve().then(function () {
|
||||
return action[0].call(action[1], data.data);
|
||||
Promise.resolve().then(function() {
|
||||
return action(data.data);
|
||||
}).then((result) => {
|
||||
comObj.postMessage({
|
||||
sourceName,
|
||||
@ -122,7 +122,7 @@ function MessageHandler(sourceName, targetName, comObj) {
|
||||
} else if (data.streamId) {
|
||||
this._createStreamSink(data);
|
||||
} else {
|
||||
action[0].call(action[1], data.data);
|
||||
action(data.data);
|
||||
}
|
||||
} else {
|
||||
throw new Error(`Unknown action from worker: ${data.action}`);
|
||||
@ -132,12 +132,12 @@ function MessageHandler(sourceName, targetName, comObj) {
|
||||
}
|
||||
|
||||
MessageHandler.prototype = {
|
||||
on(actionName, handler, scope) {
|
||||
on(actionName, handler) {
|
||||
var ah = this.actionHandler;
|
||||
if (ah[actionName]) {
|
||||
throw new Error(`There is already an actionName called "${actionName}"`);
|
||||
}
|
||||
ah[actionName] = [handler, scope];
|
||||
ah[actionName] = handler;
|
||||
},
|
||||
/**
|
||||
* Sends a message to the comObj to invoke the action with the supplied data.
|
||||
@ -318,7 +318,7 @@ MessageHandler.prototype = {
|
||||
streamSink.sinkCapability.resolve();
|
||||
streamSink.ready = streamSink.sinkCapability.promise;
|
||||
this.streamSinks[streamId] = streamSink;
|
||||
resolveCall(action[0], [data.data, streamSink], action[1]).then(() => {
|
||||
resolveCall(action, [data.data, streamSink]).then(() => {
|
||||
comObj.postMessage({
|
||||
sourceName,
|
||||
targetName,
|
||||
|
Loading…
Reference in New Issue
Block a user