@okikio/sharedworker, SharedWorkers on all browsers

For bundle.js.org, and astro.build/play, I found that I needed a way to use SharedWorkers reliably on all browsers, so, I decided to make a miniature script that would act as a wrapper around the SharedWorker class, by default it would try to create a …


This content originally appeared on DEV Community and was authored by Okiki

For bundle.js.org, and astro.build/play, I found that I needed a way to use SharedWorkers reliably on all browsers, so, I decided to make a miniature script that would act as a wrapper around the SharedWorker class, by default it would try to create a SharedWorker but otherwise would switch to normal web workers this made SharedWorkers a type of progressive enhancement.

When I realized that a polyfill/ponyfill doesn't exist for SharedWorkers I realized I needed to make one, and to ensure reliable that the polyfill was thoroughly vetted and tested for cross browser compatibility, so, I made @okikio/sharedworker.

GitHub logo okikio / sharedworker

A small spec. compliant polyfill for SharedWorkers, it acts as a drop in replacement for normal Workers.

@okikio /sharedworker

Open Bundle

NPM | Github | Docs | Licence

A small mostly spec. compliant polyfill/ponyfill for SharedWorkers, it acts as a drop in replacement for normal Workers, and supports a similar API surface that matches normal Workers.

Ponyfills are seperate modules that are included to replicate the functionality of the original API, but are not required to be used, while polyfills are update the original API on the global scope if it isn't supported in that specific environment or it's feature set is lacking compared to modern variations.

Installation

npm install sharedworker
Others
yarn add sharedworker

or

pnpm install sharedworker

Usage

import { SharedWorkerPolyfill as SharedWorker } from "@okikio
/sharedworker";
// or 
import SharedWorker from "@okikio
/sharedworker";

You can also use it directly through a script tag:

<script src="https://unpkg.com/@okikio
/sharedworker" type="module"></script>
<script type="module">

Usage

@okikio/sharedworker is a small mostly spec. compliant polyfill/ponyfill for SharedWorkers, it acts as a drop in replacement for normal Workers, and supports a similar API surface that matches normal Workers.

You use it like this,

shared-worker.js

/* 
 * All variables and values outside the `start(...)` function are shared between all pages, this behavior can cause unexpected bugs if you're not careful
 */
const start = (port) => {
    // All your normal Worker and SharedWorker stuff should just work
    // With no more setup 

    /** 
     * All variables and values inside the `start(...)` function are isolated to each page, and will be allocated seperately per page. 
     */
    port.onmessage = ({ data }) => {
        if (data == "Hey")
            port.postMessage("Hello, from the SharedWorker."); 
    };
};

self.onconnect = e => {
    let [port] = e.ports;
    start(port);
};

// This is the fallback, just in case the browser doesn't support SharedWorkers
if ("SharedWorkerGlobalScope" in self) 
    start(self);

main.js

import SharedWorker from "@okikio/sharedworker";

const sharedworker = new SharedWorker(new URL("shared-worker.js", import.meta.url));
sharedworker.onmessage = ({ data }) => {
    console.log(data); //= Hello, from SharedWorker
};

sharedworker.postMessage("Hey");

In the cases of bundle.js.org and astro.build/play, @okikio/sharedworker was used for esbuild as well as the monaco-editors editor and typescript workers.

Limitation

The major limitation with @okikio/sharedworker is that on browsers that don't support SharedWorker, you can't use @okikio/sharedworker as a cross tab, communication tool. But for everything else it's feature parity and spec. compliance should be great.

Conclusion

So, will you use it? Tell me below, or say Hi on twitter.

Image from Tengyart on Unsplash.


This content originally appeared on DEV Community and was authored by Okiki


Print Share Comment Cite Upload Translate Updates
APA

Okiki | Sciencx (2021-10-15T22:49:14+00:00) @okikio/sharedworker, SharedWorkers on all browsers. Retrieved from https://www.scien.cx/2021/10/15/okikio-sharedworker-sharedworkers-on-all-browsers/

MLA
" » @okikio/sharedworker, SharedWorkers on all browsers." Okiki | Sciencx - Friday October 15, 2021, https://www.scien.cx/2021/10/15/okikio-sharedworker-sharedworkers-on-all-browsers/
HARVARD
Okiki | Sciencx Friday October 15, 2021 » @okikio/sharedworker, SharedWorkers on all browsers., viewed ,<https://www.scien.cx/2021/10/15/okikio-sharedworker-sharedworkers-on-all-browsers/>
VANCOUVER
Okiki | Sciencx - » @okikio/sharedworker, SharedWorkers on all browsers. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/10/15/okikio-sharedworker-sharedworkers-on-all-browsers/
CHICAGO
" » @okikio/sharedworker, SharedWorkers on all browsers." Okiki | Sciencx - Accessed . https://www.scien.cx/2021/10/15/okikio-sharedworker-sharedworkers-on-all-browsers/
IEEE
" » @okikio/sharedworker, SharedWorkers on all browsers." Okiki | Sciencx [Online]. Available: https://www.scien.cx/2021/10/15/okikio-sharedworker-sharedworkers-on-all-browsers/. [Accessed: ]
rf:citation
» @okikio/sharedworker, SharedWorkers on all browsers | Okiki | Sciencx | https://www.scien.cx/2021/10/15/okikio-sharedworker-sharedworkers-on-all-browsers/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.