redux-enqueue
Simple queue system for redux. Use with redux-thunk.
npm install --save redux-enqueue
Create Store
Create a redux store using the enqueue middleware.
// createStore.js;;; const middlewares = ; return ;
Integrate into Actions
Call await dispatch(enqueue(queueId))
, which will return a function that you must call when the action is finished. This will ensure that actions sharing a queueId
s will not be run concurrently, but actions with other queueId
s may be.
Note that we're using try/finally. This is recommended: the code in the finally block is guaranteed to be run.
// authentication.js; const login = async { const completionHandler = await ; // arbitrary id try api; finally ; }; const logout = async { const completionHandler = await ; // don't log out whilst logging in try api; finally ; };
Cookbook
Fetching data
The below example will load messages for an id unless they've already been loaded. Fetching messages for different ids can happen concurrently. Fetching for the same id will not be able to happen in parallel, and no subsequent calls for this id will be executed after the first successful run.
// messages.js; const loadMessages = async { const completionHandler = await ; try const currentMessages = messages; if currentMessagesid return; const messages = await api; ; finally ; };