UNPKG

sveltekit-sync

Version:
58 lines (57 loc) 2.11 kB
/** * usePresence Hook * * Svelte 5 runes-compatible hook for simplified presence/awareness usage. * Provides reactive state, automatic tracking, and event handling. */ import { type User, type PresenceState, type CursorPosition, type Selection, type EditingState } from '../presence.svelte.js'; import type { SyncChannel } from '../channel.svelte.js'; export interface UsePresenceOptions<T = any> { customState?: T; idleTimeout?: number; trackCursor?: boolean; trackSelection?: boolean; } export interface UsePresenceReturn<T> { readonly others: PresenceState<T>[]; readonly othersCount: number; readonly onlineCount: number; readonly myPresence: PresenceState<T>; updateCursor(position: CursorPosition): void; updateSelection(selection: Selection | null): void; updateEditing(editing: EditingState | null): void; updateCustom(custom: Partial<T>): void; setStatus(status: 'online' | 'idle' | 'away'): void; getUser(userId: string): PresenceState<T> | null; getUsersEditing(resourceId: string): PresenceState<T>[]; follow(userId: string): () => void; isFollowing(userId: string): boolean; on(event: 'join' | 'leave' | 'update', handler: (state: PresenceState<T>) => void): () => void; destroy(): void; } /** * Create a presence hook for a channel * * @param channel - The SyncChannel to use for presence * @param user - Current user information * @param options - Configuration options * @returns Presence API with reactive state and actions * * @example * ```typescript * const presence = usePresence(channel, currentUser, { * customState: { editing: 'doc-123' }, * trackCursor: true, * idleTimeout: 300000 // 5 minutes * }); * * // Access reactive state * const collaborators = presence.others; * const count = presence.onlineCount; * * // Update presence * presence.updateCursor({ x: 100, y: 200 }); * presence.updateCustom({ editing: 'section-2' }); * ``` */ export declare function usePresence<T = any>(channel: SyncChannel, user: User, options?: UsePresenceOptions<T>): UsePresenceReturn<T>;