sveltekit-sync
Version:
Local-first sync engine for SvelteKit
93 lines (92 loc) • 2.53 kB
TypeScript
/**
* EphemeralStore - In-memory store for transient data like presence
*
* Data stored here is not persisted to database and expires after TTL.
* Used for presence, awareness, and other real-time ephemeral data.
*/
export interface EphemeralEntry<T = any> {
data: T;
userId: string;
clientId: string;
channel: string;
expiresAt: number;
updatedAt: number;
}
export interface EphemeralStoreOptions {
/** Time-to-live in milliseconds (default: 60000 = 60s) */
ttl?: number;
/** Cleanup interval in milliseconds (default: 30000 = 30s) */
cleanupInterval?: number;
/** Callback when an entry expires */
onExpire?: (entry: EphemeralEntry) => void;
}
/**
* Generic in-memory store for ephemeral data
*/
export declare class EphemeralStore<T = any> {
private store;
private ttl;
private cleanupInterval;
private onExpire?;
constructor(options?: EphemeralStoreOptions);
/**
* Store or update an entry
*/
set(key: string, entry: Omit<EphemeralEntry<T>, 'expiresAt' | 'updatedAt'>): void;
/**
* Get an entry by key
* Returns null if not found or expired
*/
get(key: string): T | null;
/**
* Get full entry with metadata
*/
getEntry(key: string): EphemeralEntry<T> | null;
/**
* Delete an entry
* Returns true if entry existed
*/
delete(key: string): boolean;
/**
* Get all entries for a specific channel
*/
getByChannel(channel: string): EphemeralEntry<T>[];
/**
* Get all entries for a specific user across all channels
*/
getByUser(userId: string): EphemeralEntry<T>[];
/**
* Get all data values in a channel (without metadata)
* Optionally exclude a specific client
*/
getAllInChannel(channel: string, excludeClientId?: string): T[];
/**
* Get entry by channel and user
*/
getByChannelAndUser(channel: string, userId: string): EphemeralEntry<T> | null;
/**
* Remove all entries for a specific client
*/
removeByClient(clientId: string): number;
/**
* Remove all entries in a channel
*/
removeByChannel(channel: string): number;
/**
* Clean up expired entries
* Called automatically on interval
*/
cleanup(): void;
/**
* Get the number of entries in the store
*/
size(): number;
/**
* Clear all entries
*/
clear(): void;
/**
* Stop cleanup interval and clear store
*/
destroy(): void;
}