command-chain

command chain

npm install command-chain
20 downloads in the last week
20 downloads in the last month

Simple Command Chaining

install

npm install command-chain

api

  • class CommandChain
    • string wait if you want don't direct call next command in function type command. return this property in method
    • int length() length of CommandChain commands
    • void add(Command) add command to CommandChain
      • interface Command
        • void execute(CommandChain)
    • void add(function) add function command to CommandChain
      • function func(CommandChain)
    • void remove(Command or function) remove command from CommandChain
    • void execute([int start], [int end], [function callback]) execute commands
    • void next() execute next command

on coffee-script

{CommandChain} = require('command-chain')

class Cmd1
    execute : (chain) =>
        console.log('start cmd1')
        setTimeout ->
            console.log('complete cmd1')
            chain.next()
        , 1000

class Cmd2
    execute : (chain) =>
        console.log('start cmd2')
        setTimeout ->
            console.log('complete cmd2')
            chain.next()
        , 1000

chain = new CommandChain

# add Command type instance
chain.add(new Cmd1)
chain.add(new Cmd2)

# add function (direct call next command)
chain.add ->
    console.log('complete function1')
chain.add (chain) ->
    console.log('complete function2')

# add function (waiting chain.next())
chain.add (chain) ->
    console.log('start function3)
    setTimeout ->
        console.log('complete function3)
        chain.next()
    , 1000
    chain.wait

# execute all commands
chain.execute()
# execute all commands and execute callback function
# chain.execute ->
#     console.log('complete all commands')
# execute 2.. commands
# chain.execute(2)
# execute 0..4 commands
# chain.execute(0, 4)
npm loves you