sequence-js

0.0.3 • Public • Published

sequence-js

Super simple sequence script. Create sequences and execute them independently.

var Sequence = require('sequence-js')

// Create sequence and add some callbacks
var loginSequence = new Sequence()
    .add(requireUsernameOrEmail)
    .add(requirePassword)
    .add(authenticate)
    .catch(loginErrors)

// ....
// on login request
    // exec(arg1, arg2, ..., argN)
    loginSequence.exec(request, response)
    // loginSequence.exec(request, response) // will work fine, but no point here


// ...
function requireUsernameOrEmail(req, res) {
    if (req.query.username || req.query.email)
        // next(arg1, arg2, ..., argN)
        this.next(req, res, req.query.username || req.query.email)
    else
        // error(arg1, arg2, ..., argN)
        this.error(res, 'Must provide username or email!')
}

function requirePassword(req, res, identity) {
    if (req.query.password)
        this.next(req, res, identity, req.query.password)
    else
        this.error(res, 'Must provide a password!')
}

function authenticate(req, res, id, pw) {
    if (/*** check credentials ***/)
        res.autenticate(id, true) // or w/e you do :>
    else
        this.error(res, 'Invalid login credentials.')
}

function loginErrors(res, reason) {
    res.reply(403, reason)
}

API

Some shitty API documentation...
Assume var Sequence = require('sequence-js')

on sequence objects

new Sequence()

var sequence = new Sequence()
create a new sequence

.add(function(arg1, arg2, ..., argN){ ... })

add a function to the sequence

.catch(function(arg1, arg2, ..., argN) { ... })

Set error handler function. Called via this.error(...)

.exec(arg1, arg2, ..., argN)

execute the sequence asynchronously, i.e multiple concurrent executions are ok.

.wrap()

returns a function-style callable version of the sequence is basically a wrap around exec, e.g:

var routine = someSeq.wrap()
routine('yo', 'ja', 'mu')

inside callbacks

this.next()

call next sequenced function with supplied arguments

this.error()

skip and call the error handler with supplider arguments

Readme

Keywords

Package Sidebar

Install

npm i sequence-js

Weekly Downloads

3

Version

0.0.3

License

none

Last publish

Collaborators

  • smeets