Maintained by Kubric
yarn add @kubric/redux-knob
Typescript types will be also available soon.
import { createStore, combineReducers, applyMiddleware } from "redux"; import { ActionQueue, enableBatching, ENABLE_ACTION_QUEUE, FLUSH_ACTION_QUEUE } from "@kubric/redux-knob"; // ActionQueue with default options const actionQueue = new ActionQueue(); store = createStore( enableBatching( combineReducers({ data }) ), applyMiddleware(...[actionQueue.getWare()]) );
store.dispatch({ type: ENABLE_ACTION_QUEUE }); store.dispatch(actions[0]); store.dispatch(actions[1]); store.dispatch({ type: FLUSH_ACTION_QUEUE, payload: actions });
We will be taking help of the following reducer across the recipes.
const data = (state = defaultState, action) => { switch (action.type) { case "reset": return defaultState; case "π§": return { ...state, cheese: state.cheese + 1 }; case "π": return { ...state, pizza: state.pizza + 1 }; case "π₯¦": return { ...state, broccoli: state.broccoli + 1 }; case "π₯¬": return { ...state, leafygreens: state.leafygreens + 1 }; default: return state; } };
1. Batched Action
import { createStore, combineReducers, applyMiddleware } from "redux"; import { enableBatching } from "@kubric/redux-knob"; store = createStore( enableBatching( combineReducers({ data }), { batchType: "batchedType" } ), {} ); const actions = [{ type: "π₯¦" }, { type: "π₯¬" }, { type: "π₯¦" }, { type: "π₯¦" }]; store.dispatch({ type: "batchedType", payload: actions }); console.log(store.getState()); // { data: { broccoli: 3, leafygreens: 1, pizza: 0, cheese: 0 }}
2. ActionQueue (With Enable Flush Actions)
import { createStore, combineReducers, applyMiddleware } from "redux"; import { ActionQueue, enableBatching } from "@kubric/redux-knob"; const actionQueue = new ActionQueue({ enableType: "π", flushType: "π" }); store = createStore( enableBatching( combineReducers({ data // The reducer }), { batchType: BATCH_TYPE } ), applyMiddleware(...[actionQueue.getWare()]) ); store.dispatch({ type: "π₯¦" }); // Follows normal execution store.dispatch({ type: "π" }); // Enables the queue, starts queing store.dispatch({ type: "π₯¬" }); // Pushes to the queue store.dispatch({ type: "π₯¦" }); // Pushes to the queue store.dispatch({ type: "π" }); // Flushes the queue, dispatched a batched action store.dispatch({ type: "π₯¬" }); // Follows normal execution
3. ThrottleQueue
import { createStore, combineReducers, applyMiddleware } from "redux"; import { ThrottleQueue, enableBatching } from "@kubric/redux-knob"; const throttler = new ThrottleQueue({ filterTypes: [π§, π], filter: 'include', delay: 1000 }); store = createStore( enableBatching( combineReducers({ data // Reducer maintaining counts of food items }), { batchType: BATCH_TYPE } ), applyMiddleware(...[throttler.getWare()]) ); store.dispatch({ type: "π§" }); // This will get queued store.dispatch({ type: "π§" }); // This will get queued store.dispatch({ type: "π₯¦" }); store.dispatch({ type: "π₯¦" }); store.dispatch({ type: "π₯¬" }); store.dispatch({ type: "π" }); // This will get queued store.dispatch({ type: "π₯¬" }); console.log(store.getState()); // { data: { broccoli: 2, leafygreens: 2, pizza: 0, cheese: 0 }} // β° After 1000ms the first two actions will be batched & dispatched console.log(store.getState()); // { data: { broccoli: 2, leafygreens: 2, pizza: 1, cheese: 2 }}