Skip to content
This repository has been archived by the owner on Jun 9, 2019. It is now read-only.

mohayonao/inline-worker

master
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Code

Latest commit

 

Git stats

Files

Permalink
Failed to load latest commit information.
Type
Name
Latest commit message
Commit time
 
 
 
 
 
 
 
 
 
 
 
 
 
 

inline-worker

Build Status NPM Version License

JavaScript utility to create a universal WebWorker from a function

Installation

$ npm install inline-worker

API

InlineWorker

  • constructor(func: function, [ self: object ]): Worker | InlineWorker

Example

const InlineWorker = require("inline-worker");

let self = {};
let worker = new InlineWorker(function(self) {
  self.onmessage = function(e) {
    postMessage(self.bark(e.data)); // (2) hello!!
  };

  // worker internal function
  self.bark = function(msg) {
    return msg + "!!";
  };
}, self);

worker.onmessage = function(e) {
  console.log(e.data + "!!"); // (3) hello!!!!
};

worker.postMessage("hello"); // (1)

What is worker instance?

if (global.window === global) {
  assert(worker instanceof Worker); // in the borwser
} else {
  assert(worker instanceof InlineWorker); // in the node.js
}

You can test worker internal functions via self.

assert(self.bark("bye") === "bye!!");

License

MIT

About

JavaScript utility to create a WebWorker from a function

Resources

License

Stars

Watchers

Forks

Packages

No packages published