redux-effect-ts
v0.1.0
Published
Redux middleware for dispatching Effect programs
Readme
redux-effect-ts
Dispatch Effect programs through Redux. Plain actions continue through the
middleware synchronously; Effects run on the supplied ManagedRuntime and
return a Promise. The caller owns and disposes that runtime.
Requires Effect 4.1.2 or newer in the 4.x series and Redux 5.
import { configureStore, createSlice } from '@reduxjs/toolkit';
import { Context, Effect, Layer, ManagedRuntime } from 'effect';
import { createEffectMiddleware, type EffectStore } from 'redux-effect-ts';
type State = { readonly value: number };
class Store extends Context.Service<Store, EffectStore<State, Store>>()('example/Store') {}
const counter = createSlice({
name: 'counter',
initialState: { value: 0 },
reducers: {
increment: (state) => { state.value += 1; },
},
});
const runtime = ManagedRuntime.make(Layer.empty);
const store = configureStore({
reducer: counter.reducer,
middleware: (defaults) =>
defaults().prepend(createEffectMiddleware({ runtime, store: Store })),
});
const increment = Effect.gen(function*() {
const redux = yield* Store;
yield* redux.dispatch(counter.actions.increment());
return (yield* redux.getState).value;
});
const value = await store.dispatch(increment); // 1
await runtime.dispose();Prepend the middleware before Redux Toolkit's default serializability check so the Effect itself is not treated as an ordinary action.
EffectStore provides a lazy getState Effect and a dispatch method. Nested
dispatch returns an Effect rather than a Promise: it preserves the caller's
dependencies, error channel, scope, and interruption. Dispatching a plain action
inside an Effect is also lazy. Outer dispatch rejects its Promise on unhandled
failures; the middleware does not create error actions.
