1. inline-worker
utility to create a universal WebWorker from a function
inline-worker
Package: inline-worker
Created by: mohayonao
Last modified: Sun, 19 Jun 2022 01:23:08 GMT
Version: 1.1.0
License: MIT
Downloads: 27,262
Repository: https://github.com/mohayonao/inline-worker

Install

npm install inline-worker
yarn add inline-worker

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

Dependencies

RELATED POST

10 Must-Know Windows Shortcuts That Will Save You Time

10 Must-Know Windows Shortcuts That Will Save You Time

Arrays vs Linked Lists: Which is Better for Memory Management in Data Structures?

Arrays vs Linked Lists: Which is Better for Memory Management in Data Structures?

Navigating AWS Networking: Essential Hacks for Smooth Operation

Navigating AWS Networking: Essential Hacks for Smooth Operation

Achieving Stunning Visuals with Unity's Global Illumination

Achieving Stunning Visuals with Unity's Global Illumination

Nim's Hidden Gems: Lesser-known Features for Writing Efficient Code

Nim's Hidden Gems: Lesser-known Features for Writing Efficient Code