项目作者: wonism

项目描述 :
Redux middleware for flushing frequent actions. It optimizes the redux based application via reducing re-rendering caused of changed state.
高级语言: JavaScript
项目地址: git://github.com/wonism/redux-flush.git
创建时间: 2018-10-14T18:39:09Z
项目社区:https://github.com/wonism/redux-flush

开源协议:

下载


Redux Flush

Redux middleware for flushing frequent actions.
It optimizes the redux based application via reducing re-rendering caused of changed state.

npm version
Build Status

Installation

  1. $ npm i -S redux-flush

Demo

  1. $ npm run dev
  2. # and visit localhost:7777

Usage

⚠ Caution

Basically, The action with meta.flush = true will have array-like payload.
So, when you write reducers, please be CAREFUL.

If you want to pass just action payload, you can add omitKey. And it MUST be array.

Example

Example Image

Example with codes

  1. import { combineReducers, createStore, applyMiddleware, compose } from 'redux';
  2. import { composeWithDevTools } from 'redux-devtools-extension';
  3. import createFlush from 'redux-flush/es';
  4. // or
  5. // import createFlush from 'redux-flush';
  6. const reducers = combineReducers({
  7. app: (state = {}, { type, num, rand }) => {
  8. // payload will be delivered as a stream
  9. if (type === 'CLICK_EVENT') {
  10. return {
  11. ...state,
  12. num,
  13. rand: [...state.rand, ...rand],
  14. };
  15. }
  16. return state;
  17. },
  18. });
  19. const isProduction = process.env.NODE_ENV === 'production';
  20. const flushMiddleware = createFlush();
  21. const middleware = applyMiddleware(flushMiddleware);
  22. const composeEnhancers = !isProduction ? global.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__ || composeWithDevTools : compose;
  23. const store = createStore(reducers, { app: { num: -1, rand: [], } }, composeEnhancers(middleware));
  24. store.subscribe(() => {
  25. const state = store.getState();
  26. const { num, rand } = state.app;
  27. document.getElementById('number').textContent = num;
  28. document.getElementById('random').textContent = rand.join(', ');
  29. });
  30. {
  31. let num = 0;
  32. document.querySelector('button').addEventListener('click', () => {
  33. store.dispatch({
  34. type: 'CLICK_EVENT',
  35. num,
  36. rand: Math.floor(Math.random() * 10) + 1,
  37. meta: {
  38. flush: true,
  39. interval: 1000,
  40. omitKey: ['num'],
  41. },
  42. });
  43. num += 1;
  44. });
  45. }