[MessageHandler] Convert the code to a proper class
This commit is contained in:
parent
5d5733c0a7
commit
0293222b96
@ -61,7 +61,8 @@ function wrapReason(reason) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function MessageHandler(sourceName, targetName, comObj) {
|
class MessageHandler {
|
||||||
|
constructor(sourceName, targetName, comObj) {
|
||||||
this.sourceName = sourceName;
|
this.sourceName = sourceName;
|
||||||
this.targetName = targetName;
|
this.targetName = targetName;
|
||||||
this.comObj = comObj;
|
this.comObj = comObj;
|
||||||
@ -136,7 +137,6 @@ function MessageHandler(sourceName, targetName, comObj) {
|
|||||||
comObj.addEventListener('message', this._onComObjOnMessage);
|
comObj.addEventListener('message', this._onComObjOnMessage);
|
||||||
}
|
}
|
||||||
|
|
||||||
MessageHandler.prototype = {
|
|
||||||
on(actionName, handler) {
|
on(actionName, handler) {
|
||||||
if (typeof PDFJSDev === 'undefined' ||
|
if (typeof PDFJSDev === 'undefined' ||
|
||||||
PDFJSDev.test('!PRODUCTION || TESTING')) {
|
PDFJSDev.test('!PRODUCTION || TESTING')) {
|
||||||
@ -148,7 +148,8 @@ MessageHandler.prototype = {
|
|||||||
throw new Error(`There is already an actionName called "${actionName}"`);
|
throw new Error(`There is already an actionName called "${actionName}"`);
|
||||||
}
|
}
|
||||||
ah[actionName] = handler;
|
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.
|
||||||
* @param {string} actionName - Action to call.
|
* @param {string} actionName - Action to call.
|
||||||
@ -156,13 +157,14 @@ MessageHandler.prototype = {
|
|||||||
* @param {Array} [transfers] - List of transfers/ArrayBuffers.
|
* @param {Array} [transfers] - List of transfers/ArrayBuffers.
|
||||||
*/
|
*/
|
||||||
send(actionName, data, transfers) {
|
send(actionName, data, transfers) {
|
||||||
this.postMessage({
|
this._postMessage({
|
||||||
sourceName: this.sourceName,
|
sourceName: this.sourceName,
|
||||||
targetName: this.targetName,
|
targetName: this.targetName,
|
||||||
action: actionName,
|
action: actionName,
|
||||||
data,
|
data,
|
||||||
}, transfers);
|
}, transfers);
|
||||||
},
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 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.
|
||||||
* Expects that the other side will callback with the response.
|
* Expects that the other side will callback with the response.
|
||||||
@ -176,7 +178,7 @@ MessageHandler.prototype = {
|
|||||||
const capability = createPromiseCapability();
|
const capability = createPromiseCapability();
|
||||||
this.callbackCapabilities[callbackId] = capability;
|
this.callbackCapabilities[callbackId] = capability;
|
||||||
try {
|
try {
|
||||||
this.postMessage({
|
this._postMessage({
|
||||||
sourceName: this.sourceName,
|
sourceName: this.sourceName,
|
||||||
targetName: this.targetName,
|
targetName: this.targetName,
|
||||||
action: actionName,
|
action: actionName,
|
||||||
@ -187,7 +189,8 @@ MessageHandler.prototype = {
|
|||||||
capability.reject(ex);
|
capability.reject(ex);
|
||||||
}
|
}
|
||||||
return capability.promise;
|
return capability.promise;
|
||||||
},
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 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.
|
||||||
* Expect that the other side will callback to signal 'start_complete'.
|
* Expect that the other side will callback to signal 'start_complete'.
|
||||||
@ -214,7 +217,7 @@ MessageHandler.prototype = {
|
|||||||
cancelCall: null,
|
cancelCall: null,
|
||||||
isClosed: false,
|
isClosed: false,
|
||||||
};
|
};
|
||||||
this.postMessage({
|
this._postMessage({
|
||||||
sourceName,
|
sourceName,
|
||||||
targetName,
|
targetName,
|
||||||
action: actionName,
|
action: actionName,
|
||||||
@ -257,8 +260,11 @@ MessageHandler.prototype = {
|
|||||||
return cancelCapability.promise;
|
return cancelCapability.promise;
|
||||||
},
|
},
|
||||||
}, queueingStrategy);
|
}, queueingStrategy);
|
||||||
},
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @private
|
||||||
|
*/
|
||||||
_createStreamSink(data) {
|
_createStreamSink(data) {
|
||||||
const self = this;
|
const self = this;
|
||||||
const action = this.actionHandler[data.action];
|
const action = this.actionHandler[data.action];
|
||||||
@ -281,7 +287,7 @@ MessageHandler.prototype = {
|
|||||||
this.sinkCapability = createPromiseCapability();
|
this.sinkCapability = createPromiseCapability();
|
||||||
this.ready = this.sinkCapability.promise;
|
this.ready = this.sinkCapability.promise;
|
||||||
}
|
}
|
||||||
self.postMessage({
|
self._postMessage({
|
||||||
sourceName,
|
sourceName,
|
||||||
targetName,
|
targetName,
|
||||||
stream: StreamKind.ENQUEUE,
|
stream: StreamKind.ENQUEUE,
|
||||||
@ -349,8 +355,11 @@ MessageHandler.prototype = {
|
|||||||
reason: wrapReason(reason),
|
reason: wrapReason(reason),
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
},
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @private
|
||||||
|
*/
|
||||||
_processStreamMessage(data) {
|
_processStreamMessage(data) {
|
||||||
const streamId = data.streamId;
|
const streamId = data.streamId;
|
||||||
const sourceName = this.sourceName;
|
const sourceName = this.sourceName;
|
||||||
@ -482,8 +491,11 @@ MessageHandler.prototype = {
|
|||||||
default:
|
default:
|
||||||
throw new Error('Unexpected stream case');
|
throw new Error('Unexpected stream case');
|
||||||
}
|
}
|
||||||
},
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @private
|
||||||
|
*/
|
||||||
async _deleteStreamController(streamId) {
|
async _deleteStreamController(streamId) {
|
||||||
// Delete the `streamController` only when the start, pull, and cancel
|
// Delete the `streamController` only when the start, pull, and cancel
|
||||||
// capabilities have settled, to prevent `TypeError`s.
|
// capabilities have settled, to prevent `TypeError`s.
|
||||||
@ -495,26 +507,26 @@ MessageHandler.prototype = {
|
|||||||
return capability && capability.promise.catch(function() { });
|
return capability && capability.promise.catch(function() { });
|
||||||
}));
|
}));
|
||||||
delete this.streamControllers[streamId];
|
delete this.streamControllers[streamId];
|
||||||
},
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Sends raw message to the comObj.
|
* Sends raw message to the comObj.
|
||||||
* @private
|
|
||||||
* @param {Object} message - Raw message.
|
* @param {Object} message - Raw message.
|
||||||
* @param transfers List of transfers/ArrayBuffers, or undefined.
|
* @param transfers List of transfers/ArrayBuffers, or undefined.
|
||||||
|
* @private
|
||||||
*/
|
*/
|
||||||
postMessage(message, transfers) {
|
_postMessage(message, transfers) {
|
||||||
if (transfers && this.postMessageTransfers) {
|
if (transfers && this.postMessageTransfers) {
|
||||||
this.comObj.postMessage(message, transfers);
|
this.comObj.postMessage(message, transfers);
|
||||||
} else {
|
} else {
|
||||||
this.comObj.postMessage(message);
|
this.comObj.postMessage(message);
|
||||||
}
|
}
|
||||||
},
|
}
|
||||||
|
|
||||||
destroy() {
|
destroy() {
|
||||||
this.comObj.removeEventListener('message', this._onComObjOnMessage);
|
this.comObj.removeEventListener('message', this._onComObjOnMessage);
|
||||||
},
|
}
|
||||||
};
|
}
|
||||||
|
|
||||||
export {
|
export {
|
||||||
MessageHandler,
|
MessageHandler,
|
||||||
|
Loading…
Reference in New Issue
Block a user