Coder Social home page Coder Social logo

originalsin / service-worker-command-bridge Goto Github PK

View Code? Open in Web Editor NEW

This project forked from gdnmobilelab/service-worker-command-bridge

0.0 3.0 0.0 44 KB

A utility library to facilitate communication between a Service Worker and web page client through Promises.

TypeScript 76.47% JavaScript 22.79% HTML 0.74%

service-worker-command-bridge's Introduction

service-worker-command-bridge

A utility library to facilitate communication between a Service Worker and web page client through Promises.

Why?

Service Worker communication is message-based, which makes it difficult to maintain a Promise-like flow when running a command inside the worker and parsing the result inside the web client.

How do I use it?

You should require the library in both your worker and client JS, like so:

In Service Worker

import { CommandListener } from "service-worker-command-bridge";

CommandListener.bind("test-command", (data) => {
    
    // You can return a value directly, or return a promise
    // and the bridge will return the resolved value to
    // the client.
    
    return data.startNumber * 10;
})

In client

import { runServiceWorkerCommand } from "service-worker-command-bridge";

runServiceWorkerCommand("test-command", {
    startNumber: 10
})
.then((endNumber) => {
    console.log(endNumber); // = 100
})

Upgrading from v1

Version 2.0 has a number of breaking changes you should be aware of:

  • It now uses ES6 modules, so your build system needs to be able to handle them. Rollup and the latest version of Webpack should work fine. Optimally your build system will also support tree-shaking, as both the client and worker sides are included in index.js.
  • On the worker side, you must now call CommandListener.listen() to start the bridge.
  • It has been rewritten in TypeScript, and emits definition files for you to use.

service-worker-command-bridge's People

Contributors

alastaircoote avatar

Watchers

Serg avatar James Cloos avatar  avatar

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.