sveltekit-sync
Version:
Local-first sync engine for SvelteKit
75 lines (74 loc) • 2.22 kB
TypeScript
import type { RealtimeClient } from '../realtime/client.js';
export interface User {
id: string;
name: string;
email?: string;
avatar?: string;
color?: string;
}
export interface CursorPosition {
x: number;
y: number;
line?: number;
column?: number;
}
export interface Selection {
start: CursorPosition;
end: CursorPosition;
text?: string;
}
export interface EditingState {
resourceId: string;
field?: string;
action?: 'typing' | 'selecting' | 'idle';
timestamp: number;
}
export interface PresenceState<T = any> {
user: User;
status: 'online' | 'idle' | 'away' | 'offline';
lastSeen: number;
cursor?: CursorPosition;
selection?: Selection;
editing?: EditingState;
custom?: T;
}
export declare class PresenceStore<T = any> {
private realtimeClient;
private tableName;
private myState;
private othersState;
private followingUserId;
private heartbeatInterval;
private idleTimer;
private eventListeners;
private cursorDebounceTimer;
private cursorDebounceMs;
constructor(realtimeClient: RealtimeClient | null, tableName: string, user: User, customState?: T);
private generateColor;
private setupPresenceSync;
private startHeartbeat;
private setupIdleDetection;
private resetIdle;
private resetIdleTimer;
private broadcastPresence;
on(event: string, handler: (data: any) => void): () => void;
private emit;
updatePresence(state: Partial<PresenceState<T>>): void;
updateCursor(position: CursorPosition): void;
updateSelection(selection: Selection | null): void;
updateEditing(editing: EditingState | null): void;
get myPresence(): PresenceState<T>;
get others(): PresenceState<T>[];
get othersCount(): number;
get onlineCount(): number;
getUser(userId: string): PresenceState<T> | null;
getOnlineUsers(): PresenceState<T>[];
getUsersEditing(resourceId: string): PresenceState<T>[];
follow(userId: string): () => void;
stopFollowing(): void;
isFollowing(userId: string): boolean;
setStatus(status: PresenceState['status']): void;
setIdle(): void;
setActive(): void;
destroy(): void;
}