sharedworker
A small spec. compliant polyfill for SharedWorkers, it acts as a drop in re...
README
@okikio/sharedworker
Ponyfills are seperate modules that are included to replicate the functionality of the original API, but are not required to be used.
Polyfills 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.
Check out the blog post, created for it's launch.
Installation
- ``` sh
- npm install @okikio/sharedworker
- ```
Others
- ``` sh
- yarn add @okikio/sharedworker
- ```
- ``` sh
- pnpm install @okikio/sharedworker
- ```
Usage
- ```ts
- import { SharedWorkerPolyfill as SharedWorker } from "@okikio/sharedworker";
- // or
- import SharedWorker from "@okikio/sharedworker";
- ```
- ``` html
- <script src="https://unpkg.com/@okikio/sharedworker" type="module"></script>
- <script type="module">
- // You can then use it like this
- const { SharedWorkerPolyfill: SharedWorker } = window.sharedworker;
- </script>
- ```
- ```ts
- import SharedWorker from "https://cdn.skypack.dev/@okikio/sharedworker";
- // or
- import SharedWorker from "https://cdn.jsdelivr.net/npm/@okikio/sharedworker";
- // or any number of other CDN's
- ```
- ```ts
- /*
- * 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 can be placed here and should just work, with no extra setup required
- /**
- * All variables and values inside the `start(...)` function are isolated to each page, and will be allocated seperately per page.
- */
- port.onmessage = ({ data }) => {
- console.log("Cool")
- };
- };
- self.onconnect = e => {
- let [port] = e.ports;
- start(port);
- };
- // This is the fallback, just in case the browser doesn't support SharedWorkers natively
- if ("SharedWorkerGlobalScope" in self)
- start(self);
- ```
_Note: make sure to read the comments in the above code carefully to avoid unexpected bugs._
Showcase
API
_Note: the normal functionality of the methods and properties that are normally available on SharedWorker.prototype will still be kept intact, in @okikio/sharedworker._
Browser Support
| Chrome | Edge | Firefox | Safari | IE |
|---|---|---|---|---|
| ------ | ---- | ------- | ------ | --- |
| 4+ | 12+ | 4+ | 4+ | 10+ |
_Note: some features of Workers appeared at later versions of the spec., so, I suggest looking into the feature support table for Workers and SharedWorkers._
Contributing
- ``` sh
- npm install
- ```
- ``` sh
- npm test
- ```
- ``` sh
- npm run build
- ```
- ``` sh
- npm run typedoc && npm run preview
- ```
_Note: this project uses Conventional Commits standard for commits, so, please format your commits using the rules it sets out._
探客时代
