redux-code
Version:
Redux helpers for actions and reducers
47 lines (46 loc) • 1.17 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.middleware = exports.clear = exports.push = exports.queue = void 0;
let DISPATCH = undefined;
let DISPATCHING = false;
exports.queue = [];
/**
*
* Dispatch an action to the store.
* @param {action} action The action to dispatch.
* @returns {action} The action.
*/
function push(action) {
if (DISPATCHING || !DISPATCH)
return exports.queue.push(action);
return DISPATCH(action);
}
exports.push = push;
/**
*
* Clear the queue.
*/
const clear = () => exports.queue.splice(0, exports.queue.length);
exports.clear = clear;
/**
*
* Create middleware that dispatches actions from the queue.
*/
function middleware({ dispatch }) {
DISPATCH = dispatch;
return (next) => (action) => {
try {
DISPATCHING = true;
return next(action);
}
finally {
DISPATCHING = false;
if (exports.queue.length > 0) {
const action = exports.queue.shift();
if (action)
dispatch(action);
}
}
};
}
exports.middleware = middleware;