Skip to content

Lightweight utility to proxy api requests from main thread to web worker.

Notifications You must be signed in to change notification settings

scidock/proxyworker

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

35 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

proxyworker

CircleCI

Lightweight utility to proxy api requests from main thread to web worker. Run the tests in your browser here!

Usage

// Main thread
const {ProxyWorker} = require('proxyworker');

const worker = new ProxyWorker(new Worker('./path/to/worker.js'));

// Call 'echo' method on worker with some arguments.
// This returns a promise that will resolve with the worker api response.
worker.callWithArgs('echo', ['a', 'b', 'c', 1, 2, 3]);

// Subscribe to worker event.
worker.subscribe('breakfast', breakfast => {
  // breakfast => ['green', 'eggs', 'and', 'tofu']
});
// worker.js
const {emit, proxy} = require('proxyworker');

proxy({
  methods: {
    echo: function() {
      let result = Array.prototype.slice.call(arguments);
      return Promise.resolve(result);
    }
  },

  events: [
    'breakfast',
  ]
});

// Time for breakfast?
setInterval(function() {
  emit('breakfast', [
    'green',
    'eggs',
    'and',
    'tofu',
  ]);
}, 24 * 60 * 60 * 1000);

About

Lightweight utility to proxy api requests from main thread to web worker.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages