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

flui

v0.1.0

Published

Async flow control library which uses event or queue approach to avoid callback hell

Downloads

12

Readme

Flui

Flui is an async flow control for Node and browsers which uses event or queue approach. With Flui you can completely avoid callback hell. With events, each action is loosely coupled to others so it allows you to compose them very flexibly. Queue approach is more straightforward and it allows you to write asynchronous code similar to synchronous way. Enough talking, let's see an example.

Examples

Events

Imagine you want to implement simple CRON in Node. Our script will execute all files in our directory tree in an interval. We won't check if the file is executable because it is unnecessary for our purpose.

Traditional way

var fs = require('fs');
var spawn = require('child_process').spawn;
var path = require('path');

var flui = require('flui');

var dir = 'path/to';

setInterval(function () {
    fs.readdir(dir, function (err, files) {
        if (err) console.log(err);
        
        files.forEach(function (file) {
            fs.stat(path.join(dir, file), function (err, stats) {
                if (err) console.log(err);
            
                if (stats.isFile()) {
                    spawn(path.join(dir, file));
                }
            });
        });
    });
}, 60 * 60 * 1000);

Flui way

var fs = require('fs');
var spawn = require('child_process').spawn;
var path = require('path');

var flui = require('flui');
var context = flui.context();

var dir = 'path/to';

setInterval(context.then('tick'), 60 * 60 * 1000);

context.on('tick', function () {
    fs.readdir(dir, context.then('files'));
});

context.on('files', function (err, files) {
    if (err) context.trigger('error', err);
    files.forEach(context.then('file'));
});

context.on('file', function (file) {
    fs.stat(path.join(dir, file), function (err, stats) {
        context.trigger('stats', err, stats, file)
    });
});

context.on('stats', function (err, stats, file) {
    if (err) context.trigger('error', err);
    if (stats.isFile()) {
        spawn(path.join(dir, file));
    }
});

context.on('error', function (err) {
    //one error hadler for all errors
    console.log(err);
});

Queue

Queue is useful for sequence of asynchronous operations. Assume you want to write method to authenticate a user.

Traditional way

var db = require('db');
var bcrypt = require('bcrypt');

var flui = require('flui');

db.
model('users').
findOne(req.param('username')).
exec(function (err, user) {
    if (err) res.send(err);
    
    if (user) {
        bcrypt.compare(req.param('password'), user.password, function (err, valid) {
            if (err) res.send(err);
            
            if (valid) {
                user.online = true;
                
                user.save(function (err, user) {
                    req.session.auth = true;
                    res.send(user);
                });
            }
        }
    }
});

Flui way

var db = require('db');
var bcrypt = require('bcrypt');

var flui = require('flui');

var queue = flui.queue([
    function (username) {
        db.
        model('users').
        findOne(username).
        exec(queue.wait());
    },
    function (err, user) {
        if (err) throw err;
        
        if (user) {
            bcrypt.compare(req.param('password'), user.password, function (err, valid) {
                if (err) queue.raiseError(err);
                queue.next(valid, user);
            });
        }
    },
    function (valid, user) {        
        if (valid) {
            user.online = true;
            user.save(queue.wait());
        }
    },
    function (err, user) {
        req.session.auth = true;
        res.send(user);
    }
]).
error(function (err) {
    //one handler for all errors
    res.send(err);
}).
run(req.param('username'));

Custom eventable library

Definition

function UserService(url) {
    this.url = url;
    this.session = {};

    var self = this;

    this.on('_login', function (result) {
        if (result.error) self.trigger(['login:error', 'error'], result.error);
        else {
            self.trigger(['login:success', 'success'], result);
            self.session = result;

            socket.subscribe(result.id, self.then('_subscription'));
        }
    });
    
    this.on('_subscription', function (subscription) {
        subscription.onMessage('logout', self.then('_logout'));
        subscription.onMessage('friend:online', self.then('friend:online'));
        subscription.onMessage('friend:offline', self.then('friend:offline'));
    });
    
    this.on('_logout', function (user) {
        socket.unsubscribe(user.id);
        self.session = {};
        self.trigger(['logout:success', 'success'], user);
    });
}

UserService.prototype.login = function (credentials) {
    http.post(this.url + '/login', credentials, this.then('_login'));
};

UserService.prototype.logout = function () {
    http.post(this.url + '/logout', this.then('_logout'));
};

//extend service
flui.eventable(UserService);

Usage

var user = new UserService('/api/users');

//profile component
user.on('login:success', function (user) {
    //display user info
});

//storage component
user.on('login:success', function (user) {
    //set user into session storage
});

//flash component
user.on('login:error', function (error) {
    //display error message
});

user.on('logout:success', function () {
    //display logout message
});

//chat messages component
user.on('friend:online', function (friend) {
    //add friend to collection
});

user.on('friend:offline', function (friend) {
    //remove friend from collection
});

Installation

Node

npm install flui

Browser

<script type="text/javascript" src="path/to/flui.js"></script>

API

flui.context()

Creates new event context. You can bind and trigger events on it.

  • on (event, callback) - binds a listener to given event
  • trigger (event, [args...]) - triggers the event and pass given arguments to listeners
  • then (event) - returns a function which triggers given event immidiately it will be executed (arguments are also passed)

flui.queue(actions)

Creates a queue of actions which will be executed in diven order. It also allows to add actions later or stop the execution.

  • next ([args...]) - invokes next action in queue and passed given arguments
  • wait () - returns a function which invokes next action immidiately it will be executed (arguments are also passed)
  • run ([args...]) - starts the execution and passes arguments into first action
  • stop () - stops the execution
  • error (callback) - given callback will be called when an error occurs (there could be at most one listener)
  • raiseError (error) - raises an error on queue when throw statement couldn't be used (in asynchronous operations)

flui.eventable(toExtend)

Extends given object/class to bind and trigger events on it. It will get the same API as in flui.context(). The argument could be object, instance of function or function (class).

License

Tipograph is MIT licensed. Feel free to use it, contribute or spread the word. Created with love by Petr Nevyhoštěný (Twitter).