varo

    0.3.0 • Public • Published

    logo

    pattern matched composition for browser apps

    varo

    Build Status Gitter

    varo is a pattern matched logic library designed for the browser. Varo is designed to compliment Seneca by providing a similar, albeit, smaller set of API's. It's ideal use case is in browser-side apps.

    The focus of Varo is to provide the ability to compose logic around patterns; it does not handle transport or any other concerns.

    If you're using this module, and need help, you can:

    Install

    To install, simply use npm,

    npm install varo

    Test

    To run tests, simply use npm:

    npm run test

    Quick Example

    'use strict'
     
    var Varo = require('varo')()
     
    // Only one handler will be called, more specific wins,
    // then insertion order is checked on two identical matches.
    Varo.handle({role: 'sum'}, function (msg, done) {
      return done(null, {answer: (msg.left + msg.right)})
    })
     
    // Won't be called unless the pattern above was removed
    Varo.handle({role: 'sum'}, function (msg, done) {
      return done(null, {answer: (msg.left - msg.right)})
    })
     
    // Listen for anything emitted via emit
    Varo.observe({role: 'user'}, function (msg) {
      console.log(msg.user.name)
    })
     
    Varo.emit(role: 'user', event: 'change', user: {name: 'Dean'}})
     
    // Get a response to a message. Handy for asking for data or
    // making calculations.
    Varo.act({role: 'sum', left: 1, right: 2}, function (err, reply) {
      console.log(reply)
    })
     
    // You don't have to use a callback, if you are running an handler that
    // doesn't respond then it is ok to leave it out.
    Varo.act({role: 'sum', left: 1, right: 2})
     

    API

    .handle(msg, handler(err, reply)) : this

    Adds a handler for the msg provided. The handler is called when .act() is used. Note that only one handler will be called.

    Varo.handle({role: 'sum'}, function (msg, done) {
      return done(null, {answer: (msg.left - msg.right)})
    })

    .observe(msg, observer(msg)) : this

    Adds an observer that listens for any message matching the msg param. The observer is called when .emit() is used. Multiple observers can handle a single message; there is no callback

    Varo.observe({role: 'sum'}, function (msg) {
      console.log(msg)
    })

    .act(msg [, reply]) : this

    Sends the provided message to any interested single handler. Calls to act can be fire and forget or request response as necessary.

    Varo.act({role: 'sum', left: 1, right: 2}, function (err, reply) {
      console.log(reply)
    })
     
    Varo.act({role: 'sum', left: 1, right: 2})

    .emit(msg) : this

    Sends the provided message to any interested observers. Calls to emit have no callback and are considered fire and forget. This makes .emit() / .observere() a great pattern matched replacement for event emitters.

    Varo.handle({role: 'user', event: 'changed'}, function (msg) {
      console.log(msg.user.name)  
    })
     
    Varo.emit({role: 'user', event: 'changed', user: {name: 'Dean'})

    .plugin(plugin(Varo)) : this

    Calls the provided function with the current instance of varo. Useful to group functionality together in modular format.

    Varo.plugin(function (varo) {
      varo.handle({role: 'sum'}, function (msg, done) {
        return done(null, {answer: (msg.left + msg.right)})
      })
     
      varo.observe({role: 'sum'}, function (msg) {
        console.log(msg)
      })
    })
     
    Varo.act({role: 'sum', left: 1, right: 2}, function (err, reply) {
      console.log(reply)
    })

    .removeHandler(handler) : this

    Removes a named handler. Does not work for anonymous functions.

    var handler = function (msg, done) {
      return done(null, {answer: (msg.left + msg.right)})
    }
     
    Varo.handle({role: 'sum'}, handler)
    Varo.removeHandler(handler)

    .removeObserver(observer) : this

    Removes a named observer. Does not work for anonymous functions.

    var observer = function (msg) {
      console.log(msg)
    }
     
    Varo.observe({role: 'sum'}, observer)
    Varo.removeObserver(observer)

    Contributing

    The Senecajs org encourages open participation. If you feel you can help in any way, be it with documentation, examples, extra testing, or new features please get in touch.

    License

    Copyright (c) 2015, Dean McDonnell and other contributors.
    Licensed under MIT.

    Keywords

    none

    Install

    npm i varo

    DownloadsWeekly Downloads

    11

    Version

    0.3.0

    License

    MIT

    Last publish

    Collaborators

    • mcdonnelldean