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) {
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) {
@ -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) {
@ -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) {
@ -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]) {
@ -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