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:
Jonas Jenwald 2019-08-31 14:16:06 +02:00
parent d1e6d427cd
commit 055f03938b
2 changed files with 40 additions and 41 deletions

View File

@ -1875,7 +1875,7 @@ class WorkerTransport {
setupMessageHandler() { setupMessageHandler() {
const { messageHandler, loadingTask, } = this; const { messageHandler, loadingTask, } = this;
messageHandler.on('GetReader', function(data, sink) { messageHandler.on('GetReader', (data, sink) => {
assert(this._networkStream); assert(this._networkStream);
this._fullReader = this._networkStream.getFullReader(); this._fullReader = this._networkStream.getFullReader();
this._fullReader.onProgress = (evt) => { this._fullReader.onProgress = (evt) => {
@ -1902,9 +1902,9 @@ class WorkerTransport {
sink.onCancel = (reason) => { sink.onCancel = (reason) => {
this._fullReader.cancel(reason); this._fullReader.cancel(reason);
}; };
}, this); });
messageHandler.on('ReaderHeadersReady', function(data) { messageHandler.on('ReaderHeadersReady', (data) => {
const headersCapability = createPromiseCapability(); const headersCapability = createPromiseCapability();
const fullReader = this._fullReader; const fullReader = this._fullReader;
fullReader.headersReady.then(() => { fullReader.headersReady.then(() => {
@ -1932,9 +1932,9 @@ class WorkerTransport {
}, headersCapability.reject); }, headersCapability.reject);
return headersCapability.promise; return headersCapability.promise;
}, this); });
messageHandler.on('GetRangeReader', function(data, sink) { messageHandler.on('GetRangeReader', (data, sink) => {
assert(this._networkStream); assert(this._networkStream);
const rangeReader = const rangeReader =
this._networkStream.getRangeReader(data.begin, data.end); this._networkStream.getRangeReader(data.begin, data.end);
@ -1970,14 +1970,14 @@ class WorkerTransport {
sink.onCancel = (reason) => { sink.onCancel = (reason) => {
rangeReader.cancel(reason); rangeReader.cancel(reason);
}; };
}, this); });
messageHandler.on('GetDoc', function({ pdfInfo, }) { messageHandler.on('GetDoc', ({ pdfInfo, }) => {
this._numPages = pdfInfo.numPages; this._numPages = pdfInfo.numPages;
loadingTask._capability.resolve(new PDFDocumentProxy(pdfInfo, this)); loadingTask._capability.resolve(new PDFDocumentProxy(pdfInfo, this));
}, this); });
messageHandler.on('PasswordRequest', function(exception) { messageHandler.on('PasswordRequest', (exception) => {
this._passwordCapability = createPromiseCapability(); this._passwordCapability = createPromiseCapability();
if (loadingTask.onPassword) { if (loadingTask.onPassword) {
@ -1996,34 +1996,34 @@ class WorkerTransport {
new PasswordException(exception.message, exception.code)); new PasswordException(exception.message, exception.code));
} }
return this._passwordCapability.promise; return this._passwordCapability.promise;
}, this); });
messageHandler.on('PasswordException', function(exception) { messageHandler.on('PasswordException', function(exception) {
loadingTask._capability.reject( loadingTask._capability.reject(
new PasswordException(exception.message, exception.code)); new PasswordException(exception.message, exception.code));
}, this); });
messageHandler.on('InvalidPDF', function(exception) { messageHandler.on('InvalidPDF', function(exception) {
loadingTask._capability.reject( loadingTask._capability.reject(
new InvalidPDFException(exception.message)); new InvalidPDFException(exception.message));
}, this); });
messageHandler.on('MissingPDF', function(exception) { messageHandler.on('MissingPDF', function(exception) {
loadingTask._capability.reject( loadingTask._capability.reject(
new MissingPDFException(exception.message)); new MissingPDFException(exception.message));
}, this); });
messageHandler.on('UnexpectedResponse', function(exception) { messageHandler.on('UnexpectedResponse', function(exception) {
loadingTask._capability.reject( loadingTask._capability.reject(
new UnexpectedResponseException(exception.message, exception.status)); new UnexpectedResponseException(exception.message, exception.status));
}, this); });
messageHandler.on('UnknownError', function(exception) { messageHandler.on('UnknownError', function(exception) {
loadingTask._capability.reject( loadingTask._capability.reject(
new UnknownErrorException(exception.message, exception.details)); 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 // For consistency: Ensure that progress is always reported when the
// entire PDF file has been loaded, regardless of how it was fetched. // entire PDF file has been loaded, regardless of how it was fetched.
if (loadingTask.onProgress) { if (loadingTask.onProgress) {
@ -2033,9 +2033,9 @@ class WorkerTransport {
}); });
} }
this.downloadInfoCapability.resolve(data); this.downloadInfoCapability.resolve(data);
}, this); });
messageHandler.on('StartRenderPage', function(data) { messageHandler.on('StartRenderPage', (data) => {
if (this.destroyed) { if (this.destroyed) {
return; // Ignore any pending requests if the worker was terminated. return; // Ignore any pending requests if the worker was terminated.
} }
@ -2043,9 +2043,9 @@ class WorkerTransport {
const page = this.pageCache[data.pageIndex]; const page = this.pageCache[data.pageIndex];
page._stats.timeEnd('Page Request'); page._stats.timeEnd('Page Request');
page._startRenderPage(data.transparency, data.intent); page._startRenderPage(data.transparency, data.intent);
}, this); });
messageHandler.on('commonobj', function(data) { messageHandler.on('commonobj', (data) => {
if (this.destroyed) { if (this.destroyed) {
return; // Ignore any pending requests if the worker was terminated. return; // Ignore any pending requests if the worker was terminated.
} }
@ -2100,9 +2100,9 @@ class WorkerTransport {
default: default:
throw new Error(`Got unknown common object type ${type}`); throw new Error(`Got unknown common object type ${type}`);
} }
}, this); });
messageHandler.on('obj', function(data) { messageHandler.on('obj', (data) => {
if (this.destroyed) { if (this.destroyed) {
// Ignore any pending requests if the worker was terminated. // Ignore any pending requests if the worker was terminated.
return undefined; return undefined;
@ -2149,9 +2149,9 @@ class WorkerTransport {
throw new Error(`Got unknown object type ${type}`); throw new Error(`Got unknown object type ${type}`);
} }
return undefined; return undefined;
}, this); });
messageHandler.on('DocProgress', function(data) { messageHandler.on('DocProgress', (data) => {
if (this.destroyed) { if (this.destroyed) {
return; // Ignore any pending requests if the worker was terminated. return; // Ignore any pending requests if the worker was terminated.
} }
@ -2162,11 +2162,12 @@ class WorkerTransport {
total: data.total, 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) { if (this.destroyed) {
return Promise.reject(new Error('Worker was destroyed')); return Promise.reject(new Error('Worker was destroyed'));
} }
@ -2227,16 +2228,14 @@ class WorkerTransport {
}; };
img.src = imageUrl; img.src = imageUrl;
}); });
}, this); });
messageHandler.on('FetchBuiltInCMap', function(data) { messageHandler.on('FetchBuiltInCMap', (data) => {
if (this.destroyed) { if (this.destroyed) {
return Promise.reject(new Error('Worker was destroyed')); return Promise.reject(new Error('Worker was destroyed'));
} }
return this.CMapReaderFactory.fetch({ return this.CMapReaderFactory.fetch(data);
name: data.name,
}); });
}, this);
} }
_onUnsupportedFeature({ featureId, }) { _onUnsupportedFeature({ featureId, }) {

View File

@ -30,11 +30,11 @@ const StreamKind = {
START_COMPLETE: 8, START_COMPLETE: 8,
}; };
async function resolveCall(fn, args, thisArg = null) { async function resolveCall(fn, args) {
if (!fn) { if (!fn) {
return undefined; return undefined;
} }
return fn.apply(thisArg, args); return fn.apply(null, args);
} }
function wrapReason(reason) { function wrapReason(reason) {
@ -101,7 +101,7 @@ function MessageHandler(sourceName, targetName, comObj) {
let sourceName = this.sourceName; let sourceName = this.sourceName;
let targetName = data.sourceName; let targetName = data.sourceName;
Promise.resolve().then(function() { Promise.resolve().then(function() {
return action[0].call(action[1], data.data); return action(data.data);
}).then((result) => { }).then((result) => {
comObj.postMessage({ comObj.postMessage({
sourceName, sourceName,
@ -122,7 +122,7 @@ function MessageHandler(sourceName, targetName, comObj) {
} else if (data.streamId) { } else if (data.streamId) {
this._createStreamSink(data); this._createStreamSink(data);
} else { } else {
action[0].call(action[1], data.data); action(data.data);
} }
} else { } else {
throw new Error(`Unknown action from worker: ${data.action}`); throw new Error(`Unknown action from worker: ${data.action}`);
@ -132,12 +132,12 @@ function MessageHandler(sourceName, targetName, comObj) {
} }
MessageHandler.prototype = { MessageHandler.prototype = {
on(actionName, handler, scope) { on(actionName, handler) {
var ah = this.actionHandler; var ah = this.actionHandler;
if (ah[actionName]) { if (ah[actionName]) {
throw new Error(`There is already an actionName called "${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. * Sends a message to the comObj to invoke the action with the supplied data.
@ -318,7 +318,7 @@ MessageHandler.prototype = {
streamSink.sinkCapability.resolve(); streamSink.sinkCapability.resolve();
streamSink.ready = streamSink.sinkCapability.promise; streamSink.ready = streamSink.sinkCapability.promise;
this.streamSinks[streamId] = streamSink; this.streamSinks[streamId] = streamSink;
resolveCall(action[0], [data.data, streamSink], action[1]).then(() => { resolveCall(action, [data.data, streamSink]).then(() => {
comObj.postMessage({ comObj.postMessage({
sourceName, sourceName,
targetName, targetName,