Skip to content

Instantly share code, notes, and snippets.

@mohayonao
Last active August 29, 2015 14:23
Show Gist options
  • Select an option

  • Save mohayonao/44cdb7ec0505a36d3219 to your computer and use it in GitHub Desktop.

Select an option

Save mohayonao/44cdb7ec0505a36d3219 to your computer and use it in GitHub Desktop.
const WORKER_ENABLED = !!(global === global.window && global.URL && global.Worker);
const IN_WORKER_CONTEXT = !!(global === global.self && global.location);
let pathname = WORKER_ENABLED && (() => {
let scripts = global.document.getElementsByTagName("script");
let script = scripts[scripts.length - 1].src;
return new global.URL(script).pathname;
})();
export default class OUroborosWorker {
constructor(self = {}) {
if (WORKER_ENABLED) {
return new global.Worker(pathname);
}
if (IN_WORKER_CONTEXT) {
return {};
}
this.self = self;
this.self.postMessage = (data) => {
setTimeout(() => {
if (typeof this.onmessage === "function") {
this.onmessage({ data });
}
}, 0);
};
}
postMessage(data) {
setTimeout(() => {
if (typeof this.self.onmessage === "function") {
this.self.onmessage({ data });
}
}, 0);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment