@channel-state/svelte
Version:
Svelte stores for channel-state, providing seamless integration with Svelte applications for cross-context state management.
58 lines (55 loc) • 2.21 kB
text/typescript
import * as svelte_store from 'svelte/store';
import { ChannelStore, StoreStatus } from '@channel-state/core';
/**
* @module @channel-state/svelte
* @description Provides a Svelte store for integrating with ChannelStore.
*/
/**
* A Svelte store that provides access to a ChannelStore's state.
* The returned store is both readable and writable. Updates to the store
* will update the underlying ChannelStore, and changes in the ChannelStore
* (from other tabs/windows or local updates) will update the Svelte store.
* @template T The type of the state managed by the ChannelStore.
* @param channelStore The ChannelStore instance to connect to.
* @returns A Svelte `Writable` store that represents the current state of the ChannelStore.
* @example
* ```svelte
* <script lang="ts">
* import { useChannelState } from '@channel-state/svelte';
* import { ChannelStore } from '@channel-state/core';
*
* const countStore = new ChannelStore<number>({ name: 'count', initial: 0 });
* const count = useChannelState(countStore);
*
* function increment() {
* $count++;
* }
* </script>
*
* <button on:click={increment}>Count: {$count}</button>
* ```
*/
declare function useChannelState<T>(channelStore: ChannelStore<T>): {
subscribe: (this: void, run: svelte_store.Subscriber<T>, invalidate?: () => void) => svelte_store.Unsubscriber;
set: (value: T) => void;
update: (this: void, updater: svelte_store.Updater<T>) => void;
};
/**
* A Svelte store that provides access to a ChannelStore's status.
* @param channelStore The ChannelStore instance to connect to.
* @returns A Svelte `Readable` store that represents the current status of the ChannelStore.
* @example
* ```svelte
* <script lang="ts">
* import { useChannelStatus } from '@channel-state/svelte';
* import { ChannelStore } from '@channel-state/core';
*
* const countStore = new ChannelStore<number>({ name: 'count', initial: 0 });
* const status = useChannelStatus(countStore);
* </script>
*
* <p>Status: {$status}</p>
* ```
*/
declare function useChannelStatus(channelStore: ChannelStore<unknown>): svelte_store.Readable<StoreStatus>;
export { useChannelState, useChannelStatus };