npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

redux-order

v1.0.4

Published

redux middleware asyn promise

Downloads

8

Readme

redux-order

redux-order是处理redux的中间件,简化redux的异步流控制处理。

安装

npm install redux-order

引入redux-order

import {createStore, applyMiddleware, compose} from 'redux';
import reduxOrder from 'redux-order';
import reducers from './reduces';

const enhancer = compose(
  //引入中间件
  applyMiddleware(reduxOrder())
);

const store = createStore(
  reducers,
  enhancer
);

export default store;

reduces中处理异步

// action
const LOGIN = 'auth/LOGIN';
const LOGIN_SUCCESS = 'auth/LOGIN_SUCCESS';
const LOGIN_FAIL = 'auth/LOGIN_FAIL';

const initialState = {};
// reducer
export default function reducer(state = initialState, action = {}) {
  switch (action.type) {
    case LOGIN:
      console.log(action);
      return {
        ...state,
        requesting: true,
        requested: false,
    };
    case LOGIN_SUCCESS:
      console.log(action);
      return {
        ...state,
        requesting: false,
        requested: true,
        auth: action.res
    };
    case LOGIN_FAIL:
      console.log(action);
      return {
        ...state,
        requesting: false,
        requested: true,
        loginError: action.err
    };
    default:
      return state;
  }
}
// 触发 action
export function login(user, pass) {
  return {
    types: [LOGIN, LOGIN_SUCCESS, LOGIN_FAIL],
    promise: axios.post('/api/login', {user, pass}),
    data: 'message',
    list: [1, 2, 3, 4]
  };
}

异步action

  • 在上面的例子里,我们创建了异步的redux请求。例子中定义了LOGINLOGIN_SUCCESSLOGIN_FAIL三个action,依次代表请求发出、请求成功、请求失败。在发出一个异步promise请求后,首先触发了LOGIN,假如请求成功则进入LOGIN_SUCCESS,否则就进入LOGIN_FAIL
  • 异步动作需要定义types,types为数组并且至少需要两个action(发出请求和结果),promise参数为异步请求,异步请求必须为promise对象。
  • 请求的结果在reducer中的action对象中获得,如果是成功的返回结果为action.req,失败的则是action.err
  • 在发出action时,还可以携带payload。可以自定义需要携带的参数,在reducer中即可访问action携带的参数。

同步action

export function logout() {
  return {
    type: LOGOUT
  };
}

上面为触发一个同步的action,type 参数定义要触发的动作,同样也可以携带payload。注意⚠️异步的action参数为types,而同步的为type