Counter: {interval.counter}
Interval delay: {delay}ms
Status: {interval.isActive ? "Running" : "Paused"}
``` -------------------------------- ### Resource with Multiple Dependencies (Svelte) Source: https://github.com/svecosystem/runed/blob/main/sites/docs/src/content/utilities/resource.md Illustrates how to use the 'resource' utility with multiple reactive dependencies. The fetcher function receives an array of the current values of these dependencies. This is useful for scenarios where data fetching depends on several reactive sources. ```svelte ``` -------------------------------- ### Pre-render Execution with Resource.pre (Svelte) Source: https://github.com/svecosystem/runed/blob/main/sites/docs/src/content/utilities/resource.md Demonstrates the use of `resource.pre()` for executing data fetching during the pre-rendering phase. This is beneficial for SSR (Server-Side Rendering) scenarios where initial data needs to be fetched before the client-side hydration. ```svelte ``` -------------------------------- ### Context Class Type Definition (TypeScript) Source: https://github.com/svecosystem/runed/blob/main/sites/docs/src/content/utilities/context.md Provides the TypeScript type definition for the `Context` class. It outlines the constructor and methods available for managing context, including `key`, `exists`, `get`, `getOr`, and `set`. ```typescript class ContextYou searched for: {throttledSearch}
Count: {count.current}
Previous: {`${previous.current}`}
Target node
Target node in viewport: {inViewport.current}
``` -------------------------------- ### Perform one-time detection with IsInViewport Source: https://github.com/svecosystem/runed/blob/main/sites/docs/src/content/utilities/is-in-viewport.md Shows how to use the 'once' option to stop observing an element after it has been seen for the first time. This is useful for triggering one-time animations or lazy loading. ```svelteWidth: {size.width} Height: {size.height}
``` -------------------------------- ### Implement Finite State Machines Source: https://context7.com/svecosystem/runed/llms.txt FiniteStateMachine provides a strongly-typed way to manage application states, transitions, and side effects. It supports lifecycle hooks like _enter and _exit, as well as debounced events. ```typescript import { FiniteStateMachine } from "runed"; type States = "idle" | "loading" | "success" | "error"; type Events = "fetch" | "resolve" | "reject" | "reset"; const fsm = new FiniteStateMachineCurrently active element: {activeElement.current?.localName ?? "No active element found"}
``` -------------------------------- ### Attach Event Listener with useEventListener Source: https://github.com/svecosystem/runed/blob/main/sites/docs/src/content/utilities/use-event-listener.md Demonstrates how to use the useEventListener hook to track document clicks within a class-based structure and a Svelte component. It ensures memory safety by automatically removing listeners upon component destruction. ```typescript import { useEventListener } from "runed"; export class ClickLogger { #clicks = $state(0); constructor() { useEventListener( () => document.body, "click", () => this.#clicks++ ); } get clicks() { return this.#clicks; } } ``` ```svelteYou've clicked the document {logger.clicks} {logger.clicks === 1 ? "time" : "times"}
``` -------------------------------- ### Implement AnimationFrames with FPS control in Svelte Source: https://github.com/svecosystem/runed/blob/main/sites/docs/src/content/utilities/animation-frames.md This snippet demonstrates how to initialize an AnimationFrames instance to track frame counts and delta time. It utilizes Svelte 5 runes to bind the animation state and FPS limits to UI components. ```svelte{stats}
FPS limit: {fpsLimit}{fpsLimit === 0 ? " (not limited)" : ""}
Focus within form: {focusWithinForm.current}
``` -------------------------------- ### Controlling PersistedState Connection Source: https://github.com/svecosystem/runed/blob/main/sites/docs/src/content/utilities/persisted-state.md Demonstrates manual control over the connection state of PersistedState using the `connected` option and `.connect()`/`.disconnect()` methods. This allows for managing when state is persisted to and read from storage. Dependencies include the 'runed' library. ```ts // Start disconnected from storage const state = new PersistedState("temp-data", initialValue, { connected: false }); // State changes are kept in memory only state.current = "new value"; // Connect to storage when ready state.connect(); // Now persists to storage // Check connection status console.log(state.connected); // true // Disconnect from storage state.disconnect(); // Removes from storage, keeps value in memory ``` -------------------------------- ### Define a Complex Finite State Machine with Actions and Lifecycle Methods Source: https://github.com/svecosystem/runed/blob/main/sites/docs/src/content/utilities/finite-state-machine.md Demonstrates the creation of a finite state machine with multiple states, events, conditional transitions using actions, and lifecycle methods for state entry and exit. It includes a debounce mechanism for event handling. ```typescript import { FiniteStateMachine } from "runed"; type MyStates = "disabled" | "idle" | "running"; type MyEvents = "toggleEnabled" | "start" | "stop"; const f = new FiniteStateMachineDocument visible: {visible.current ? "Yes" : "No"}
``` -------------------------------- ### Apply Date Formats Using `dateFormats` Option Source: https://github.com/svecosystem/runed/blob/main/sites/docs/src/content/utilities/use-search-params.md Shows an alternative method for defining date formats using the `dateFormats` option, which can be used with various validation libraries including createSearchParamsSchema. This approach centralizes date format configuration. ```typescript // Works with Zod, Valibot, Arktype, or createSearchParamsSchema const params = useSearchParams(zodSchema, { dateFormats: { birthDate: "date", // YYYY-MM-DD createdAt: "datetime" // ISO8601 } }); ``` -------------------------------- ### Persisting Complex Objects with PersistedState Source: https://github.com/svecosystem/runed/blob/main/sites/docs/src/content/utilities/persisted-state.md Illustrates how PersistedState handles different types of data, including arrays, plain objects, and custom class instances. It highlights that only plain structures are deeply reactive for automatic persistence. Dependencies include the 'runed' library. ```ts const persistedArray = new PersistedState("foo", ["a", "b"]); persistedArray.current.push("c"); // This will persist the change const persistedObject = new PersistedState("bar", { name: "Bob" }); persistedObject.current.name = "JG"; // This will persist the change class Person { name: string; constructor(name: string) { this.name = name; } } const persistedComplexObject = new PersistedState("baz", new Person("Bob")); persistedComplexObject.current.name = "JG"; // This will NOT persist the change persistedComplexObject.current = new Person("JG"); // This will persist the change ```