[MessageHandler] Convert the code to a proper class
				
					
				
			This commit is contained in:
		
							parent
							
								
									5d5733c0a7
								
							
						
					
					
						commit
						0293222b96
					
				@ -61,82 +61,82 @@ function wrapReason(reason) {
 | 
				
			|||||||
  }
 | 
					  }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
function MessageHandler(sourceName, targetName, comObj) {
 | 
					class MessageHandler {
 | 
				
			||||||
  this.sourceName = sourceName;
 | 
					  constructor(sourceName, targetName, comObj) {
 | 
				
			||||||
  this.targetName = targetName;
 | 
					    this.sourceName = sourceName;
 | 
				
			||||||
  this.comObj = comObj;
 | 
					    this.targetName = targetName;
 | 
				
			||||||
  this.callbackId = 1;
 | 
					    this.comObj = comObj;
 | 
				
			||||||
  this.streamId = 1;
 | 
					    this.callbackId = 1;
 | 
				
			||||||
  this.postMessageTransfers = true;
 | 
					    this.streamId = 1;
 | 
				
			||||||
  this.streamSinks = Object.create(null);
 | 
					    this.postMessageTransfers = true;
 | 
				
			||||||
  this.streamControllers = Object.create(null);
 | 
					    this.streamSinks = Object.create(null);
 | 
				
			||||||
  this.callbackCapabilities = Object.create(null);
 | 
					    this.streamControllers = Object.create(null);
 | 
				
			||||||
  this.actionHandler = Object.create(null);
 | 
					    this.callbackCapabilities = Object.create(null);
 | 
				
			||||||
 | 
					    this.actionHandler = Object.create(null);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  this._onComObjOnMessage = (event) => {
 | 
					    this._onComObjOnMessage = (event) => {
 | 
				
			||||||
    const data = event.data;
 | 
					      const data = event.data;
 | 
				
			||||||
    if (data.targetName !== this.sourceName) {
 | 
					      if (data.targetName !== this.sourceName) {
 | 
				
			||||||
      return;
 | 
					        return;
 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
    if (data.stream) {
 | 
					 | 
				
			||||||
      this._processStreamMessage(data);
 | 
					 | 
				
			||||||
      return;
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
    if (data.callback) {
 | 
					 | 
				
			||||||
      const callbackId = data.callbackId;
 | 
					 | 
				
			||||||
      const capability = this.callbackCapabilities[callbackId];
 | 
					 | 
				
			||||||
      if (!capability) {
 | 
					 | 
				
			||||||
        throw new Error(`Cannot resolve callback ${callbackId}`);
 | 
					 | 
				
			||||||
      }
 | 
					      }
 | 
				
			||||||
      delete this.callbackCapabilities[callbackId];
 | 
					      if (data.stream) {
 | 
				
			||||||
 | 
					        this._processStreamMessage(data);
 | 
				
			||||||
      if (data.callback === CallbackKind.DATA) {
 | 
					        return;
 | 
				
			||||||
        capability.resolve(data.data);
 | 
					 | 
				
			||||||
      } else if (data.callback === CallbackKind.ERROR) {
 | 
					 | 
				
			||||||
        capability.reject(wrapReason(data.reason));
 | 
					 | 
				
			||||||
      } else {
 | 
					 | 
				
			||||||
        throw new Error('Unexpected callback case');
 | 
					 | 
				
			||||||
      }
 | 
					      }
 | 
				
			||||||
      return;
 | 
					      if (data.callback) {
 | 
				
			||||||
    }
 | 
					        const callbackId = data.callbackId;
 | 
				
			||||||
    const action = this.actionHandler[data.action];
 | 
					        const capability = this.callbackCapabilities[callbackId];
 | 
				
			||||||
    if (!action) {
 | 
					        if (!capability) {
 | 
				
			||||||
      throw new Error(`Unknown action from worker: ${data.action}`);
 | 
					          throw new Error(`Cannot resolve callback ${callbackId}`);
 | 
				
			||||||
    }
 | 
					        }
 | 
				
			||||||
    if (data.callbackId) {
 | 
					        delete this.callbackCapabilities[callbackId];
 | 
				
			||||||
      const sourceName = this.sourceName;
 | 
					
 | 
				
			||||||
      const targetName = data.sourceName;
 | 
					        if (data.callback === CallbackKind.DATA) {
 | 
				
			||||||
      new Promise(function(resolve) {
 | 
					          capability.resolve(data.data);
 | 
				
			||||||
        resolve(action(data.data));
 | 
					        } else if (data.callback === CallbackKind.ERROR) {
 | 
				
			||||||
      }).then(function(result) {
 | 
					          capability.reject(wrapReason(data.reason));
 | 
				
			||||||
        comObj.postMessage({
 | 
					        } else {
 | 
				
			||||||
          sourceName,
 | 
					          throw new Error('Unexpected callback case');
 | 
				
			||||||
          targetName,
 | 
					        }
 | 
				
			||||||
          callback: CallbackKind.DATA,
 | 
					        return;
 | 
				
			||||||
          callbackId: data.callbackId,
 | 
					      }
 | 
				
			||||||
          data: result,
 | 
					      const action = this.actionHandler[data.action];
 | 
				
			||||||
        });
 | 
					      if (!action) {
 | 
				
			||||||
      }, function(reason) {
 | 
					        throw new Error(`Unknown action from worker: ${data.action}`);
 | 
				
			||||||
        comObj.postMessage({
 | 
					      }
 | 
				
			||||||
          sourceName,
 | 
					      if (data.callbackId) {
 | 
				
			||||||
          targetName,
 | 
					        const sourceName = this.sourceName;
 | 
				
			||||||
          callback: CallbackKind.ERROR,
 | 
					        const targetName = data.sourceName;
 | 
				
			||||||
          callbackId: data.callbackId,
 | 
					        new Promise(function(resolve) {
 | 
				
			||||||
          reason: wrapReason(reason),
 | 
					          resolve(action(data.data));
 | 
				
			||||||
        });
 | 
					        }).then(function(result) {
 | 
				
			||||||
      });
 | 
					          comObj.postMessage({
 | 
				
			||||||
      return;
 | 
					            sourceName,
 | 
				
			||||||
    }
 | 
					            targetName,
 | 
				
			||||||
    if (data.streamId) {
 | 
					            callback: CallbackKind.DATA,
 | 
				
			||||||
      this._createStreamSink(data);
 | 
					            callbackId: data.callbackId,
 | 
				
			||||||
      return;
 | 
					            data: result,
 | 
				
			||||||
    }
 | 
					          });
 | 
				
			||||||
    action(data.data);
 | 
					        }, function(reason) {
 | 
				
			||||||
  };
 | 
					          comObj.postMessage({
 | 
				
			||||||
  comObj.addEventListener('message', this._onComObjOnMessage);
 | 
					            sourceName,
 | 
				
			||||||
}
 | 
					            targetName,
 | 
				
			||||||
 | 
					            callback: CallbackKind.ERROR,
 | 
				
			||||||
 | 
					            callbackId: data.callbackId,
 | 
				
			||||||
 | 
					            reason: wrapReason(reason),
 | 
				
			||||||
 | 
					          });
 | 
				
			||||||
 | 
					        });
 | 
				
			||||||
 | 
					        return;
 | 
				
			||||||
 | 
					      }
 | 
				
			||||||
 | 
					      if (data.streamId) {
 | 
				
			||||||
 | 
					        this._createStreamSink(data);
 | 
				
			||||||
 | 
					        return;
 | 
				
			||||||
 | 
					      }
 | 
				
			||||||
 | 
					      action(data.data);
 | 
				
			||||||
 | 
					    };
 | 
				
			||||||
 | 
					    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…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user