Why Gemfury? Push, build, and install  RubyGems npm packages Python packages Maven artifacts PHP packages Go Modules Bower components Debian packages RPM packages NuGet packages

pfchangs / redux-actions   js

Repository URL to install this package:

Version: 0.9.0-alpha 

/ lib / handleAction.js

'use strict';

exports.__esModule = true;
exports['default'] = handleAction;

var _fluxStandardAction = require('flux-standard-action');

function isFunction(val) {
  return typeof val === 'function';
}

function inArray(array, val) {
  return array.indexOf(val) !== -1;
}

function getHandlerKey(action) {
  if (_fluxStandardAction.isError(action)) return 'throw';

  if (action.sequence && inArray(['start', 'return'], action.sequence.type)) {
    return action.sequence.type;
  }

  return 'next';
}

function handleAction(type, reducers) {
  return function (state, action) {
    // If action type does not match, return previous state
    if (action.type !== type) return state;

    var handlerKey = getHandlerKey(action);

    // If function is passed instead of map, use as reducer
    var reducersMap = isFunction(reducers) ? { next: reducers, 'throw': reducers } : reducers;

    // Otherwise, assume an action map was passed
    var reducer = reducersMap[handlerKey];

    return isFunction(reducer) ? reducer(state, action) : state;
  };
}

module.exports = exports['default'];