A reasonably popular npm package is https://github.com/lamansky/is-instance-of (1k downloads last week), but it's severely outdated. I think this package would be a great place for such a function? Or maybe there already is one that does what I'm looking for and I couldn't find it?
Example use case:
// Covers ServiceWorkerGlobalScope, DedicatedWorkerGlobalScope,
// plain WorkerGlobalScope from web-worker polyfill, SharedWorkerGlobalScope, etc.
if (isInstanceOf(globalThis, "WorkerGlobalScope")) {
onmessage = (e) => console.log(e.data);
doThingInWorker(self);
}
doNormalThing();
vs the old way
for (let p = globalThis; p; p = Object.getPrototypeOf(p)) {
if (Object.prototype.toString.call(p).slice(8, -1) === "WorkerGlobalScope") {
onmessage = (e) => console.log(e.data);
doThingInWorker(self);
break;
}
}
doNormalThing();
A reasonably popular npm package is https://github.com/lamansky/is-instance-of (1k downloads last week), but it's severely outdated. I think this package would be a great place for such a function? Or maybe there already is one that does what I'm looking for and I couldn't find it?
Example use case:
vs the old way