@augmentcode/ag-redux-toolkit
Version:
Redux + Saga state management for Svelte apps — designed for AI agents. Includes runtime TypeScript source, AI skills, and reference documentation for robust, observable, high-performance state management.
30 lines (29 loc) • 946 B
JavaScript
import { getStoreContext } from "../utils/runtime-svelte/utils.js";
import { deriveSagaName } from "../utils/sagas/derive-saga-name.js";
import { startSaga, stopSaga } from "../slices/saga-manager/saga-manager-slice.js";
import { onDestroy, onMount } from "svelte";
//#region src/components-svelte/use-run-saga.ts
/**
* Start a saga when the component mounts and stop it when it unmounts.
* Call this at component init time to start a saga with automatic cleanup.
*
* @param saga - The saga function to run
*/
function useRunSaga(saga) {
const storeContext = getStoreContext();
if (!storeContext) return;
let stopSaga$1;
onMount(() => {
const sagaName = deriveSagaName(saga);
storeContext.store.dispatch(startSaga(sagaName, saga));
stopSaga$1 = () => {
storeContext.store.dispatch(stopSaga(sagaName));
};
});
onDestroy(() => {
stopSaga$1?.();
});
}
//#endregion
export { useRunSaga };
//# sourceMappingURL=use-run-saga.js.map