src/worker/async_proxy.js
function AsyncProxy({ path='openpgp.worker.js', n = 1, workers = [], config } = {}) {
  /**
   * Message handling
   */
  const handleMessage = workerId => event => {
    const msg = event.data;
    switch (msg.event) {
      case 'loaded':
        this.workers[workerId].loadedResolve(true);
        break;
      case 'method-return':
        if (msg.err) {
          // fail
          const err = new Error(msg.err);
          // add worker stack
          err.workerStack = msg.stack;
          this.tasks[msg.id].reject(err);
        } else {
          // success
          this.tasks[msg.id].resolve(msg.data);
        }
        delete this.tasks[msg.id];
        this.workers[workerId].requests--;
        break;
      case 'request-seed':
        this.seedRandom(workerId, msg.amount);
        break;
      default:
        throw new Error('Unknown Worker Event.');
    }
  };

  if (workers.length) {
    this.workers = workers;
  }
  else {
    this.workers = [];
    while (this.workers.length < n) {
      this.workers.push(new Worker(path));
    }
  }

  let workerId = 0;
  this.workers.forEach(worker => {
    worker.loadedPromise = new Promise(resolve => {
      worker.loadedResolve = resolve;
    });
    worker.requests = 0;
    worker.onmessage = handleMessage(workerId++);
    worker.onerror = e => {
      worker.loadedResolve(false);
      console.error('Unhandled error in openpgp worker: ' + e.message + ' (' + e.filename + ':' + e.lineno + ')');
      return false;
    };

    if (config) {
      worker.postMessage({ event:'configure', config });
    }
  });

  // Cannot rely on task order being maintained, use object keyed by request ID to track tasks
  this.tasks = {};
  this.currentID = 0;
}
function AsyncProxy({ path='openpgp.worker.js', n = 1, workers = [], config } = {}) {
  /**
   * Message handling
   */
  const handleMessage = workerId => event => {
    const msg = event.data;
    switch (msg.event) {
      case 'loaded':
        this.workers[workerId].loadedResolve(true);
        break;
      case 'method-return':
        if (msg.err) {
          // fail
          const err = new Error(msg.err);
          // add worker stack
          err.workerStack = msg.stack;
          this.tasks[msg.id].reject(err);
        } else {
          // success
          this.tasks[msg.id].resolve(msg.data);
        }
        delete this.tasks[msg.id];
        this.workers[workerId].requests--;
        break;
      case 'request-seed':
        this.seedRandom(workerId, msg.amount);
        break;
      default:
        throw new Error('Unknown Worker Event.');
    }
  };

  if (workers.length) {
    this.workers = workers;
  }
  else {
    this.workers = [];
    while (this.workers.length < n) {
      this.workers.push(new Worker(path));
    }
  }

  let workerId = 0;
  this.workers.forEach(worker => {
    worker.loadedPromise = new Promise(resolve => {
      worker.loadedResolve = resolve;
    });
    worker.requests = 0;
    worker.onmessage = handleMessage(workerId++);
    worker.onerror = e => {
      worker.loadedResolve(false);
      console.error('Unhandled error in openpgp worker: ' + e.message + ' (' + e.filename + ':' + e.lineno + ')');
      return false;
    };

    if (config) {
      worker.postMessage({ event:'configure', config });
    }
  });

  // Cannot rely on task order being maintained, use object keyed by request ID to track tasks
  this.tasks = {};
  this.currentID = 0;
}
function AsyncProxy ( { path = 'openpgp.worker.js' , n = 1 , workers = [ ] , config } = { } ) { /**
   * Message handling
   */ const handleMessage = workerId => event => { const msg = event . data ; switch ( msg . event ) { case 'loaded' : this . workers [ workerId ] . loadedResolve ( true ) ; break ; case 'method-return' : if ( msg . err ) { // fail const err = new Error ( msg . err ) ; // add worker stack err . workerStack = msg . stack ; this . tasks [ msg . id ] . reject ( err ) ; } else { // success this . tasks [ msg . id ] . resolve ( msg . data ) ; } delete this . tasks [ msg . id ] ; this . workers [ workerId ] . requests -- ; break ; case 'request-seed' : this . seedRandom ( workerId , msg . amount ) ; break ; default : throw new Error ( 'Unknown Worker Event.' ) ; } } ; if ( workers . length ) { this . workers = workers ; } else { this . workers = [ ] ; while ( this . workers . length < n ) { this . workers . push ( new Worker ( path ) ) ; } } let workerId = 0 ; this . workers . forEach ( worker => { worker . loadedPromise = new Promise ( resolve => { worker . loadedResolve = resolve ; } ) ; worker . requests = 0 ; worker . onmessage = handleMessage ( workerId ++ ) ; worker . onerror = e => { worker . loadedResolve ( false ) ; console . error ( 'Unhandled error in openpgp worker: ' + e . message + ' (' + e . filename + ':' + e . lineno + ')' ) ; return false ; } ; if ( config ) { worker . postMessage ( { event : 'configure' , config } ) ; } } ) ; // Cannot rely on task order being maintained, use object keyed by request ID to track tasks this . tasks = { } ; this . currentID = 0 ; }
Initializes a new proxy and loads the web worker
@param {String} path            The path to the worker or 'openpgp.worker.js' by default
@param {Number} n               number of workers to initialize if path given
@param {Object} config          config The worker configuration
@param {Array<Object>} worker   alternative to path parameter: web worker initialized with 'openpgp.worker.js'
@constructor
Initializes a new proxy and loads the web worker