{"id":18728,"library":"redux-middleware-oneshot","title":"redux-middleware-oneshot","description":"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.","status":"active","version":"0.1.1","language":"javascript","source_language":"en","source_url":"https://github.com/michaelcontento/redux-middleware-oneshot","tags":["javascript","redux","redux-middleware","middleware"],"install":[{"cmd":"npm install redux-middleware-oneshot","lang":"bash","label":"npm"},{"cmd":"yarn add redux-middleware-oneshot","lang":"bash","label":"yarn"},{"cmd":"pnpm add redux-middleware-oneshot","lang":"bash","label":"pnpm"}],"dependencies":[{"reason":"peer dependency for creating middleware, dispatching actions, and composing store enhancers","package":"redux","optional":false}],"imports":[{"note":"Package provides a default export and is ESM-compatible, though CJS require() may work depending on bundler.","wrong":"const createOneShot = require('redux-middleware-oneshot');","symbol":"default","correct":"import createOneShot from 'redux-middleware-oneshot';"},{"note":"The function is exported as default, not as a named export. Using named import will result in undefined.","wrong":"import { createOneShot } from 'redux-middleware-oneshot';","symbol":"createOneShot","correct":"import createOneShot from 'redux-middleware-oneshot';"},{"note":"Standard Redux middleware composition. Some developers mistakenly call createOneShot inside applyMiddleware inline incorrectly.","wrong":"const store = applyMiddleware(createOneShot(...))(createStore)(reducer);","symbol":"applyMiddleware usage","correct":"import { applyMiddleware, createStore } from 'redux';\nconst middleware = createOneShot(dispatch => { ... });\nconst store = createStore(reducer, applyMiddleware(middleware));"}],"quickstart":{"code":"import { createStore, applyMiddleware } from 'redux';\nimport createOneShot from 'redux-middleware-oneshot';\n\n// Example reducer\nconst reducer = (state = [], action) => {\n  if (action.type === 'ADD') {\n    return [...state, action.payload];\n  }\n  return state;\n};\n\n// Create middleware that dispatches an action from a simulated event emitter\nconst emitter = {\n  listeners: [],\n  on(event, cb) { this.listeners.push(cb); },\n  emit(event, data) { this.listeners.forEach(l => l(data)); }\n};\n\nconst myMiddleware = createOneShot((dispatch) => {\n  // This runs exactly once when first action goes through\n  emitter.on('data', (value) => {\n    dispatch({ type: 'ADD', payload: value });\n  });\n});\n\nconst store = createStore(reducer, applyMiddleware(myMiddleware));\n\n// Trigger first action to initialize\nstore.dispatch({ type: '__INIT__' });\n\n// Later, emit an event - it will be dispatched\nemitter.emit('data', 42);\nconsole.log(store.getState()); // [42]","lang":"javascript","description":"Demonstrates creating a one-shot middleware that listens to a custom event emitter and dispatches Redux actions from it."},"warnings":[{"fix":"Ensure at least one action is dispatched (e.g., an init action) to trigger the middleware initialization.","message":"The setup function only runs once, but only after the first action is dispatched. If no action is ever dispatched, the setup never initializes.","severity":"gotcha","affected_versions":">=0.0.0"},{"fix":"Consider writing your own tests for the middleware behavior.","message":"No tests exist in the package (as per README). Use with caution in production.","severity":"gotcha","affected_versions":"=0.1.1"},{"fix":"Evaluate alternatives or contribute to the package if needed.","message":"The package has low download counts and no recent updates. It may be considered unmaintained in terms of active development.","severity":"gotcha","affected_versions":">=0.0.0"}],"env_vars":null,"last_verified":"2026-04-25T00:00:00.000Z","next_check":"2026-07-24T00:00:00.000Z","problems":[{"fix":"Use default import: import createOneShot from 'redux-middleware-oneshot'","cause":"Using named import instead of default import: import { createOneShot } from 'redux-middleware-oneshot'","error":"TypeError: createOneShot is not a function"},{"fix":"Use the dispatch parameter provided to the callback: createOneShot(dispatch => { ... })","cause":"Forgetting that the setup function receives dispatch as an argument and trying to use dispatch from outer scope","error":"Uncaught ReferenceError: dispatch is not defined"},{"fix":"Dispatch an initial dummy action to trigger the one-shot initialization: store.dispatch({ type: 'INIT' })","cause":"The setup function never runs because no initial action has been dispatched through the store","error":"The middleware does not dispatch any actions"}],"ecosystem":"npm","meta_description":null,"install_score":null,"install_tag":null,"quickstart_score":null,"quickstart_tag":null}