Merge pull request #11212 from Snuffleupagus/MessageHandler-deleteStreamController

[MessageHandler] Some additional (small) clean-up of the code
This commit is contained in:
Tim van der Meij 2019-10-06 19:23:13 +02:00 committed by GitHub
commit 5fb3eb7728
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -31,7 +31,13 @@ const StreamKind = {
}; };
function wrapReason(reason) { 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; return reason;
} }
switch (reason.name) { switch (reason.name) {
@ -334,20 +340,6 @@ MessageHandler.prototype = {
const streamId = data.streamId; const streamId = data.streamId;
const comObj = this.comObj; 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) { switch (data.stream) {
case StreamKind.START_COMPLETE: case StreamKind.START_COMPLETE:
if (data.success) { if (data.success) {
@ -423,14 +415,14 @@ MessageHandler.prototype = {
} }
this.streamControllers[streamId].isClosed = true; this.streamControllers[streamId].isClosed = true;
this.streamControllers[streamId].controller.close(); this.streamControllers[streamId].controller.close();
deleteStreamController(); this._deleteStreamController(streamId);
break; break;
case StreamKind.ERROR: case StreamKind.ERROR:
assert(this.streamControllers[streamId], assert(this.streamControllers[streamId],
'error should have stream controller'); 'error should have stream controller');
this.streamControllers[streamId].controller.error( this.streamControllers[streamId].controller.error(
wrapReason(data.reason)); wrapReason(data.reason));
deleteStreamController(); this._deleteStreamController(streamId);
break; break;
case StreamKind.CANCEL_COMPLETE: case StreamKind.CANCEL_COMPLETE:
if (data.success) { if (data.success) {
@ -439,7 +431,7 @@ MessageHandler.prototype = {
this.streamControllers[streamId].cancelCall.reject( this.streamControllers[streamId].cancelCall.reject(
wrapReason(data.reason)); wrapReason(data.reason));
} }
deleteStreamController(); this._deleteStreamController(streamId);
break; break;
case StreamKind.CANCEL: case StreamKind.CANCEL:
if (!this.streamSinks[streamId]) { if (!this.streamSinks[streamId]) {
@ -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. * Sends raw message to the comObj.
* @private * @private