redux-middleware-oneshot
raw JSON → 0.1.1 verified Sat Apr 25 auth: no javascript
Creates a Redux middleware that invokes a setup function exactly once, when the first action passes through the middleware chain, allowing you to dispatch actions from arbitrary event sources (e.g., event emitters, WebSocket listeners) in a clean and timely manner. Current version 0.1.1 is early stage with limited releases and no tests. Redux peer dependency supports versions 1.x–3.x. Differentiates from other middleware patterns by ensuring one-time initialization tied to the first Redux action.
Common errors
error TypeError: createOneShot is not a function ↓
cause Using named import instead of default import: import { createOneShot } from 'redux-middleware-oneshot'
fix
Use default import: import createOneShot from 'redux-middleware-oneshot'
error Uncaught ReferenceError: dispatch is not defined ↓
cause Forgetting that the setup function receives dispatch as an argument and trying to use dispatch from outer scope
fix
Use the dispatch parameter provided to the callback: createOneShot(dispatch => { ... })
error The middleware does not dispatch any actions ↓
cause The setup function never runs because no initial action has been dispatched through the store
fix
Dispatch an initial dummy action to trigger the one-shot initialization: store.dispatch({ type: 'INIT' })
Warnings
gotcha The setup function only runs once, but only after the first action is dispatched. If no action is ever dispatched, the setup never initializes. ↓
fix Ensure at least one action is dispatched (e.g., an init action) to trigger the middleware initialization.
gotcha No tests exist in the package (as per README). Use with caution in production. ↓
fix Consider writing your own tests for the middleware behavior.
gotcha The package has low download counts and no recent updates. It may be considered unmaintained in terms of active development. ↓
fix Evaluate alternatives or contribute to the package if needed.
Install
npm install redux-middleware-oneshot yarn add redux-middleware-oneshot pnpm add redux-middleware-oneshot Imports
- default wrong
const createOneShot = require('redux-middleware-oneshot');correctimport createOneShot from 'redux-middleware-oneshot'; - createOneShot wrong
import { createOneShot } from 'redux-middleware-oneshot';correctimport createOneShot from 'redux-middleware-oneshot'; - applyMiddleware usage wrong
const store = applyMiddleware(createOneShot(...))(createStore)(reducer);correctimport { applyMiddleware, createStore } from 'redux'; const middleware = createOneShot(dispatch => { ... }); const store = createStore(reducer, applyMiddleware(middleware));
Quickstart
import { createStore, applyMiddleware } from 'redux';
import createOneShot from 'redux-middleware-oneshot';
// Example reducer
const reducer = (state = [], action) => {
if (action.type === 'ADD') {
return [...state, action.payload];
}
return state;
};
// Create middleware that dispatches an action from a simulated event emitter
const emitter = {
listeners: [],
on(event, cb) { this.listeners.push(cb); },
emit(event, data) { this.listeners.forEach(l => l(data)); }
};
const myMiddleware = createOneShot((dispatch) => {
// This runs exactly once when first action goes through
emitter.on('data', (value) => {
dispatch({ type: 'ADD', payload: value });
});
});
const store = createStore(reducer, applyMiddleware(myMiddleware));
// Trigger first action to initialize
store.dispatch({ type: '__INIT__' });
// Later, emit an event - it will be dispatched
emitter.emit('data', 42);
console.log(store.getState()); // [42]