Skip to content

Commit 055f039

Browse files
committed
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.
1 parent d1e6d42 commit 055f039

File tree

2 files changed

+40
-41
lines changed

2 files changed

+40
-41
lines changed

src/display/api.js

Lines changed: 32 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1875,7 +1875,7 @@ class WorkerTransport {
18751875
setupMessageHandler() {
18761876
const { messageHandler, loadingTask, } = this;
18771877

1878-
messageHandler.on('GetReader', function(data, sink) {
1878+
messageHandler.on('GetReader', (data, sink) => {
18791879
assert(this._networkStream);
18801880
this._fullReader = this._networkStream.getFullReader();
18811881
this._fullReader.onProgress = (evt) => {
@@ -1902,9 +1902,9 @@ class WorkerTransport {
19021902
sink.onCancel = (reason) => {
19031903
this._fullReader.cancel(reason);
19041904
};
1905-
}, this);
1905+
});
19061906

1907-
messageHandler.on('ReaderHeadersReady', function(data) {
1907+
messageHandler.on('ReaderHeadersReady', (data) => {
19081908
const headersCapability = createPromiseCapability();
19091909
const fullReader = this._fullReader;
19101910
fullReader.headersReady.then(() => {
@@ -1932,9 +1932,9 @@ class WorkerTransport {
19321932
}, headersCapability.reject);
19331933

19341934
return headersCapability.promise;
1935-
}, this);
1935+
});
19361936

1937-
messageHandler.on('GetRangeReader', function(data, sink) {
1937+
messageHandler.on('GetRangeReader', (data, sink) => {
19381938
assert(this._networkStream);
19391939
const rangeReader =
19401940
this._networkStream.getRangeReader(data.begin, data.end);
@@ -1970,14 +1970,14 @@ class WorkerTransport {
19701970
sink.onCancel = (reason) => {
19711971
rangeReader.cancel(reason);
19721972
};
1973-
}, this);
1973+
});
19741974

1975-
messageHandler.on('GetDoc', function({ pdfInfo, }) {
1975+
messageHandler.on('GetDoc', ({ pdfInfo, }) => {
19761976
this._numPages = pdfInfo.numPages;
19771977
loadingTask._capability.resolve(new PDFDocumentProxy(pdfInfo, this));
1978-
}, this);
1978+
});
19791979

1980-
messageHandler.on('PasswordRequest', function(exception) {
1980+
messageHandler.on('PasswordRequest', (exception) => {
19811981
this._passwordCapability = createPromiseCapability();
19821982

19831983
if (loadingTask.onPassword) {
@@ -1996,34 +1996,34 @@ class WorkerTransport {
19961996
new PasswordException(exception.message, exception.code));
19971997
}
19981998
return this._passwordCapability.promise;
1999-
}, this);
1999+
});
20002000

20012001
messageHandler.on('PasswordException', function(exception) {
20022002
loadingTask._capability.reject(
20032003
new PasswordException(exception.message, exception.code));
2004-
}, this);
2004+
});
20052005

20062006
messageHandler.on('InvalidPDF', function(exception) {
20072007
loadingTask._capability.reject(
20082008
new InvalidPDFException(exception.message));
2009-
}, this);
2009+
});
20102010

20112011
messageHandler.on('MissingPDF', function(exception) {
20122012
loadingTask._capability.reject(
20132013
new MissingPDFException(exception.message));
2014-
}, this);
2014+
});
20152015

20162016
messageHandler.on('UnexpectedResponse', function(exception) {
20172017
loadingTask._capability.reject(
20182018
new UnexpectedResponseException(exception.message, exception.status));
2019-
}, this);
2019+
});
20202020

20212021
messageHandler.on('UnknownError', function(exception) {
20222022
loadingTask._capability.reject(
20232023
new UnknownErrorException(exception.message, exception.details));
2024-
}, this);
2024+
});
20252025

2026-
messageHandler.on('DataLoaded', function(data) {
2026+
messageHandler.on('DataLoaded', (data) => {
20272027
// For consistency: Ensure that progress is always reported when the
20282028
// entire PDF file has been loaded, regardless of how it was fetched.
20292029
if (loadingTask.onProgress) {
@@ -2033,19 +2033,19 @@ class WorkerTransport {
20332033
});
20342034
}
20352035
this.downloadInfoCapability.resolve(data);
2036-
}, this);
2036+
});
20372037

2038-
messageHandler.on('StartRenderPage', function(data) {
2038+
messageHandler.on('StartRenderPage', (data) => {
20392039
if (this.destroyed) {
20402040
return; // Ignore any pending requests if the worker was terminated.
20412041
}
20422042

20432043
const page = this.pageCache[data.pageIndex];
20442044
page._stats.timeEnd('Page Request');
20452045
page._startRenderPage(data.transparency, data.intent);
2046-
}, this);
2046+
});
20472047

