{"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.","language":"javascript","status":"active","last_verified":"Sat Apr 25","install":{"commands":["npm install redux-middleware-oneshot"],"cli":null},"imports":["import createOneShot from 'redux-middleware-oneshot';","import createOneShot from 'redux-middleware-oneshot';","import { applyMiddleware, createStore } from 'redux';\nconst middleware = createOneShot(dispatch => { ... });\nconst store = createStore(reducer, applyMiddleware(middleware));"],"auth":{"required":false,"env_vars":[]},"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.","tag":null,"tag_description":null,"last_tested":null,"results":[]},"compatibility":null}