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

fluorine

v1.0.5

Published

Fluorine node Package to run js flows better than promises

Downloads

5

Readme

Fluorine

npm-image bower-image

Fluorine - Flow based programing abstraction.

Fluorine can simply be though of as an abstraction or a DSL. It is a structure of code in which you can manage complex asynchronous code with ease.

Probably one of the most important features of Fluorine is its non-intrusive philosophy, since it separates code from structure.

This "structure" is though of as a flow within a graph, where each node in the graph is a function that runs some synchronous or asynchronous code.

Now, even though this sounds very simple, it is quite powerful, because you can code the "flow" which is the order in which functions will run, and then go ahead and code the behavior that will run within each node separately.

This simple fact, allows for clean, maintainable, simpler and atomic code. Removing callback-hells, nesting, smells, complexity.

You may not realize until you get there, but refactoring a flow consists of re-arranging the order of the nodes, while on the other hand you'll probaly produce a lot of bugs while trying to refactor all the mixed, nested complexity that lies in front of you. That is.. if you return alive from mordor.

Now, within the Fluorine world, the program is a graph, the execution of the program is called a flow and each node is called a step.

Let us Orchestrate asynchronous and synchronous code!

Usage

Basically you instantiate a flow. Then you add a step to the flow. Any step can have other steps as dependencies.

When the program runs, it will run each step only if all of its dependencies have been met.

Dependencies/Requirements

Require the libraries needed for Fluorine

  1. Neon
  2. CustomEventSupport (part of the Neon stdlib)
  3. NodeSupport (part of the Neon stdlib)

Geting Started

Suppose you need to execute two asynchronous functions (someAsyncFn, anotherAsyncFn) and when both are resolved execute something with the response of the previous asynchronous functions.

var flow = new Flow({name: 'test'});

flow.step('bar')(function (step) {
    someAsyncFn(function (someData) {
        step.success(someData);
    });
});

flow.step('baz')(function (step) {
    anotherAsyncFn(function (moreData) {
        step.success(moreData);
    });
});

flow.step('foo').dependsOn('bar', 'baz')(function (step) {
    // step.data.bar has someData as passed by the bar step
    // step.data.baz has moreData as passed by the baz step
    // Execute something else and then finish
    step.success();
});

Examples

No more coupling async methods with unecessary nesting, let suppose you have fn1, fn2, fn3 and fn4 which are function that are asynchronous, and you want to call them in sequence after each other

Using nested callbacks

fn1(function () {
    // execute some code
    fn2(function () {
        // more code
        fn3(function () {
            // ...
            fn4(function () {
                // finalize
            });
        });
    });
});

Using Fluorine

var f = new Flow({name: 'testFlow'});

f.step('1')(function (step) {
    // execute some code
    fn1(step.success);
});

f.step('2').dependsOn('1')(function (step) {
    // more code
    fn2(step.success);
});

flow.step('3').dependsOn('2')(function (step) {
    // ...
    fn3(step.success);
});

flow.step('4').dependsOn('3')(function (step) {
    // finalize
    fn4(step.success);
});

Let run fn1, fn2 and fn3 and only execute fn4 after the previous funcitons finish

var f = new Flow({name:'dependentFlow'});

f.step('1')(function (step) {
    fn1(step.success);
});

f.step('2')(function (step) {
    fn2(step.success);
});

f.step('3')(function (step) {
    fn3(step.success);
});

f.step('4').dependsOn('1', '2', '3')(function (step) {
    fn4(step.success);
});

Execute fn1 and fn2 then when both are ready execute fn3 and then fn4

var f = new Flow({name: 'lastFlow'});

f.step('1')(function (step) {
    fn1(step.success);
});

f.step('2')(function (step) {
    fn2(step.success);
});

f.step('3').dependsOn('1', '2')(function (step) {
    fn3(step.success);
});

f.step('4').dependsOn('3')(function (step) {
    fn4(step.success);
});

Error handling

If you want to handle errors in the execution of one of your nodes, you can pass an additional function parameter to the step execution, that will be called in case of error.

To trigger an error, you must explicitely call step.fail(error) for other nodes to be notified

var f = new Flow({ name : 'flow' })

f.step('foo')(function(step) {
    step.success();
});

f.step('bar')(function(step) {
    errorData = { error : 'some' };
    step.fail(errorData);
});

f.step('baz').dependsOn('foo', 'bar')(function(step) {
    // This won't be executed
}, function(step) {
    console.log("Error: ", step.errors.bar);
});