2048-
messageHandler.on('commonobj', function(data) {
2048+
messageHandler.on('commonobj', (data) => {
20492049
if (this.destroyed) {
20502050
return; // Ignore any pending requests if the worker was terminated.
20512051
}
@@ -2100,9 +2100,9 @@ class WorkerTransport {
21002100
default:
21012101
throw new Error(`Got unknown common object type ${type}`);
21022102
}
2103-
}, this);
2103+
});
21042104

2105-
messageHandler.on('obj', function(data) {
2105+
messageHandler.on('obj', (data) => {
21062106
if (this.destroyed) {
21072107
// Ignore any pending requests if the worker was terminated.
21082108
return undefined;
@@ -2149,9 +2149,9 @@ class WorkerTransport {
21492149
throw new Error(`Got unknown object type ${type}`);
21502150
}
21512151
return undefined;
2152-
}, this);
2152+
});
21532153

2154-
messageHandler.on('DocProgress', function(data) {
2154+
messageHandler.on('DocProgress', (data) => {
21552155
if (this.destroyed) {
21562156
return; // Ignore any pending requests if the worker was terminated.
21572157
}
@@ -2162,11 +2162,12 @@ class WorkerTransport {
21622162
total: data.total,
21632163
});
21642164
}
2165-
}, this);
2165+
});
21662166

2167-
messageHandler.on('UnsupportedFeature', this._onUnsupportedFeature, this);
2167+
messageHandler.on('UnsupportedFeature',
2168+
this._onUnsupportedFeature.bind(this));
21682169

2169-
messageHandler.on('JpegDecode', function(data) {
2170+
messageHandler.on('JpegDecode', (data) => {
21702171
if (this.destroyed) {
21712172
return Promise.reject(new Error('Worker was destroyed'));
21722173
}
@@ -2227,16 +2228,14 @@ class WorkerTransport {
22272228
};
22282229
img.src = imageUrl;
22292230
});
2230-
}, this);
2231+
});
22312232

2232-
messageHandler.on('FetchBuiltInCMap', function(data) {
2233+
messageHandler.on('FetchBuiltInCMap', (data) => {
22332234
if (this.destroyed) {
22342235
return Promise.reject(new Error('Worker was destroyed'));
22352236
}
2236-
return this.CMapReaderFactory.fetch({
2237-
name: data.name,
2238-
});
2239-
}, this);
2237+
return this.CMapReaderFactory.fetch(data);
2238+
});
22402239
}
22412240

22422241
_onUnsupportedFeature({ featureId, }) {

src/shared/message_handler.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,11 @@ const StreamKind = {
3030
START_COMPLETE: 8,
3131
};
3232

33-
async function resolveCall(fn, args, thisArg = null) {
33+
async function resolveCall(fn, args) {
3434
if (!fn) {
3535
return undefined;
3636
}
37-
return fn.apply(thisArg, args);
37+
return fn.apply(null, args);
3838
}
3939

4040
function wrapReason(reason) {
@@ -100,8 +100,8 @@ function MessageHandler(sourceName, targetName, comObj) {
100100
if (data.callbackId) {
101101
let sourceName = this.sourceName;
102102
let targetName = data.sourceName;
103-
Promise.resolve().then(function () {
104-
return action[0].call(action[1], data.data);
103+
Promise.resolve().then(function() {
104+
return action(data.data);
105105
}).then((result) => {
106106
comObj.postMessage({
107107
sourceName,
@@ -122,7 +122,7 @@ function MessageHandler(sourceName, targetName, comObj) {
122122
} else if (data.streamId) {
123123
this._createStreamSink(data);
124124
} else {
125-
action[0].call(action[1], data.data);
125+
action(data.data);
126126
}
127127
} else {
128128
throw new Error(`Unknown action from worker: ${data.action}`);
@@ -132,12 +132,12 @@ function MessageHandler(sourceName, targetName, comObj) {
132132
}
133133

134134
MessageHandler.prototype = {
135-
on(actionName, handler, scope) {
135+
on(actionName, handler) {
136136
var ah = this.actionHandler;
137137
if (ah[actionName]) {
138138
throw new Error(`There is already an actionName called "${actionName}"`);
139139
}
140-
ah[actionName] = [handler, scope];
140+
ah[actionName] = handler;
141141
},
142142
/**
143143
* Sends a message to the comObj to invoke the action with the supplied data.
@@ -318,7 +318,7 @@ MessageHandler.prototype = {
318318
streamSink.sinkCapability.resolve();
319319
streamSink.ready = streamSink.sinkCapability.promise;
320320
this.streamSinks[streamId] = streamSink;
321-
resolveCall(action[0], [data.data, streamSink], action[1]).then(() => {
321+
resolveCall(action, [data.data, streamSink]).then(() => {
322322
comObj.postMessage({
323323
sourceName,
324324
targetName,

0 commit comments

Comments
 (0)