src/actionFn.js
/**
   * Fetch data from server
   * @param  {Object}   pathvars    path vars for url
   * @param  {Object}   params      fetch params
   * @param  {Function} callback)   callback execute after end request
   */
Fetch data from server
@param  {Object}   pathvars    path vars for url
@param  {Object}   params      fetch params
@param  {Function} callback)   callback execute after end request
Fetch data from server @param { Object } pathvars path vars for url @param { Object } params fetch params @param { Function } callback ) callback execute after end request
function fn(...args) {
    const [pathvars, params, callback] = extractArgs(args);
    const syncing = params ? !!params.syncing : false;
    params && delete params.syncing;
    pubsub.push(callback);
    return (...middlewareArgs) => {
      const middlewareParser =
        get(meta, "holder", "middlewareParser") || defaultMiddlewareArgsParser;
      const { dispatch, getState } = middlewareParser(...middlewareArgs);
      const state = getState();
      const isLoading = get(state, meta.prefix, meta.reducerName, "loading");
      if (isLoading) {
        return Promise.reject("isLoading");
      }
      const requestOptions = { pathvars, params };
      const prevData = get(state, meta.prefix, meta.reducerName, "data");
      dispatch({ type: actionFetch, syncing, request: requestOptions });
      const fetchResolverOpts = {
        dispatch,
        getState,
        request: requestOptions,
        actions: meta.actions,
        prefetch: meta.prefetch
      };
      if (Object.defineProperty) {
        Object.defineProperty(fetchResolverOpts, "requestOptions", {
          get() {
            console.warn("Deprecated option, use `request` option");
            return requestOptions;
          }
        });
      } else {
        fetchResolverOpts.requestOptions = requestOptions;
      }
      const result = new Promise((done, fail) => {
        fetchResolver(0, fetchResolverOpts, err => {
          if (err) {
            pubsub.reject(err);
            return fail(err);
          }
          new Promise((resolve, reject) => {
            requestHolder.set({
              resolve,
              reject,
              promise: request(pathvars, params, {}, getState, dispatch).then(
                resolve,
                reject
              )
            });
          }).then(
            d => {
              requestHolder.pop();
              const data = meta.transformer(d, prevData, {
                type: actionSuccess,
                request: requestOptions
              });
              dispatch({
                data,
                origData: d,
                type: actionSuccess,
                syncing: false,
                request: requestOptions
              });
              if (meta.broadcast) {
                meta.broadcast.forEach(type => {
                  dispatch({
                    type,
                    data,
                    origData: d,
                    request: requestOptions
                  });
                });
              }
              if (meta.postfetch) {
                meta.postfetch.forEach(postfetch => {
                  postfetch instanceof Function &&
                    postfetch({
                      data,
                      getState,
                      dispatch,
                      actions: meta.actions,
                      request: requestOptions
                    });
                });
              }
              pubsub.resolve(data);
              done(data);
            },
            error => {
              dispatch({
                error,
                type: actionFail,
                loading: false,
                syncing: false,
                request: requestOptions
              });
              pubsub.reject(error);
              fail(error);
            }
          );
        });
      });
      result.catch(none);
      return result;
    };
  }
function fn ( ... args ) { const [ pathvars , params , callback ] = extractArgs ( args ) ; const syncing = params ? ! ! params . syncing : false ; params && delete params . syncing ; pubsub . push ( callback ) ; return ( ... middlewareArgs ) => { const middlewareParser = get ( meta , "holder" , "middlewareParser" ) || defaultMiddlewareArgsParser ; const { dispatch , getState } = middlewareParser ( ... middlewareArgs ) ; const state = getState ( ) ; const isLoading = get ( state , meta . prefix , meta . reducerName , "loading" ) ; if ( isLoading ) { return Promise . reject ( "isLoading" ) ; } const requestOptions = { pathvars , params } ; const prevData = get ( state , meta . prefix , meta . reducerName , "data" ) ; dispatch ( { type : actionFetch , syncing , request : requestOptions } ) ; const fetchResolverOpts = { dispatch , getState , request : requestOptions , actions : meta . actions , prefetch : meta . prefetch } ; if ( Object . defineProperty ) { Object . defineProperty ( fetchResolverOpts , "requestOptions" , { get ( ) { console . warn ( "Deprecated option, use `request` option" ) ; return requestOptions ; } } ) ; } else { fetchResolverOpts . requestOptions = requestOptions ; } const result = new Promise ( ( done , fail ) => { fetchResolver ( 0 , fetchResolverOpts , err => { if ( err ) { pubsub . reject ( err ) ; return fail ( err ) ; } new Promise ( ( resolve , reject ) => { requestHolder . set ( { resolve , reject , promise : request ( pathvars , params , { } , getState , dispatch ) . then ( resolve , reject ) } ) ; } ) . then ( d => { requestHolder . pop ( ) ; const data = meta . transformer ( d , prevData , { type : actionSuccess , request : requestOptions } ) ; dispatch ( { data , origData : d , type : actionSuccess , syncing : false , request : requestOptions } ) ; if ( meta . broadcast ) { meta . broadcast . forEach ( type => { dispatch ( { type , data , origData : d , request : requestOptions } ) ; } ) ; } if ( meta . postfetch ) { meta . postfetch . forEach ( postfetch => { postfetch instanceof Function && postfetch ( { data , getState , dispatch , actions : meta . actions , request : requestOptions } ) ; } ) ; } pubsub . resolve ( data ) ; done ( data ) ; } , error => { dispatch ( { error , type : actionFail , loading : false , syncing : false , request : requestOptions } ) ; pubsub . reject ( error ) ; fail ( error ) ; } ) ; } ) ; } ) ; result . catch ( none ) ; return result ; } ; }
Fetch data from server
 @param  {Object}   pathvars    path vars for url
 @param  {Object}   params      fetch params
 @param  {Function} callback)   callback execute after end request
Fetch data from server @param { Object } pathvars path vars for url @param { Object } params fetch params @param { Function } callback ) callback execute after end request
/* eslint no-console: 0 */