Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[MessageHandler] Some additional (small) clean-up of the code #11212

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 23 additions & 18 deletions src/shared/message_handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,13 @@ const StreamKind = {
};

function wrapReason(reason) {
if (typeof reason !== 'object') {
if (typeof PDFJSDev === 'undefined' ||
PDFJSDev.test('!PRODUCTION || TESTING')) {
assert(reason instanceof Error ||
(typeof reason === 'object' && reason !== null),
'wrapReason: Expected "reason" to be a (possibly cloned) Error.');
}
if (typeof reason !== 'object' || reason === null) {
return reason;
}
switch (reason.name) {
Expand Down Expand Up @@ -334,20 +340,6 @@ MessageHandler.prototype = {
const streamId = data.streamId;
const comObj = this.comObj;

let deleteStreamController = () => {
// Delete the `streamController` only when the start, pull, and cancel
// capabilities have settled, to prevent `TypeError`s.
Promise.all([
this.streamControllers[streamId].startCall,
this.streamControllers[streamId].pullCall,
this.streamControllers[streamId].cancelCall
].map(function(capability) {
return capability && capability.promise.catch(function() { });
})).then(() => {
delete this.streamControllers[streamId];
});
};

switch (data.stream) {
case StreamKind.START_COMPLETE:
if (data.success) {
Expand Down Expand Up @@ -423,14 +415,14 @@ MessageHandler.prototype = {
}
this.streamControllers[streamId].isClosed = true;
this.streamControllers[streamId].controller.close();
deleteStreamController();
this._deleteStreamController(streamId);
break;
case StreamKind.ERROR:
assert(this.streamControllers[streamId],
'error should have stream controller');
this.streamControllers[streamId].controller.error(
wrapReason(data.reason));
deleteStreamController();
this._deleteStreamController(streamId);
break;
case StreamKind.CANCEL_COMPLETE:
if (data.success) {
Expand All @@ -439,7 +431,7 @@ MessageHandler.prototype = {
this.streamControllers[streamId].cancelCall.reject(
wrapReason(data.reason));
}
deleteStreamController();
this._deleteStreamController(streamId);
break;
case StreamKind.CANCEL:
if (!this.streamSinks[streamId]) {
Expand Down Expand Up @@ -475,6 +467,19 @@ MessageHandler.prototype = {
}
},

async _deleteStreamController(streamId) {
// Delete the `streamController` only when the start, pull, and cancel
// capabilities have settled, to prevent `TypeError`s.
await Promise.all([
this.streamControllers[streamId].startCall,
this.streamControllers[streamId].pullCall,
this.streamControllers[streamId].cancelCall
].map(function(capability) {
return capability && capability.promise.catch(function() { });
}));
delete this.streamControllers[streamId];
},

/**
* Sends raw message to the comObj.
* @private
Expand Down