");--light-color-scheme:light;--dark-color-background:#2b2e33;--dark-color-background-secondary:#1e2024;--dark-color-background-warning:#bebe00;--dark-color-warning-text:#222;--dark-color-icon-background:var(--dark-color-background-secondary);--dark-color-accent:#9096a2;--dark-color-active-menu-item:#5d5d6a;--dark-color-text:#f5f5f5;--dark-color-text-aside:#dddddd;--dark-color-link:#00aff4;--dark-color-ts-keyword:#3399ff;--dark-color-ts-project:#e358ff;--dark-color-ts-module:var(--dark-color-ts-project);--dark-color-ts-namespace:var(--dark-color-ts-project);--dark-color-ts-enum:#f4d93e;--dark-color-ts-enum-member:var(--dark-color-ts-enum);--dark-color-ts-variable:#798dff;--dark-color-ts-function:#a280ff;--dark-color-ts-class:#8ac4ff;--dark-color-ts-interface:#6cff87;--dark-color-ts-constructor:var(--dark-color-ts-class);--dark-color-ts-property:var(--dark-color-ts-variable);--dark-color-ts-method:var(--dark-color-ts-function);--dark-color-ts-call-signature:var(--dark-color-ts-method);--dark-color-ts-index-signature:var(--dark-color-ts-property);--dark-color-ts-constructor-signature:var(--dark-color-ts-constructor);--dark-color-ts-parameter:var(--dark-color-ts-variable);--dark-color-ts-type-parameter:var(--dark-color-ts-type-alias);--dark-color-ts-accessor:var(--dark-color-ts-property);--dark-color-ts-get-signature:var(--dark-color-ts-accessor);--dark-color-ts-set-signature:var(--dark-color-ts-accessor);--dark-color-ts-type-alias:#ff6492;--dark-external-icon:url("data:image/svg+xml;utf8,");--dark-color-scheme:dark}@media (prefers-color-scheme:light){:root{--color-background:var(--light-color-background);--color-background-secondary:var(--light-color-background-secondary);--color-background-warning:var(--light-color-background-warning);--color-warning-text:var(--light-color-warning-text);--color-icon-background:var(--light-color-icon-background);--color-accent:var(--light-color-accent);--color-active-menu-item:var(--light-color-active-menu-item);--color-text:var(--light-color-text);--color-text-aside:var(--light-color-text-aside);--color-link:var(--light-color-link);--color-ts-keyword:var(--light-color-ts-keyword);--color-ts-module:var(--light-color-ts-module);--color-ts-namespace:var(--light-color-ts-namespace);--color-ts-enum:var(--light-color-ts-enum);--color-ts-enum-member:var(--light-color-ts-enum-member);--color-ts-variable:var(--light-color-ts-variable);--color-ts-function:var(--light-color-ts-function);--color-ts-class:var(--light-color-ts-class);--color-ts-interface:var(--light-color-ts-interface);--color-ts-constructor:var(--light-color-ts-constructor);--color-ts-property:var(--light-color-ts-property);--color-ts-method:var(--light-color-ts-method);--color-ts-call-signature:var(--light-color-ts-call-signature);--color-ts-index-signature:var(--light-color-ts-index-signature);--color-ts-constructor-signature:var(--light-color-ts-constructor-signature);--color-ts-parameter:var(--light-color-ts-parameter);--color-ts-type-parameter:var(--light-color-ts-type-parameter);--color-ts-accessor:var(--light-color-ts-accessor);--color-ts-get-signature:var(--light-color-ts-get-signature);--color-ts-set-signature:var(--light-color-ts-set-signature);--color-ts-type-alias:var(--light-color-ts-type-alias);--external-icon:var(--light-external-icon);--color-scheme:var(--light-color-scheme)}:root{--hl-0:var(--light-hl-0);--hl-1:var(--light-hl-1);--hl-2:var(--light-hl-2);--hl-3:var(--light-hl-3);--hl-4:var(--light-hl-4);--hl-5:var(--light-hl-5);--hl-6:var(--light-hl-6);--hl-7:var(--light-hl-7);--code-background:var(--light-code-background)}}@media (prefers-color-scheme:dark){:root{--color-background:var(--dark-color-background);--color-background-secondary:var(--dark-color-background-secondary);--color-background-warning:var(--dark-color-background-warning);--color-warning-text:var(--dark-color-warning-text);--color-icon-background:var(--dark-color-icon-background);--color-accent:var(--dark-color-accent);--color-active-menu-item:var(--dark-color-active-menu-item);--color-text:var(--dark-color-text);--color-text-aside:var(--dark-color-text-aside);--color-link:var(--dark-color-link);--color-ts-keyword:var(--dark-color-ts-keyword);--color-ts-module:var(--dark-color-ts-module);--color-ts-namespace:var(--dark-color-ts-namespace);--color-ts-enum:var(--dark-color-ts-enum);--color-ts-enum-member:var(--dark-color-ts-enum-member);--color-ts-variable:var(--dark-color-ts-variable);--color-ts-function:var(--dark-color-ts-function);--color-ts-class:var(--dark-color-ts-class);--color-ts-interface:var(--dark-color-ts-interface);--color-ts-constructor:var(--dark-color-ts-constructor);--color-ts-property:var(--dark-color-ts-property);--color-ts-method:var(--dark-color-ts-method);--color-ts-call-signature:var(--dark-color-ts-call-signature);--color-ts-index-signature:var(--dark-color-ts-index-signature);--color-ts-constructor-signature:var(--dark-color-ts-constructor-signature);--color-ts-parameter:var(--dark-color-ts-parameter);--color-ts-type-parameter:var(--dark-color-ts-type-parameter);--color-ts-accessor:var(--dark-color-ts-accessor);--color-ts-get-signature:var(--dark-color-ts-get-signature);--color-ts-set-signature:var(--dark-color-ts-set-signature);--color-ts-type-alias:var(--dark-color-ts-type-alias);--external-icon:var(--dark-external-icon);--color-scheme:var(--dark-color-scheme)}}html{color-scheme:var(--color-scheme)}body{margin:0}h1,h2,h3,h4{line-height:1.2}h1>a,h2>a{text-decoration:none;color:var(--color-text)}h1{font-size:1.875rem;margin:.67rem 0}h2{font-size:1.5rem;margin:.83rem 0}h3{font-size:1.25rem;margin:1rem 0}h4{font-size:1.05rem;margin:1.33rem 0}.uppercase{text-transform:uppercase}ul{margin:1em 0}.container{max-width:1700px;padding:0 2rem}.container-main{margin:0 auto;min-height:calc(100vh - 41px - 56px - 4rem)}body{background:var(--color-background);font-family:"Segoe UI",sans-serif;font-size:16px;color:var(--color-text)}a{color:var(--color-link);text-decoration:none}code,pre{font-family:Menlo,Monaco,Consolas,"Courier New",monospace;padding:.2em;margin:0;font-size:.875rem;border-radius:.8em}pre{position:relative;white-space:pre;white-space:pre-wrap;word-wrap:break-word;padding:10px;border:1px solid var(--color-accent)}pre code{padding:0;font-size:100%}pre>button{position:absolute;top:10px;right:10px;opacity:0;box-sizing:border-box}blockquote{margin:1em 0;padding-left:1em;border-left:4px solid gray}.tsd-typography{line-height:1.333em}.tsd-typography ul{list-style:square;padding:0 0 0 20px;margin:0}.tsd-typography p,.tsd-typography ul{margin:1em 0}.tsd-filter-visibility h4{font-size:1rem;padding-top:.75rem;padding-bottom:.5rem;margin:0}.tsd-filter-item:not(:last-child){margin-bottom:.5rem}.tsd-filter-input{display:flex;width:fit-content;width:-moz-fit-content;align-items:center}.tsd-filter-input input[type=checkbox]{position:absolute;width:1.5em;height:1.5em;opacity:0}.tsd-filter-input svg{width:1.5em;height:1.5em;margin-right:.5em;border-radius:.33em;opacity:.99}.tsd-checkbox-background{fill:var(--color-accent)}input[type=checkbox]:checked~svg .tsd-checkbox-checkmark{stroke:var(--color-text)}.tsd-theme-toggle{padding-top:.75rem}.tsd-theme-toggle>h4{display:inline;vertical-align:middle;margin-right:.75rem}.tsd-anchor{position:relative;top:-100px}.tsd-navigation.settings{margin:1rem 0}.tsd-navigation .tsd-accordion-summary,.tsd-navigation>a{width:calc(100% - .5rem)}.tsd-navigation a,.tsd-page-navigation a{display:inline-flex;align-items:center;padding:.25rem;color:var(--color-text);text-decoration:none;box-sizing:border-box}.tsd-navigation a.current{background:var(--color-active-menu-item)}.tsd-navigation ul,.tsd-page-navigation ul{margin-top:0;margin-bottom:0;padding:0;list-style:none}.tsd-navigation li,.tsd-page-navigation li{padding:0;max-width:100%}.tsd-small-nested-navigation{margin-left:1.5rem}.tsd-page-navigation ul{padding-left:1.75rem}.tsd-accordion-summary{list-style-type:none;outline:0}.tsd-accordion-summary::-webkit-details-marker{display:none}.tsd-accordion-summary>*{margin-top:0;margin-bottom:0;padding-top:0;padding-bottom:0}.tsd-kind-icon{margin-right:.5rem;width:1.25rem;height:1.25rem;min-width:1.25rem;min-height:1.25rem}.tsd-panel{margin-bottom:2.5rem}.tsd-panel>h1,.tsd-panel>h2{margin:1.5rem -1.5rem .75rem;padding:0 1.5rem .75rem}#tsd-search .title{position:relative;z-index:2}#tsd-search .field{position:absolute;left:0;top:0;right:2.5rem;height:100%}#tsd-search .field input{box-sizing:border-box;position:relative;top:-50px;z-index:1;width:100%;padding:0 10px;opacity:0;outline:0;border:0;background:0 0;color:var(--color-text)}#tsd-search .field label{position:absolute;overflow:hidden;right:-40px}#tsd-search .results{position:absolute;visibility:hidden;top:40px;width:100%;margin:0;padding:0;list-style:none;box-shadow:0 0 4px rgba(0,0,0,.25)}#tsd-search .results li{background-color:var(--color-background);line-height:initial;padding:4px}#tsd-search .results li:nth-child(2n){background-color:var(--color-background-secondary)}#tsd-search .results li.state{display:none}#tsd-toolbar-links{position:absolute;top:0;right:2rem;height:100%;display:flex;align-items:center;justify-content:flex-end}.tsd-page-toolbar{position:sticky;z-index:1;top:0;left:0;width:100%;color:var(--color-text);background:var(--color-background-secondary);border-bottom:1px var(--color-accent) solid}.tsd-page-toolbar a{color:var(--color-text);text-decoration:none}.tsd-page-toolbar a.title{font-weight:700}.tsd-page-toolbar .tsd-toolbar-contents{display:flex;justify-content:space-between;height:2.5rem;margin:0 auto}.tsd-page-toolbar .table-cell{position:relative;white-space:nowrap;line-height:40px}.tsd-page-toolbar .table-cell:first-child{width:100%}.tsd-page-toolbar .tsd-toolbar-icon{box-sizing:border-box;line-height:0;padding:12px 0}.tsd-widget{display:inline-block;overflow:hidden;opacity:.8;height:40px;vertical-align:bottom}.tsd-widget.no-caption{width:40px}.tsd-widget.no-caption:before{margin:0}.tsd-widget.menu{display:none}img{max-width:100%}.tsd-kind-icon~span{color:var(--color-text)}*{scrollbar-width:thin;scrollbar-color:var(--color-accent) var(--color-icon-background)}::-webkit-scrollbar{width:.75rem}::-webkit-scrollbar-track{background:var(--color-icon-background)}::-webkit-scrollbar-thumb{background-color:var(--color-accent);border-radius:999rem;border:.25rem solid var(--color-icon-background)}@media (max-width:769px){.tsd-widget.menu{display:inline-block}.container-main{display:flex}html .col-content{float:none;max-width:100%;width:100%}html .col-sidebar{position:fixed!important;overflow-y:auto;-webkit-overflow-scrolling:touch;z-index:1024;top:0!important;bottom:0!important;left:auto!important;right:0!important;padding:1.5rem 1.5rem 0 0;width:75vw;visibility:hidden;background-color:var(--color-background);transform:translate(100%,0)}html .col-sidebar>:last-child{padding-bottom:20px}}@media (min-width:770px){.container-main{display:grid;grid-template-columns:minmax(0,1fr) minmax(0,2fr);grid-template-areas:"sidebar content";margin:2rem auto}.col-sidebar{grid-area:sidebar}.col-content{grid-area:content;padding:0 1rem}}@media (min-width:770px) and (max-width:1399px){.col-sidebar{max-height:calc(100vh - 2rem - 42px);overflow:auto;position:sticky;top:42px;padding-top:1rem}.site-menu{margin-top:1rem}}@media (min-width:1200px){.container-main{grid-template-columns:minmax(0,1fr) minmax(0,2.5fr) minmax(0,20rem);grid-template-areas:"sidebar content toc"}.col-sidebar{display:contents}.page-menu{grid-area:toc;padding-left:1rem}.site-menu{grid-area:sidebar}.site-menu{margin-top:1rem 0}.page-menu,.site-menu{max-height:calc(100vh - 2rem - 42px);overflow:auto;position:sticky;top:42px}}:root{--light-hl-0:#6F42C1;--dark-hl-0:#B392F0;--light-hl-1:#24292E;--dark-hl-1:#E1E4E8;--light-hl-2:#032F62;--dark-hl-2:#9ECBFF;--light-hl-3:#D73A49;--dark-hl-3:#F97583;--light-hl-4:#6A737D;--dark-hl-4:#6A737D;--light-hl-5:#22863A;--dark-hl-5:#85E89D;--light-hl-6:#E36209;--dark-hl-6:#FFAB70;--light-hl-7:#005CC5;--dark-hl-7:#79B8FF;--light-code-background:#fff;--dark-code-background:#24292e}.hl-0{color:var(--hl-0)}.hl-1{color:var(--hl-1)}.hl-2{color:var(--hl-2)}.hl-3{color:var(--hl-3)}.hl-4{color:var(--hl-4)}.hl-5{color:var(--hl-5)}code,pre{background:var(--code-background)}:root{--light-color-background-secondary:#fff;--dark-color-background:#1b1c1f;--dark-color-background-secondary:#36393f;--light-color-panel-divider:#dadce0;--dark-color-panel-divider:#47474d;--color-panel-divider:var(--light-color-panel-divider);--dark-color-text-aside:#8486b4}@media (prefers-color-scheme:light){:root{--light-color-background-secondary:#fff;--light-color-panel-divider:#dadce0;--color-panel-divider:var(--light-color-panel-divider)}}@media (prefers-color-scheme:dark){:root{--hl-0:var(--dark-hl-0);--hl-1:var(--dark-hl-1);--hl-2:var(--dark-hl-2);--hl-3:var(--dark-hl-3);--hl-4:var(--dark-hl-4);--hl-5:var(--dark-hl-5);--hl-6:var(--dark-hl-6);--hl-7:var(--dark-hl-7);--code-background:var(--dark-code-background)}:root{--dark-color-background:#1b1c1f;--dark-color-background-secondary:#36393f;--dark-color-panel-divider:#47474d;--color-panel-divider:var(--dark-color-panel-divider);--dark-color-text-aside:#8486b4}}body,html{font-family:Lexend,Manrope,"Century Gothic",sans-serif;font-weight:400}h1{font-weight:300;padding-bottom:.9rem}h2{font-weight:400;color:var(--color-ts)}h3{font-weight:500}h4{font-weight:500}pre{padding:14px}code,pre{border-radius:6px;border:1px solid}pre code{border:none}blockquote{padding:.2em .8em .2em 1em;border-left:5px solid gray;margin-bottom:2.5rem;background-color:var(--color-background-secondary);border-radius:.45em}img[src^="https://bundlejs.com/"]{overflow:hidden;border-radius:20em;border:1px solid}.container{max-width:1600px;margin-inline:auto}.container .col-content{max-width:800px;margin-inline:auto}header.tsd-page-toolbar{border-bottom:1px solid var(--color-panel-divider)}.tsd-typography p,.tsd-typography ul{line-height:1.85em;font-weight:300}.tsd-navigation{padding-inline-end:0.25rem}.tsd-accordion-details{padding-inline-start:1.35rem}.tsd-accordion-details>ul>li>ul{padding-left:0}.container-main{--mask-image:linear-gradient(0deg, transparent 0%, white 2%, white 50%, white 98%, transparent 100%)}@media (min-width:769px){.container-main{grid-template-columns:minmax(0,15rem) minmax(0,2.5fr)}.col-sidebar{border-right:1px solid var(--color-panel-divider);mask-image:var(--mask-image);-webkit-mask-image:var(--mask-image);padding-block:1rem}}@media (min-width:1200px){.container-main{grid-template-columns:minmax(0,18rem) minmax(0,2.5fr) minmax(0,15.5rem)}.page-menu{border-left:1px solid var(--color-panel-divider)}.site-menu{border-right:1px solid var(--color-panel-divider);mask-image:var(--mask-image);-webkit-mask-image:var(--mask-image);padding-block:1rem}}.tsd-accordion-details ul{padding-left:1rem}.tsd-accordion-details>ul{padding-left:0}.tsd-navigation a.current{border-radius:.45rem;padding-block:0.3rem;padding-inline:0.3rem}.tsd-widget.search:after{content:"\f690"}.tsd-widget.menu{position:relative;height:40px;vertical-align:bottom}.tsd-widget.menu:after{content:"\f561"}
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.
- 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.
npm install @okikio/sharedworker
yarn add @okikio/sharedworker
or
pnpm install @okikio/sharedworker
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"></script>
<script type="module">
// You can then use it like this
const { SharedWorkerPolyfill: SharedWorker } = window.sharedworker;
</script>
You can also use it via a CDN, e.g.
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
@okikio/sharedworker
supports the same API surfaces as SharedWorker
and Worker
, except it adds some none spec. compliant properties and methods to the SharedWorkerPolyfill
class, that enables devs to use SharedWorker
's on browsers that don't support it.
In order to support browsers that don't natively support SharedWorker
's, the actual worker file needs to be tweaked slightly,
/*
* 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 for WebWorkers, 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.
A couple sites that use @okikio/sharedworker
:
The API of @okikio/sharedworker
closely match the web SharedWorker
API, except that all the major methods and properties of SharedWorker.prototype.port
are available directly on SharedWorker.prototype
including addEventListener
and removeEventListener
.
Note: the normal functionality of the methods and properties that are normally available on
SharedWorker.prototype
will still be kept intact, in@okikio/sharedworker
.
In addition, the terminate()
method was added to @okikio/sharedworker
, this allows both the close()
method (this is from SharedWorker.prototype.port
) and the terminate()
method to manually close workers.
Check out the API site for detailed API documentation.
Chrome | Edge | Firefox | Safari | IE |
---|---|---|---|---|
4+ | 12+ | 4+ | 4+ | 10+ |
Native support for SharedWorker
is not supported at all on Safari and IE, as well as all mobile browsers (excluding Firefox For Android).
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.
I encourage you to use pnpm to contribute to this repo, but you can also use yarn or npm if you prefer.
Install all necessary packages
npm install
Then run tests (WIP)
npm test
Build project
npm run build
Preview API Docs
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.
See the LICENSE file for license rights and limitations (MIT).
Generated using TypeDoc