Optional
hooks: RedialMiddlewareHooksOptional hooks to run before and after the action is forwarded.
Middleware with a dispose
method for cleaning up any IPC event listeners.
import { createRedialRendererMiddleware } from "@laserware/redial/renderer";
import { configureStore, type Store } from "@reduxjs/toolkit";
import { rootReducer } from "../common/rootReducer";
export function createStore(): Store {
const redialMiddleware = createRedialRendererMiddleware({
// Ensure payload is serialized before sending:
beforeSend(action) {
// In Svelte, a `$state()` Rune turns an object into a Proxy, which is
// not serializable, so you need to convert it back to an object before
// sending it to the main process:
if (action.payload !== undefined) {
action.payload = JSON.parse(JSON.stringify(action.payload));
}
return action;
},
// You can use the afterSend hook to make changes to the action before
// it gets sent to the next middleware:
afterSend(action) {
action.meta.timestamp = Date.now();
return action;
}
});
let preloadedState;
// If using Vite:
if (import.meta.env.MODE === "development") {
preloadedState = redialMiddleware.getMainStateSync();
}
const store = configureStore({
preloadedState,
reducer: rootReducer,
middleware: (getDefaultMiddleware) =>
getDefaultMiddleware().concat(redialMiddleware),
});
}
Whenever an action is fired from the renderer process, forward it to the main process to ensure global state is in sync. The optional
hooks
argument allows you to make changes to the action prior to forwarding it to the main process and after forwarding to the main process before passing the action to the next middlewares.