Saga middleware for Redux to handle side effects using ES6 generators
—
Redux-Saga middleware creation and configuration for connecting sagas to your Redux store.
Creates Redux middleware and connects sagas to the Redux Store. The middleware provides methods to run sagas and manage saga context.
/**
* Creates Redux middleware and connects sagas to the Redux Store
* @param options - Configuration options for the middleware
* @returns SagaMiddleware instance with run() and setContext() methods
*/
function createSagaMiddleware<C extends object>(
options?: SagaMiddlewareOptions<C>
): SagaMiddleware<C>;
interface SagaMiddlewareOptions<C extends object = {}> {
/** Initial value of the saga's context */
context?: C;
/** Monitor for receiving saga execution events */
sagaMonitor?: SagaMonitor;
/** Error handler for uncaught errors from sagas */
onError?(error: Error, errorInfo: ErrorInfo): void;
/** Array of effect middlewares for intercepting effects */
effectMiddlewares?: EffectMiddleware[];
/** Custom channel for take and put effects */
channel?: MulticastChannel<Action>;
}
interface SagaMiddleware<C extends object = {}> extends Middleware {
/** Dynamically run a saga after middleware setup */
run<S extends Saga>(saga: S, ...args: Parameters<S>): Task;
/** Update the saga context */
setContext(props: Partial<C>): void;
}Usage Examples:
import { createStore, applyMiddleware } from "redux";
import createSagaMiddleware from "redux-saga";
// Basic middleware creation
const sagaMiddleware = createSagaMiddleware();
// With options
const sagaMiddleware = createSagaMiddleware({
context: { api: apiClient },
onError: (error, errorInfo) => {
console.error("Saga error:", error);
console.error("Stack:", errorInfo.sagaStack);
},
sagaMonitor: myCustomMonitor
});
// Apply to store
const store = createStore(
reducer,
applyMiddleware(sagaMiddleware)
);
// Run sagas
const task = sagaMiddleware.run(rootSaga);Allows starting sagas outside the Redux middleware environment. Useful for connecting sagas to external input/output or testing.
/**
* Start a saga outside of Redux middleware environment
* @param options - Configuration for saga execution environment
* @param saga - Generator function to run
* @param args - Arguments to pass to the saga
* @returns Task object representing the running saga
*/
function runSaga<Action, State, S extends Saga>(
options: RunSagaOptions<Action, State>,
saga: S,
...args: Parameters<S>
): Task;
interface RunSagaOptions<A, S> {
/** Channel for take effects */
channel?: PredicateTakeableChannel<A>;
/** Function to fulfill put effects */
dispatch?(output: A): any;
/** Function to fulfill select effects */
getState?(): S;
/** Monitor for saga events */
sagaMonitor?: SagaMonitor;
/** Error handler */
onError?(error: Error, errorInfo: ErrorInfo): void;
/** Saga context */
context?: object;
/** Effect middlewares */
effectMiddlewares?: EffectMiddleware[];
}Usage Examples:
import { runSaga } from "redux-saga";
// Run saga with custom dispatch/getState
const task = runSaga(
{
dispatch: (action) => console.log("Action:", action),
getState: () => ({ user: { id: 1 } })
},
mySaga,
{ param: "value" }
);
// For testing
const dispatched = [];
runSaga(
{
dispatch: (action) => dispatched.push(action),
getState: () => mockState
},
sagaToTest
);Interface for monitoring saga execution events. Used for debugging, logging, and development tools.
interface SagaMonitor {
/** Called when a root saga starts */
rootSagaStarted?(options: { effectId: number; saga: Saga; args: any[] }): void;
/** Called when an effect is triggered */
effectTriggered?(options: {
effectId: number;
parentEffectId: number;
label?: string;
effect: any
}): void;
/** Called when an effect resolves successfully */
effectResolved?(effectId: number, result: any): void;
/** Called when an effect is rejected with error */
effectRejected?(effectId: number, error: any): void;
/** Called when an effect is cancelled */
effectCancelled?(effectId: number): void;
/** Called when a Redux action is dispatched */
actionDispatched?(action: Action): void;
}/** Symbol used for cancellation */
const CANCEL: string;
/** Special action type that terminates sagas */
const END: EndType;Install with Tessl CLI
npx tessl i tessl/npm-redux-saga