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

thread-promisify

v1.0.6

Published

Add the possibility of executing functions in an other thread. Works with Electron.

Downloads

6

Readme

thread-promisify: Execute functions in a thread

Allows you to execute methods of a class or an object into another thread, so application will have better performance.

Object / Class have to be in an module separated from the main process.

Exemple.class.js

// Example.class.js

class Example {
    constructor() {

    }
    
    // I know, it is not very useful, but it 
    // demonstrates well the power of thread
    sleep(ms) { return new Promise(function(resolve, reject) {
        var waitTill = new Date(new Date().getTime() + ms);
        while(waitTill > new Date()){};
        resolve();
    }); }
    
    hello(s) { return new Promise(function(resolve, reject) {
        return "hello "+s;
    });}

}


exports.Example = Example;
exports.ExampleStatic = {
    aStaticMethod: function() { return new Promise(function(resolve, reject) {
        return "Here you can read and parse a file";
    }); }
};

index.js

const { Threadify } = require("thread-promisify");
const { Example, ExampleStatic } = require('./threadify.example');

// No arguments, all methods are threadified
let ThreadifyExample = Example.Threadify();

var ex = new ThreadifyExample();
ex.sleep(2000).then(function() {
    console.log("Done !");
});

// Just threadify some methods and not all
let ThreadifyExample = Example.Threadify(["sleep"]);
var ex = new ThreadifyExample();
ex.sleep(2000).then(function() {
    console.log("I was executed in a thread !");
});
ex.hello("John Doe").then(function(s) {
    console.log(s);
    console.log("And I was not executed in a thread me.");
})

// Threadify an object
let Statics = Threadify(ExampleStatic);
Statics.aStaticMethod().then(function() {
    console.log("I was an object and I am executed in a thread");
})

Configuration

You can configure Threadify like this.

NOTE: For electron users, add this code in the main.js and not in the renderer.

const { ThreadifyConfig } = require("thread-promisify");

// The max number of threads who will be executed at the same time
// Default to CPU cores-1 or 1 if only 1 CPU core
ThreadifyConfig.maxThreads = 1;

// If set to true, when a Thread is inactive
// it will be killed and recreated from scratch later
// Set false if you keep static object for performance issue
// Default to true
ThreadifyConfig.killThreadsWhenInactive = true;

// The log level of the module, values: 0, 1, 2, 3
ThreadifyConfig.logLevel = 0;

Use a specific thread

You can tell a threadified method to use the same specific thread when it is available

myObject.myMethod(myArg1, myArg2, {threadify: {thread: "a-specific-thread"}}).then(function() {
    console.log("I will be using always the same thread");
});

Variables types supported

Object and variables are serialized because we use child process for thread. So if you send back an object, you will have to instantiate back again in your main thread.

Only some basic variable types are supported:

  • String
  • Number
  • Array
  • TypedArray (Uint8array, ...)

If you send an object back, they will become anonymous object without methods.

In the future, objects will be recreated but the constructor will have to be in an exports

Use a function in an method call

You can pass a function, like a progress function, as a parameter, without complications, as simple as that.

function _progress(step) {
    console.log("I am called in the main process ! Step: ", step);
}

myObject.myMethod(_progress).then(function() {
    _progress(1);
    _progress(2);
});

The method will be fired in the main process, and not in the thread process.

Event Handlers

Currently, EventEmitter is not supported by this. But in the future, maybe ! So when you will fire an event, the event will be send back to the main process too.