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 

/ src / handleAction.js

import { isError } from 'flux-standard-action';

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

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

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

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

  return 'next';
}

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

    const handlerKey = getHandlerKey(action);

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

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

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