oboe.js icon indicating copy to clipboard operation
oboe.js copied to clipboard

Oboe.js cannot be used in a web worker because window is not defined

Open paulsmithkc opened this issue 3 years ago • 2 comments

Line 2774 references window.location instead of location Line 2788 references window.setTimeout instead of setTimeout

Simply changing these two lines so that they refer to the default global object instead window makes oboe.js usable in a web worker.

paulsmithkc avatar Nov 24 '20 16:11 paulsmithkc

Simple example web worker, enabled by this patch:

importScripts('/js/oboe-browser.min.js');

self.onmessage = (evt) => {
  const cmd = evt.data.cmd;
  const url = evt.data.url;
  switch (cmd) {
    case 'start':
      return start(url);
  }
};

const start = (url) => {
  console.log('worker started');
  let chunk = [];

  oboe(url)
    .node('![*]', (item) => {
      if (item) {
        chunk.push(item);
        if (chunk.length >= 1000) {
          self.postMessage({ cmd: 'chunk', chunk: chunk });
          chunk = [];
        }
      }
      return oboe.drop;
    })
    .done((_) => {
      self.postMessage({ cmd: 'done', chunk: chunk });
    })
    .fail((res) => {
      if (res.thrown) {
        console.error(res.thrown.stack);
        self.postMessage({ cmd: 'error', error: res.thrown.message });
      } else {
        console.error(res);
        self.postMessage({ cmd: 'error', error: 'network error' });
      }
    });
};

paulsmithkc avatar Nov 24 '20 16:11 paulsmithkc

ping @jimhigson

paulsmithkc avatar Aug 20 '21 11:08 paulsmithkc