All files index.js

100% Statements 21/21
100% Branches 7/7
100% Functions 12/12
100% Lines 17/17
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 581x 13x                       8x 8x 8x 1x   7x       8x                       5x 5x 5x 1x   4x                     12x 30x 13x     17x    
const INVALID_FUNCTION = 'Invalid callback function!';
const isFunction = fn => typeof fn === 'function';
 
/**
 * Ready function that accepts a `callback` and optional `options` as params
 * An ready action will be dispatched with provided options
 * before proceeding to the next targeted action
 *
 * @param  {Function} callback
 * @param  {Object}   [options={}]
 * @return {Object}
 */
export function ready(callback, options = {}) {
  return ({ dispatch, getState }) => {
    const p = new Promise((resolve, reject) => {
      if (!isFunction(callback)) {
        reject(INVALID_FUNCTION);
      } else {
        resolve(dispatch({ type: 'READY_ACTION', options }));
      }
    });
 
    return p.then(() => callback(dispatch, getState));
  };
}
 
/**
 * Wrap function that accepts a `callback` as param
 * NO ready action will be dispatched
 *
 * @param  {Function} callback
 * @return {Object}
 */
export function wrap(callback) {
  return ({ dispatch, getState }) =>
    new Promise((resolve, reject) => {
      if (!isFunction(callback)) {
        reject(INVALID_FUNCTION);
      } else {
        resolve(callback(dispatch, getState));
      }
    });
}
 
/**
 * Redux ready wrapper middleware.
 * Need to be imported as part of application's middlewares for usage.
 *
 * @return {Function|Object}
 */
export default () => store => next => (action) => {
  if (typeof action === 'function') {
    return action(store);
  }
 
  return next(action);
};