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

node-fibers-synchronize-helper

v3.0.0

Published

helper for synchronize promise and callback and manage variables on Fiber Thread as ThreadLocal

Downloads

23

Readme

node-fibers-synchronize-helper

This helper uses Fibers coroutines by synchronize_f2 module (fork of https://www.npmjs.com/package/synchronize) and helps to manage promises and callbacks, and manage variables on underlying Fiber Thread like Java ThreadLocal, you will be able to synchronize callback and promises and develop syncronously as your did in Java returning Objects and catch exceptions, and set-get Objects on your Fiber Thread as you did with Java ThreadLocal.

Note version 3.0.x:

updated Fibers version to 3 by synchronize_f2 module (just a synchronize fork with pointing to Fibers 3)

Create/Start Fiber Thread:

var synchProm = require('node-fibers-synchronize-helper')

synchProm.executeSynch(function, callback)

     synchProm.executeSynch(function, function (err, res) {
                        if (err)
                            console.log("err", err)
                        else
                            console.log("OK:", res)
                    })

Promise:

var result = executePromiseFiberFn(Object/this, method/function, params ...)

var result = executePromiseFiberFnName(Object/this, method/function name, params ...)

example:

   var db = synchProm.executePromiseFiberFn(MongoClient, MongoClient.connect, url)
   synchProm.executePromiseFiberFn(collection, collection.insertMany, [{name:"Jim"}
      , {name:"Sarah", title:"Princess"}], {w:1})
      
    var db = synchProm.executePromiseFiberFnName(MongoClient, 'connect', url)  

Callback:

var result = executeFiberFn(Object/this, method/function, params ...)

example:

   var db = synchProm.executeFiberFn(MongoClient, MongoClient.connect, url)
   synchProm.executeFiberFn(collection, collection.insertMany, [{name:"Jim"}
      , {name:"Sarah", title:"Princess"}], {w:1})
      

Map results (keys are from result name array):

var resultMap = executeFiberFnMultiParamCb(Object/this, method/function, result name array , params ...)

example:

    var resMultiCbMap = synchProm.executeFiberFnMultiParamCb(this, multiParamCB, ['res1', 'res2'], "a", "b", "c")  
    

Fiber ThreadLocal:

synchProm.setThreadLocalValue("key", value)

synchProm.getThreadLocalValue("key")

Promise synchronization:

var synchProm = require('node-fibers-synchronize-helper')
 
var testSynch = function () {
    try{
    //promise function call
        var db = synchProm.executePromiseFiberFn(MongoClient, MongoClient.connect, url)
    //get synch object and call promise function
        var dbProm = synchProm.getSynchPromiseObj(db);
        var stats = dbProm.executePromiseFiber(db.stats)

    //cursor.toArray promise version
        var cursor = collection_test.find({});
        var resProm = synchProm.executePromiseFiberFn(cursor, cursor.toArray)



    }catch(e){
        console.log(e)
    }

}


synchProm.executeSynch(testSynch, function (err, res) {
    if (err)
        console.log("err", err)
    else
        console.log("OK:", JSON.stringify(res, null, 4))
})

Callback synchronization:

var synchProm = require('node-fibers-synchronize-helper')
 
var testSynch = function () {
    try{

    //cursor.toArray callback version
        cursor = collection_test.find({});
        var resCb = synchProm.executeFiberFn(cursor, cursor.toArray)
        assert.notEqual(null, resCb);

    //callback with param
        resCb = synchProm.executeFiberFn(cursor, cursor.count, true, {
            skip: 1
        })

    //multiparam result callback
        var resMultiCb = synchProm.executeFiberFnMultiParamCb(this, multiParamCB, ['res1', 'res2'], "a", "b", "c")   


    }catch(e){
        console.log(e)
    }

}


synchProm.executeSynch(testSynch, function (err, res) {
    if (err)
        console.log("err", err)
    else
        console.log("OK:", JSON.stringify(res, null, 4))
})

Fiber ThreadLocal:

 var synchProm = require('node-fibers-synchronize-helper')
 synchProm.setThreadLocalValue("key", value)
 synchProm.getThreadLocalValue("key")
 

example ThreadLocal:

var synchProm = require('node-fibers-synchronize-helper')

var timeoutCB = function (mills, cb) {

    setTimeout(function () {
        cb(null);
    }, mills);

}

var testThreadLocal = function () {



    for (i = 0; i < 10; i++) {
        (
            function (count) {
                var fiberThread = function () {
                    synchProm.setThreadLocalValue("count", count)

                    synchProm.executeFiberFn(this, timeoutCB, 1000)
                    console.log(count, synchProm.getThreadLocalValue("count"))
                    return count
                }
                synchProm.executeSynch(fiberThread, function (err, res) {
                    if (err)
                        console.log("err", err)
                    else
                        console.log("OK:", res)
                })
            })(i)
    }

}
testThreadLocal()