Coder Social home page Coder Social logo

okikio / sharedworker Goto Github PK

View Code? Open in Web Editor NEW
34.0 4.0 5.0 1.27 MB

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

Home Page: https://sharedworker.okikio.dev

License: MIT License

Dockerfile 2.55% JavaScript 50.50% TypeScript 29.81% CSS 16.93% Shell 0.21%
sharedworker worker polyfill ponyfill typescript webworker javascript

sharedworker's Introduction

@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.
  • 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

npm install @okikio/sharedworker
Others
yarn add @okikio/sharedworker

or

pnpm install @okikio/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">
    // 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.

Showcase

A couple sites that use @okikio/sharedworker:

API

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.

Browser Support

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.

Contributing

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.

Licence

See the LICENSE file for license rights and limitations (MIT).

sharedworker's People

Contributors

github-actions[bot] avatar okikio avatar ryan953 avatar semantic-release-bot avatar zerdos avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

sharedworker's Issues

Please clarify what this polyfill does?

I found this while trying to find a way to implement SharedWorker in Android for Chrome (the only remaining holdout for this mechanism). My question relates to what you wrote in your article:

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

If this doesn't actually bring across-tab communication to browsers that don't support SharedWorker, what exactly does it do/what is the point of it? (Surely there's a point, but I'm just not seeing it. So, I hope you can explain it to me!).

Single instance on multiple tabs

Is your feature request related to a problem? Please describe.
From the source, it seems that this library wraps a normal Worker in case there is no SharedWorker.
However, a SharedWorker is only instantiated once per origin/url. Does this library handle this as well?

Describe the solution you'd like
One tab could host the actual worker and the other tabs could be tricked into delegating everything to a BroadcastChannel, which the Worker also listens on.

Describe alternatives you've considered
None

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.