API Reference
reactiveValue
Convert a StatePort to a Svelte Writable store.
Function Signature
function reactiveValue<T>(getPort: () => StatePort<T>): Writable<T>;
Type Parameters:
T- The type of the value
Parameters:
getPort: () => StatePort<T>- A function that returns the StatePort (required for reactive tracking of props)
Returns: A Svelte Writable<T> store
Behavior:
- Requires a function (not the port directly) to enable reactive tracking of props
- Returns a Svelte store that can be used with
$storesyntax - The store is reactive and updates when the StatePort value changes
Example:
<script>
import { reactiveValue } from '@xndrjs/adapter-svelte';
import { ReactiveValue } from '@xndrjs/core';
// For static ports
const count = new ReactiveValue(1);
const countStore = reactiveValue(() => count);
// For reactive props (no $derived needed!)
let { todoListManager } = $props();
const todosStore = reactiveValue(() => todoListManager.todos);
</script>
<div>{$countStore}</div>
<div>{$todosStore}</div>
toStatePort
Convert a Svelte $state rune to a StatePort.
Function Signature
function toStatePort<T>(
getState: () => T,
setState: (value: T) => void
): StatePort<T>;
Type Parameters:
T- The type of the value
Parameters:
getState: () => T- Function that returns the current state valuesetState: (value: T) => void- Function that sets the state value
Returns: A StatePort<T> instance
Behavior:
- Converts Svelte
$statevariables toStatePortfor use with xndr APIs - The returned
StatePortsupports both direct values and updater functions inset() - Subscriptions are handled using Svelte's reactivity system
Example:
<script>
import { toStatePort } from '@xndrjs/adapter-svelte';
import { createComputed } from '@xndrjs/core';
let text = $state("");
const textPort = toStatePort(() => text, (v) => text = v);
// Use the port with xndr APIs
const textLength = createComputed(textPort)
.as((t) => t.length)
.for({ [Symbol.dispose]() {} });
</script>
<input bind:value={text} />
<p>Length: {textLength.get()}</p>