");--dark-color-background:#36393f;--dark-color-secondary-background:#2f3136;--dark-color-text:#ffffff;--dark-color-text-aside:#e6e4e4;--dark-color-link:#00aff4;--dark-color-menu-divider:#eee;--dark-color-menu-divider-focus:#000;--dark-color-menu-label:#707070;--dark-color-panel:var(--dark-color-secondary-background);--dark-color-panel-divider:#818181;--dark-color-comment-tag:#dcddde;--dark-color-comment-tag-text:#2f3136;--dark-color-ts:#c97dff;--dark-color-ts-interface:#9cbe3c;--dark-color-ts-enum:#d6ab29;--dark-color-ts-class:#3695f3;--dark-color-ts-private:#e2e2e2;--dark-color-toolbar:#34373c;--dark-color-toolbar-text:#ffffff;--dark-icon-filter:invert(1);--dark-external-icon:url("data:image/svg+xml;utf8,")}@media (prefers-color-scheme:light){:root{--color-background:var(--light-color-background);--color-secondary-background:var(--light-color-secondary-background);--color-text:var(--light-color-text);--color-text-aside:var(--light-color-text-aside);--color-link:var(--light-color-link);--color-menu-divider:var(--light-color-menu-divider);--color-menu-divider-focus:var(--light-color-menu-divider-focus);--color-menu-label:var(--light-color-menu-label);--color-panel:var(--light-color-panel);--color-panel-divider:var(--light-color-panel-divider);--color-comment-tag:var(--light-color-comment-tag);--color-comment-tag-text:var(--light-color-comment-tag-text);--color-ts:var(--light-color-ts);--color-ts-interface:var(--light-color-ts-interface);--color-ts-enum:var(--light-color-ts-enum);--color-ts-class:var(--light-color-ts-class);--color-ts-private:var(--light-color-ts-private);--color-toolbar:var(--light-color-toolbar);--color-toolbar-text:var(--light-color-toolbar-text);--icon-filter:var(--light-icon-filter);--external-icon:var(--light-external-icon)}: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-secondary-background:var(--dark-color-secondary-background);--color-text:var(--dark-color-text);--color-text-aside:var(--dark-color-text-aside);--color-link:var(--dark-color-link);--color-menu-divider:var(--dark-color-menu-divider);--color-menu-divider-focus:var(--dark-color-menu-divider-focus);--color-menu-label:var(--dark-color-menu-label);--color-panel:var(--dark-color-panel);--color-panel-divider:var(--dark-color-panel-divider);--color-comment-tag:var(--dark-color-comment-tag);--color-comment-tag-text:var(--dark-color-comment-tag-text);--color-ts:var(--dark-color-ts);--color-ts-interface:var(--dark-color-ts-interface);--color-ts-enum:var(--dark-color-ts-enum);--color-ts-class:var(--dark-color-ts-class);--color-ts-private:var(--dark-color-ts-private);--color-toolbar:var(--dark-color-toolbar);--color-toolbar-text:var(--dark-color-toolbar-text);--icon-filter:var(--dark-icon-filter);--external-icon:var(--dark-external-icon)}: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)}}body{margin:0}h1,h2{line-height:1.2}h1{font-size:2em;margin:.67em 0}h2{font-size:1.5em;margin:.83em 0}pre{white-space:pre;white-space:pre-wrap;word-wrap:break-word}ul{margin:1em 0}.container{max-width:1200px;margin:0 auto;padding:0 40px}@media (max-width:640px){.container{padding:0 20px}}.container-main{padding-bottom:200px}.row{display:flex;position:relative;margin:0 -10px}.row:after{visibility:hidden;display:block;content:"";clear:both;height:0}.col-4,.col-8{box-sizing:border-box;float:left;padding:0 10px}.col-4{width:33.3333333333%}.col-8{width:66.6666666667%}.col-4>:first-child,.col-4>:first-child>:first-child,.col-4>:first-child>:first-child>:first-child,.col-8>:first-child,.col-8>:first-child>:first-child,.col-8>:first-child>:first-child>:first-child,.tsd-panel>:first-child,.tsd-panel>:first-child>:first-child{margin-top:0}.col-4>:last-child,.col-4>:last-child>:last-child,.col-4>:last-child>:last-child>:last-child,.col-8>:last-child{margin-bottom:0}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:14px}pre{padding:10px}pre code{padding:0;font-size:100%}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}@media (min-width:901px) and (max-width:1024px){html .col-content{width:72%}html .col-menu{width:28%}html .tsd-navigation{padding-left:10px}}.tsd-page-title{padding:70px 0 20px;margin:0 0 40px;background:var(--color-panel);box-shadow:0 0 5px rgba(0,0,0,.35)}.tsd-page-title h1{margin:0}#tsd-filter{position:relative;display:inline-block;height:40px;vertical-align:bottom}#tsd-filter .tsd-filter-group{display:inline-block;height:40px;vertical-align:bottom;white-space:nowrap}#tsd-filter input{display:none}@media (max-width:900px){html .col-content{float:none;width:100%}html .col-menu{position:fixed!important;overflow:auto;-webkit-overflow-scrolling:touch;z-index:1024;top:0!important;bottom:0!important;left:auto!important;right:0!important;width:100%;padding:20px 20px 0 0;max-width:450px;visibility:hidden;background-color:var(--color-panel);transform:translate(100%,0)}html .col-menu>:last-child{padding-bottom:20px}#tsd-filter .tsd-filter-group{display:block;position:absolute;top:40px;right:20px;height:auto;background-color:var(--color-panel);visibility:hidden;transform:translate(50%,0);box-shadow:0 0 4px rgba(0,0,0,.25)}#tsd-filter .tsd-select,#tsd-filter label{display:block;padding-right:20px}}.tsd-navigation{margin:0 0 0 40px}.tsd-navigation a{display:block;padding-top:2px;padding-bottom:2px;border-left:2px solid transparent;color:var(--color-text);text-decoration:none}.tsd-navigation ul{margin:0;padding:0;list-style:none}.tsd-navigation li{padding:0}.tsd-navigation.primary{padding-bottom:40px}.tsd-navigation.primary a{display:block;padding-top:6px;padding-bottom:6px}.tsd-navigation.primary ul li a{padding-left:5px}.tsd-navigation.primary>ul{border-bottom:1px solid var(--color-panel-divider)}.tsd-navigation.primary li{border-top:1px solid var(--color-panel-divider)}.tsd-navigation.primary li.current>a{font-weight:700}.tsd-navigation.secondary{max-height:calc(100vh - 1rem - 40px);overflow:auto;position:sticky;top:calc(.5rem + 40px)}.tsd-navigation.secondary ul li a{padding-left:25px}@media (min-width:901px){.menu-sticky-wrap{position:static}}.tsd-panel{margin:20px 0;padding:20px;background-color:var(--color-panel);box-shadow:0 0 4px rgba(0,0,0,.25)}#tsd-search .title{position:relative;z-index:2}#tsd-search .field{position:absolute;left:0;top:0;right:40px;height:40px}#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{padding:0 10px;background-color:var(--color-background)}#tsd-search .results li:nth-child(2n){background-color:var(--color-panel)}#tsd-search .results li.state{display:none}.tsd-page-toolbar{position:fixed;z-index:1;top:0;left:0;width:100%;height:40px;color:var(--color-toolbar-text);background:var(--color-toolbar);border-bottom:1px solid var(--color-panel-divider)}.tsd-page-toolbar a{color:var(--color-toolbar-text);text-decoration:none}.tsd-page-toolbar a.title{font-weight:700}.tsd-page-toolbar .table-wrap{display:table;width:100%;height:40px}.tsd-page-toolbar .table-cell{display:table-cell;position:relative;white-space:nowrap;line-height:40px}.tsd-page-toolbar .table-cell:first-child{width:100%}.tsd-select .tsd-select-label:before,.tsd-select .tsd-select-list li:before,.tsd-widget:before{content:"";display:inline-block;width:40px;height:40px;margin:0 -8px 0 0;background-image:url(assets/widgets.png);background-repeat:no-repeat;text-indent:-1024px;vertical-align:bottom;filter:var(--icon-filter)}@media (-webkit-min-device-pixel-ratio:1.5),(min-resolution:144dpi){.tsd-select .tsd-select-label:before,.tsd-select .tsd-select-list li:before,.tsd-widget:before{background-image:url(assets/widgets@2x.png);background-size:320px 40px}}.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.search:before{background-position:0 0}.tsd-widget.menu:before{background-position:-40px 0}.tsd-widget.options:before{background-position:-80px 0}.tsd-widget.menu,.tsd-widget.options{display:none}input[type=checkbox]+.tsd-widget:before{background-position:-120px 0}input[type=checkbox]:checked+.tsd-widget:before{background-position:-160px 0}.tsd-select{position:relative;display:inline-block;height:40px;vertical-align:bottom}.tsd-select .tsd-select-label{opacity:.6}.tsd-select .tsd-select-label:before{background-position:-240px 0}.tsd-select .tsd-select-list{position:absolute;visibility:hidden;top:40px;left:0;margin:0;padding:0;opacity:0;list-style:none;box-shadow:0 0 4px rgba(0,0,0,.25)}.tsd-select .tsd-select-list li{padding:0 20px 0 0;background-color:var(--color-background)}.tsd-select .tsd-select-list li:before{background-position:40px 0}.tsd-select .tsd-select-list li:nth-child(2n){background-color:var(--color-panel)}.tsd-select .tsd-select-list li.selected:before{background-position:-200px 0}img{max-width:100%}:root{--light-hl-0:#24292F;--dark-hl-0:#C9D1D9;--light-hl-1:#CF222E;--dark-hl-1:#FF7B72;--light-hl-2:#0A3069;--dark-hl-2:#A5D6FF;--light-hl-3:#6E7781;--dark-hl-3:#8B949E;--light-hl-4:#116329;--dark-hl-4:#7EE787;--light-hl-5:#0550AE;--dark-hl-5:#79C0FF;--light-hl-6:#953800;--dark-hl-6:#FFA657;--light-hl-7:#8250DF;--dark-hl-7:#D2A8FF;--light-code-background:#F5F5F5;--dark-code-background:#0d1117}.hl-0{color:var(--hl-0)}.hl-1{color:var(--hl-1)}.hl-2{color:var(--hl-2)}.hl-3{color:var(--hl-3)}code,pre{background:var(--code-background)}:root{--light-color-background:#f8f9fa;--light-color-panel-divider:#dadce0;--light-link-color:#1155cc;--dark-color-panel-divider:#47474d;--dark-color-menu-divider-focus:#d73a49;--dark-color-background:#1b1c1f;--dark-color-secondary-background:#2d2f34}pre{padding:14px}code,pre{border-radius:6px;border:1px solid}pre code{border:none}body,html{font-family:Lexend,Manrope,"Century Gothic",sans-serif;font-weight:400}body:not(.light){color-scheme:dark}blockquote{padding:.2em .8em .2em 1em;border-left:5px solid gray;margin-bottom:2.5rem;background-color:var(--color-background);border-radius:.45em}.tsd-page-title{box-shadow:none}@media (prefers-color-scheme:dark){html{color-scheme:dark}.tsd-panel{box-shadow:none}}@media (prefers-color-scheme:light){.tsd-panel{box-shadow:0 1px 3px 1px rgba(60,64,67,.15)}}.tsd-page-toolbar{z-index:1025}.tsd-page-title{border-bottom:1px solid var(--color-panel-divider)}.menu-highlight{padding-top:50px}.menu-highlight .tsd-navigation.primary{padding-bottom:20px}@media (min-width:901px) and (max-width:1024px){html.default .col-menu{width:33.3333333333%}html.default .tsd-navigation{padding-left:0}}@media (max-width:900px){.tsd-widget.menu,.tsd-widget.options{display:inline-block}.tsd-select .tsd-select-list{top:0;left:auto;right:100%;margin-right:-5px}.tsd-select .tsd-select-label:before{background-position:-280px 0}html .col-menu.menu-sticky-wrap{grid-template-rows:auto 1fr;transform:translate(0,0)}}.tsd-navigation.primary ul{position:sticky;top:60px}.tsd-navigation.primary li:not(.tsd-kind-module){text-decoration:underline solid;text-underline-offset:0.5em}.tsd-navigation.secondary ul li a{border-radius:.25em}.tsd-panel{padding:45px;word-wrap:break-word}@media (max-width:700px){.tsd-panel{padding:35px}}@media (max-width:500px){.tsd-panel{padding:25px}}.tsd-panel a{white-space:normal;word-break:break-word;text-decoration:underline solid;text-underline-offset:0.2em;line-height:1.85em;color:var(--light-link-color)}.tsd-panel img[src="https://bundlejs.com/badge-light.svg"]{overflow:hidden;border-radius:20em;border:1px solid}body:not(.light) .tsd-panel a{color:var(--color-link)}.tsd-panel a[target] .external-icon{display:inline;stroke:currentColor;padding-left:.2rem;padding-right:.1rem;opacity:.6}.tsd-widget.no-caption{color:transparent}.tsd-widget.no-caption:before{background-image:none;filter:none}.tsd-widget.search:after{content:"\f690"}.tsd-widget.options:after{content:"\f407"}.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 sharedworker
yarn add sharedworker
or
pnpm install 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" type="module"></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, 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.
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