UNPKG

@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.

33 lines (32 loc) • 1.59 kB
import { STORE_CONTEXT } from "../store/store-runtime-constants.js"; import { getContext } from "svelte"; //#region src/utils/runtime-svelte/utils.ts var LIFECYCLE_OUTSIDE_COMPONENT_ERROR = "lifecycle_outside_component"; var STORE_CONTEXT_OUTSIDE_COMPONENT_MESSAGE = "Store context accessed outside component initialization. Store context helpers can only be called during component init (top-level <script> block)."; var isLifecycleOutsideComponentError = (error) => { if (!(error instanceof Error)) return false; if (error.message.includes(LIFECYCLE_OUTSIDE_COMPONENT_ERROR)) return true; return error.cause instanceof Error && isLifecycleOutsideComponentError(error.cause); }; var getStoreContext = () => { try { return getContext(STORE_CONTEXT); } catch (error) { if (isLifecycleOutsideComponentError(error)) throw new Error(STORE_CONTEXT_OUTSIDE_COMPONENT_MESSAGE, { cause: error }); throw error; } }; function getDispatch() { let context; try { context = getStoreContext(); } catch (error) { if (isLifecycleOutsideComponentError(error)) throw new Error("getDispatch() called outside component initialization. Call getDispatch() during component init (top-level <script> block) and store the returned dispatch function for event handlers, callbacks, or async functions.", { cause: error }); throw error; } if (!context?.store) throw new Error("Missing redux store context. Wrap root component into <Store/>"); return context.store.dispatch; } //#endregion export { getDispatch, getStoreContext, isLifecycleOutsideComponentError }; //# sourceMappingURL=utils.js.map