Code Monkey home page Code Monkey logo

async-worker-state's Introduction

Async Worker

Open Source Love License

An easy way to send async messages to a web-worker.

Pros

This converts the Service-Worker / Worker's method postMessage to an async blocking call. The best part is there is no overhead on CPU or memory usage.

Installation

Install in your nodejs package and start using right away. you could provide a custom worker or follow the worker boilerplate used by us.

let state = {};

addEventListener('message', ({ data }) => {
  const {
    topic,
    message: {
      key,
      value
    }
  } = data;

  switch (topic) {
    case 'set':
      state[key] = value;
      postMessage({
        topic: 'key-set',
        message: key
      });
      break;
    case 'get':
      postMessage({
        topic: 'get-key',
        message: {
          key,
          value: state[key]
        }
      });
    default:
      break;
  }
});

A sample usage of this Async Worker as follows:

const {
  AsyncWorkerStateMessage:
} = require('async-worker');

// You could pass a custom worker path in here.
// const awInstance = aw(<YOUR_WORKER's_PATH>);
const awInstance = new aw();

// this is an anonymous function or IIFE that will set a key and retreive it to the worker's state using a key-value pair.
(async () => {
    const stored = await awInstance.setState('test',456);
    const retreived = await awInstance.getState('test');
    console.log(retreived); // This will log 456
})();

The following methods are available from the class.

// get a stored key
const getProp(key: string): any;

// set a value for a prop
const setProp(key: string, value: any): any;

// get the entire stored state
const getState(): Object;

Support

We have not released an ES6 module yet. We could use your support to release packages for ES6 Browser modules, Angular, React and definitions with better documentation.

Our IBAN is PK56 HABB 0050237000223103. Bank: HBL Pakistan. Title: Syed Ahmed Haider

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.