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

nestableflow

v0.0.14

Published

Nestable flow-control module.

Downloads

6

Readme

flow.js

Asynchronous flow-control (serial, parallel, repeat, wait, function) module for Node.js, RequireJS, and browser.

Installation (Node.js)

$ npm install nestableflow

Inspired

Features

  • Nesting flow.
  • Group error handlers together in one handler.

Definition

  • flow - Instance of Flow. The block of process contains some flow or function.
  • actor - Instance of Flow or Function. The components making up a flow.

API Documentation

Class Methods

  • serial(actor[, actor, ...]) - [static] Create serial flow with arguments of actor. (suger)
  • serialActors(actors) - [static] Create serial flow with array of actor.
  • parallel(actor[, actor, ...]) - [static] Create parallel flow with arguments of actor. (suger)
  • parallelActors(actors) - [static] Create parallel flow with array of actor.
  • repeat(actor, repeatCount) - [static] Repeat actor.
  • wait(delay) - [static] Create an actor that contains timer.

Member Properties

  • currentPhase - Indicates the number of actor that is running now.
  • totalPhase - Indicates the total number of actors in the flow.

Member Methods

  • start() - Starts the flow, if it is not already running.
  • next() - Takes the flow into its next actor.
  • stop() - Stops the flow.
  • reset() - Stops the flow, if it is running, and sets the currentPhase property back to 0.

Event Handlers

  • onError - Calls when arguments of callback have some error object.
  • onComplete - Calls when the flow is finished.

Actor's method implement

Syncronous Actor

Just call flow.next() on complete of the action.

function (flow, [argument, ...]) {
  // do something
  flow.next();
}

Asyncronous Actor

Set callback flow.next.

function (next, [argument, ...]) {
  fs.readFile('foo.txt', 'utf8', flow.next);
}

Phase Concept

Serial Flow

var root = Flow.serial(
  function (flow) {
    console.log(flow.currentPhase, flow.totalPhase);    // 0 3
    setTimeout(function () {
      console.log(flow.currentPhase, flow.totalPhase);  // 0 3
      flow.next();
    }, 300);
  },
  function (flow) {
    console.log(flow.currentPhase, flow.totalPhase);    // 1 3
    setTimeout(function () {
      console.log(flow.currentPhase, flow.totalPhase);  // 1 3
      flow.next();
    }, 100);
  },
  function (flow) {
    console.log(flow.currentPhase, flow.totalPhase);    // 2 3
    setTimeout(function () {
      console.log(flow.currentPhase, flow.totalPhase);  // 2 3
      flow.next();
    }, 200);
  }
);
root.onComplete = function () {
  console.log(root.currentPhase, root.totalPhase);      // 3 3
};
root.start();

Parallel Flow

var root = Flow.parallel(
  function (flow) {
    console.log(flow.currentPhase, flow.totalPhase);    // 0 3
    setTimeout(function () {
      console.log(flow.currentPhase, flow.totalPhase);  // 2 3
      flow.next();
    }, 300);
  },
  function (flow) {
    console.log(flow.currentPhase, flow.totalPhase);    // 0 3
    setTimeout(function () {
      console.log(flow.currentPhase, flow.totalPhase);  // 0 3
      flow.next();
    }, 100);
  },
  function (flow) {
    console.log(flow.currentPhase, flow.totalPhase);    // 0 3
    setTimeout(function () {
      console.log(flow.currentPhase, flow.totalPhase);  // 1 3
      flow.next();
    }, 200);
  }
);
root.onComplete = function () {
  console.log(root.currentPhase, root.totalPhase);      // 3 3
};
root.start();

Repeat Flow

var root = Flow.repeat(
  function (flow) {
    console.log(flow.currentPhase, flow.totalPhase);    // 0 3
                                                        // 1 3
                                                        // 2 3
    flow.next();
  },
  3
);
root.onComplete = function () {
  console.log(root.currentPhase, root.totalPhase);      // 3 3
};
root.start();

Usage

File System: write -> read -> unlink

file.js

var fs = require('fs');
var Flow = require('nestableflow').Flow;
var now = (new Date()).toString();
var file = 'temp/file.txt';

// Callback style
fs.writeFile(file, now, function (err) {
  if (err) {
    console.log('error', err);
    return;
  }
  fs.readFile(file, 'utf8', function (err, data) {
    if (err) {
      console.log('error', err);
      return;
    }
    fs.unlink(file, function (err) {
      if (err) {
        console.log('error', err);
        return;
      }
      console.log('complete');
    });
  });
});

// Flow style
var root = Flow.serial(
  function (flow) {
    fs.writeFile(file, now, flow.next);
  },
  function (flow) {
    fs.readFile(file, 'utf8', flow.next);
  },
  function (flow, data) {
    console.log(data);
    fs.unlink(file, flow.next);
  }
);
root.onError = function (err) {
  console.log('error', err);
};
root.onComplete = function () {
  console.log('complete');
};
root.start();

Mongoose: save -> find

mongoose.js

var mongoose = require('mongoose');
var Flow = require('nestableflow').Flow;
var now = new Date();

mongoose.connect('mongodb://localhost/node_flow');
mongoose.model(
  'Example',
  new mongoose.Schema({
    date: Date
  })
);
var Example = mongoose.model('Example');

// Callback style
var example = new Example({
  date: now
});
example.save(function (err) {
  if (err) {
    console.log('error', err);
    return;
  }
  Example.find({}, [], function (err, examples) {
    if (err) {
      console.log('error', err);
      return;
    }
    console.log(examples);
    console.log('complete');
  })
});

// Flow style
var root = Flow.serial(
  function (flow) {
    var example = new Example({
      date: now
    });
    example.save(flow.next);
  },
  function (flow) {
    Example.find({}, [], flow.next);
  },
  function (flow, examples) {
    console.log(examples);
    flow.next();
  }
);
root.onError = function (err) {
  console.log('error', err);
};
root.onComplete = function () {
  console.log('complete');
};
root.start();

RequierJS

How to load flow.js with RequierJS

<script type="text/javascript" src="require.js"></script>
<script type="text/javascript">
  require(['../../lib/flow'], function (Flow) {
    console.log(Flow);
  });
</script>

License

Licensed under the MIT license.