Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
paulsmithkc opened this issue Nov 24, 2020 · 2 comments
Open

Comments

@paulsmithkc
Copy link

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
Copy link
Author

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
Copy link
Author

ping @jimhigson

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant