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

simplythread

v2.0.4

Published

JS Threads: Run arbitrary functions in immediatly-spawned browser threads with an async workflow

Downloads

34

Readme

SimplyThread

Build Status Coverage Code Climate

Sauce Test Status

Execute arbitrary functions in immediatly-spawned browser threads with an async Promise workflow. Relies on worker technology for threads and uses fallback methods for unsupported browsers.

Installation

npm install simplythread --save

Require (commonJS)

var SimplyThread = require('simplythread');

<script> tag

<script src="node_modules/simplythread/dist/simplythread.js"></script>

Example Usage

var thread = SimplyThread.create(function(a,b){
    return a+b;
});

thread.run(5, 10).then(function(result){ // Returns a promise
    console.log(result); // logs 15;
});



thread.setFn(function(a,b){ // Set a new function for the thread
    return (a+b)*b;
});

thread.run(5, 10).then(function(result){
    console.log(result); // logs 150;
});

thread.kill(); // Destroy thread.

API

SimplyThread.create([function])

Creates a new browser thread and returns a thread interface instance.

Arguments:

  • function (optional) - The function to set as the thread's main function that will be executed when calling thread.run().

SimplyThread.list()

Returns an array listing all active threads.

SimplyThread.killAll()

Convinence method that kills all active threads.

thread.run([arg1[, arg2[, ...]]])

Execute the function provided to the thread (either through SimplyThread.create(fn) or thread.setFn(fn)) with the provided arguments. Returns a promise which will either resolve with the result or reject upon error. If a function wasn't provided during thread creation or wasn't later set via thread.setFn the returned promise will be immediatly rejected.

thread.on(eventName, callback)

Registers the provided callback to be invoked when threadEmit(eventName) is called from inside the thread's main function.

Example:

var thread = SimplyThread.create(function(number){
    setTimeout(function(){
        threadEmit('myEvent', number*10);

        setTimeout(function(){
            threadEmit('myEvent', number*100);
        }, 50);
    }, 100);
});
thread.on('myEvent', (payload)=> console.log(payload)) // Logs '50' after 100ms, '500' after 150ms
thread.run(5);

Arguments:

  • eventName - the name of the event to register the callback with.
  • callback - the callback to invoke with the payload when threadEmit(eventName) is invoked from the thread.

thread.setFn(function[, context])

Sets the provided function as the main function that will be executed when invoking thread.run().

Arguments:

  • function - the function to set as the thread's main function.
  • context (optional) - an object which will be the context that the provided function will run under (i.e. the value of this in the function body).

thread.setContext(context)

Assigns the provided context as the thread's function's this value.

Arguments:

  • context - an object which will be the context that the thread's function will run under (i.e. the value of this in the function body).

thread.setScripts(scriptsArray)

Loads each external script provided in the scriptsArray on the thread's global scope. The scripts will be loaded asynchronously and the thread will wait for all scripts to be loaded before running any following .run() calls. If one of the scripts fail to load (i.e. they have a status code >= 400), any following .run() calls will be immediatly rejected with the script load error.

Example:

var thread = SimplyThread.create();
thread.setScripts(['https://code.jquery.com/jquery-3.1.1.js', 'MODULE:lodash']); // jQuery and Lodash will now be available on the thread's global scope.

thread.setScripts([function(){
    self.globalVariable = 'abc123';
}]) // globalVariable will now be available on the thread's global scope.

Arguments:

  • scriptsArray - array of scripts that the thread should attempt to load under its global scope. Possible values for scripts:
    • URL path - absolute path of a javascript file.
    • Module path - a string representing the name of an NPM package that'll be loaded via Browserify, preceded by a 'MODULE:' prefix. By default a module will be loaded on the thread's global scope under the package's name, but a custom name can be specified via 'MODULE:packageName#customName' syntax e.g. 'MODULE:lodash#_'.
    • Function - A function that'll be run on the thread's global scope (i.e. the this keyword will resolve to the thread's global scope). If the function returns a promise, the thread will await until it is resolved before running any .run() calls.

thread.setGlobals(object)

Sets/assigns each key:value from the provided object on the thread's global scope.

Example:

var thread = SimplyThread.create(function(){
    return typeof myVariable;
});

thread.run().then(function(result){ console.log(result);}) // Logs 'undefined'

thread.setGlobals({myVariable: 'someValue'});
thread.run().then(function(result){ console.log(result);}) // Logs 'string'

Arguments:

  • object - an object containing key:value pairs which will be set on the thread's global scope. The key will represent the name of the global variable that'll be assigned to its associated value.

thread.kill()

Destroys the thread and its interface if it hasn't already been destroyed.

Things to consider

You should generally delegate heavy/expensive computations to threads and avoid running tiny computations because transferring data between threads also takes a certain amount of time (depending on what you are transferring).