{"library":"redux-logic","title":"Redux Logic Middleware","description":"Redux Logic is a middleware for Redux applications designed to centralize and organize business logic and side effects. It allows developers to intercept actions and perform asynchronous processing, supporting various JavaScript styles including callbacks, Promises, async/await, and Observables (via RxJS). The current stable version is 5.0.2. Release cadence typically involves minor and patch versions for dependency updates and security fixes, with major versions reserved for significant changes like dropping Node.js version support or altering internal event structures. Key differentiators include its declarative API for common side effect patterns (filtering, cancellation, latest request handling, debouncing, throttling), robust testing support via `redux-logic-test`, simplified server-side rendering, and the ability to dynamically load logic for code-splitting scenarios. It aims to provide a flexible alternative to libraries like Redux Saga and Redux Observable by allowing developers to choose their preferred async programming paradigm within a declarative framework.","language":"javascript","status":"active","last_verified":"Thu Apr 23","install":{"commands":["npm install redux-logic"],"cli":null},"imports":["import { createLogic } from 'redux-logic';","import { createLogicMiddleware } from 'redux-logic';","import type { Logic } from 'redux-logic';"],"auth":{"required":false,"env_vars":[]},"quickstart":{"code":"import { createStore, applyMiddleware } from 'redux';\nimport { createLogic, createLogicMiddleware } from 'redux-logic';\nimport axios from 'axios'; // Example dependency, install if needed: npm install axios\n\nconst FETCH_POLLS = 'FETCH_POLLS';\nconst FETCH_POLLS_SUCCESS = 'FETCH_POLLS_SUCCESS';\nconst FETCH_POLLS_FAILED = 'FETCH_POLLS_FAILED';\nconst CANCEL_FETCH_POLLS = 'CANCEL_FETCH_POLLS';\n\n// Reducer\nconst initialState = { polls: [], loading: false, error: null };\nfunction reducer(state = initialState, action) {\n  switch (action.type) {\n    case FETCH_POLLS: return { ...state, loading: true, error: null };\n    case FETCH_POLLS_SUCCESS: return { ...state, loading: false, polls: action.payload };\n    case FETCH_POLLS_FAILED: return { ...state, loading: false, error: action.payload };\n    default: return state;\n  }\n}\n\n// Logic definition\nconst fetchPollsLogic = createLogic({\n  type: FETCH_POLLS,\n  cancelType: CANCEL_FETCH_POLLS,\n  latest: true,\n\n  process({ getState, action }, dispatch, done) {\n    axios\n      .get('https://survey.codewinds.com/polls')\n      .then((resp) => resp.data.polls)\n      .then((polls) => dispatch({ type: FETCH_POLLS_SUCCESS, payload: polls }))\n      .catch((err) => {\n        console.error('Fetch polls error:', err); // Log error\n        dispatch({ type: FETCH_POLLS_FAILED, payload: err.message, error: true });\n      })\n      .then(() => done()); // Always call done when finished\n  }\n});\n\n// Configure middleware and store\nconst logicMiddleware = createLogicMiddleware([fetchPollsLogic]);\nconst store = createStore(reducer, applyMiddleware(logicMiddleware));\n\n// Dispatch an action to trigger the logic\nconsole.log('Dispatching FETCH_POLLS action...');\nstore.dispatch({ type: FETCH_POLLS });\n\n// Optional: Simulate cancellation after a delay\n// setTimeout(() => {\n//   console.log('Dispatching CANCEL_FETCH_POLLS action...');\n//   store.dispatch({ type: CANCEL_FETCH_POLLS });\n// }, 100);\n\n// Listen for state changes (for demonstration)\nconst unsubscribe = store.subscribe(() => {\n  console.log('Current state:', store.getState());\n  if (!store.getState().loading) {\n    unsubscribe(); // Stop listening once loading is complete\n  }\n});\n","lang":"typescript","description":"This quickstart demonstrates how to define and integrate a `redux-logic` unit that fetches data, handles success/failure, and supports cancellation and 'take latest' behavior within a Redux store.","tag":null,"tag_description":null,"last_tested":null,"results":[]},"compatibility":null}