npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

funcwork

v2.1.2

Published

Run pure function in web worker

Downloads

15

Readme

funcwork

🍵 Run pure function in Web Worker easily. Allows add functions to Web Worker dynamically, there are no preset .js files.

online demo

Why

This package help you use Web Worker in a human way.

Install

npm i funcwork

Usage

base

import { FuncWork } from 'funcwork';

const fw = new FuncWork();
const add = (a, b) => a + b;
fw.add(add);

// The `add` function will be running in Web Worker.
// So you can do other things in main process.
fw.invoke(add, [1, 1]).then(result => {
  console.log(result); // 2
})

add multiple functions

import { FuncWork } from 'funcwork';

const fw = new FuncWork();
const add = (a, b) => a + b;
function sub(a, b) {
  return a - b;
}
fw.add(add, sub);

invoke

import { FuncWork } from 'funcwork';

const fw = new FuncWork();
const add = (a, b) => a + b;
fw.add(add);
fw.invoke(add, [1, 1]).then(result => {
  console.log(result); // 2
})

// or
fw.invoke('add', [1, 2]).then(result => {
  console.log(result); // 3
})

// recommended way
(async () => {
 try {
    const result = await fw.invoke('add', [1, 3]);
    console.log(result); // 4
 } catch (err) {
    // It may be an exception thrown by the Web Worker, or the process of function execution
    console.log(err);
 }
})()

destroy

import { FuncWork } from 'funcwork';

const fw = new FuncWork();

// ...

// Destroy Funcwork instance, it will clear all function and terminate Web Worker instance.
fw.destroy();

API

Class

Funcwork

  • Arguments

    • options: Web Worker options, get detail here
  • Returns

    Funcwork instance

  • Usage

    Create Funcwork instance

    import { FuncWork } from 'funcwork';
      
    const fw = new FuncWork();
    // or 
    const fw = new FuncWork({
        credentials: '',
        name: '',
        type: ''
    });

Instance Methods

add

  • Arguments

    • { Function } fn
  • Returns

    this

  • Usage

    Add function to Web Worker

    import { FuncWork } from 'funcwork';
      
    const fw = new FuncWork();
      
    function add (a, b) {
        return a + b;
    }
    fw.add(add);
    fw.add(function sub(a, b) { return a - b});
      
      
    // multiple
    const f3 = () => { 
    	// ... 
    };
    fw.add(function f1() {}, function f2, f3);
      
    // Chain calls
    fw.add(function f4() {})
       .add(function f5() {});

invoke

  • Arguments

    • { string | Function } name
    • { Array } params
  • Returns

    • Promise data is the result returned after the function is executed
  • Usage

    Call the registered function

    import { FuncWork } from 'funcwork';
      
    const fw = new FuncWork();
      
    function add (a, b) {
        return a + b;
    }
    fw.add(add);
      
    fw.inoke(add, [1, 2]).then(data => {
        console.log(data); // 3
    })
    fw.invoke('add', [1, 3]).then(data => {
        console.log(data); // 4
    })

remove

  • Arguments

    • { string | Function } name
  • Returns

  • Usage

    Remove a registered function

    import { FuncWork } from 'funcwork';
      
    const fw = new FuncWork();
    function add (a, b) { return a + b; }
    fw.add(add);
      
    fw.remove('add');
    // or
    fw.remove(add)
      
    fw.invoke(add) // throw new Error('add is not defined in Funcwork.')

clear

  • Arguments

  • Returns

  • Usage

    Clear all registered functions

    import { FuncWork } from 'funcwork';
      
    const fw = new FuncWork();
    function add (a, b) { return a + b; }
    fw.add(add);
      
    fw.clear();
    fw.invoke(add) // throw new Error('add is not defined in Funcwork.')

list

  • Arguments

  • Returns

  • Usage

    List all registered functions

    import { FuncWork } from 'funcwork';
      
    const fw = new FuncWork();
      
    function add (a, b) { return a + b; }
    function sub(a, b) { return a - b; }
    fw.add(add, sub);
    fw.list(); // "add | sub"

destroy

  • Arguments

  • Returns

  • Usage

    Clear all function and terminate Web Worker instance.

    import { FuncWork } from 'funcwork';
      
    const fw = new FuncWork();
    // do something...
      
    fw.destroy();