All files alley.js

100% Statements 28/28
80% Branches 8/10
100% Functions 8/8
100% Lines 25/25
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75        1x 1x 1x 1x 1x 1x 10x           1x             12x 12x                         12x 12x 12x 11x 11x 11x 11x                 12x 19x 10x 10x     19x 19x 19x                       1x    
/**
 * Module dependencies
 */
 
const flatten = require('./flatten')
const compose = require('./compose')
const isFSA = require('./is-fsa')
const slice = require('sliced')
const Tree = require('./tree')
const fold = require('./fold')
const ident = (v) => v
 
/**
 * Export `Alley`
 */
 
module.exports = Alley
 
/**
 * Initialize `Alley`
 */
 
function Alley () {
  const tree = Tree()
  return {
    send: Send(tree),
    use: Use(tree),
    alley: true,
    tree: tree
  }
}
 
/**
 * Send a value
 */
 
function Send (tree) {
  return function send (payload) {
    const action = fold(slice(arguments))
    if (!isFSA(action)) throw nonstandard(action)
    const root = tree.up('root') || []
    const parents = tree.up(action.type) || []
    const fn = compose(flatten(root.concat(parents)))
    return fn(action, ident).then(() => action)
  }
}
 
/**
 * Use a function
 */
 
function Use (tree) {
  return function use (namespace, fn) {
    if (arguments.length === 1) {
      fn = namespace
      namespace = 'root'
    }
 
    let fns = tree(namespace) || []
    fns = fns.concat(fn)
    tree(namespace, fns)
  }
}
 
/**
 * Non-standard
 *
 * @param {Mixed} action
 * @return {Error}
 */
 
function nonstandard (action) {
  return new Error('resolved action (' + JSON.stringify(action) + ') is not the correct format. Please refer to the spec: https://github.com/acdlite/flux-standard-action#actions')
}