svelte-realtime 0.1.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +1790 -0
- package/client.d.ts +239 -0
- package/client.js +1428 -0
- package/devtools.d.ts +2 -0
- package/devtools.js +214 -0
- package/package.json +80 -0
- package/server.d.ts +815 -0
- package/server.js +2311 -0
- package/test.d.ts +110 -0
- package/test.js +330 -0
- package/vite.d.ts +43 -0
- package/vite.js +1246 -0
package/client.d.ts
ADDED
|
@@ -0,0 +1,239 @@
|
|
|
1
|
+
import type { Readable } from 'svelte/store';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Typed error for RPC failures.
|
|
5
|
+
* Contains a `code` field for programmatic handling.
|
|
6
|
+
*/
|
|
7
|
+
export class RpcError extends Error {
|
|
8
|
+
code: string;
|
|
9
|
+
/** Structured validation issues (present when code === 'VALIDATION'). */
|
|
10
|
+
issues?: Array<{ path: string[]; message: string }>;
|
|
11
|
+
constructor(code: string, message?: string);
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Create a callable RPC function for a given path.
|
|
16
|
+
* Used by generated client stubs -- not called directly by users.
|
|
17
|
+
*
|
|
18
|
+
* @param path - RPC path (e.g. `'chat/sendMessage'`)
|
|
19
|
+
* @returns An async function that sends the RPC and returns the result
|
|
20
|
+
*
|
|
21
|
+
* @internal
|
|
22
|
+
*/
|
|
23
|
+
export function __rpc(path: string): ((...args: any[]) => Promise<any>) & {
|
|
24
|
+
/** Bypass deduplication -- always send a fresh request. */
|
|
25
|
+
fresh: (...args: any[]) => Promise<any>;
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Create a reactive stream store for a given path.
|
|
30
|
+
* Used by generated client stubs -- not called directly by users.
|
|
31
|
+
*
|
|
32
|
+
* The store value is:
|
|
33
|
+
* - `undefined` while loading
|
|
34
|
+
* - The initial data once loaded
|
|
35
|
+
* - Automatically updated with live pub/sub events
|
|
36
|
+
* - `{ error: RpcError }` if the initial fetch fails
|
|
37
|
+
*
|
|
38
|
+
* @param path - Stream path (e.g. `'chat/messages'`)
|
|
39
|
+
* @param options - Merge strategy and options
|
|
40
|
+
*
|
|
41
|
+
* @internal
|
|
42
|
+
*/
|
|
43
|
+
/**
|
|
44
|
+
* A reactive stream store with live update merging, optimistic updates,
|
|
45
|
+
* SSR hydration, and cursor-based pagination.
|
|
46
|
+
*/
|
|
47
|
+
export interface StreamStore<T = any> extends Readable<T> {
|
|
48
|
+
/** Apply an instant UI update. Returns a rollback function. */
|
|
49
|
+
optimistic(event: string, data: any): () => void;
|
|
50
|
+
/** Pre-populate with SSR data to avoid loading spinners. */
|
|
51
|
+
hydrate(initialData: T): StreamStore<T>;
|
|
52
|
+
/** Load the next page of data (cursor-based pagination). */
|
|
53
|
+
loadMore(...extraArgs: any[]): Promise<boolean>;
|
|
54
|
+
/** Whether more pages are available for loading. */
|
|
55
|
+
readonly hasMore: boolean;
|
|
56
|
+
/** Enable history tracking for undo/redo. */
|
|
57
|
+
enableHistory(maxSize?: number): void;
|
|
58
|
+
/** Undo the last change. */
|
|
59
|
+
undo(): void;
|
|
60
|
+
/** Redo the last undone change. */
|
|
61
|
+
redo(): void;
|
|
62
|
+
/** Whether there are entries to undo. */
|
|
63
|
+
readonly canUndo: boolean;
|
|
64
|
+
/** Whether there are entries to redo. */
|
|
65
|
+
readonly canRedo: boolean;
|
|
66
|
+
/** Pause history recording. Events still apply but no snapshots are saved. */
|
|
67
|
+
pauseHistory(): void;
|
|
68
|
+
/** Resume history recording. Records current state as a snapshot. */
|
|
69
|
+
resumeHistory(): void;
|
|
70
|
+
/** Return a wrapper store that only activates when `condition` is truthy. Accepts a boolean, a Svelte store, or a getter function. Getter functions are evaluated once at subscribe time; for reactivity, pass a store. */
|
|
71
|
+
when(condition: boolean | Readable<any> | (() => any)): Readable<T | undefined>;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
export function __stream(
|
|
75
|
+
path: string,
|
|
76
|
+
options?: {
|
|
77
|
+
merge?: 'crud' | 'latest' | 'set' | 'presence' | 'cursor';
|
|
78
|
+
key?: string;
|
|
79
|
+
prepend?: boolean;
|
|
80
|
+
max?: number;
|
|
81
|
+
replay?: boolean;
|
|
82
|
+
},
|
|
83
|
+
isDynamic?: false
|
|
84
|
+
): StreamStore;
|
|
85
|
+
|
|
86
|
+
export function __stream(
|
|
87
|
+
path: string,
|
|
88
|
+
options: {
|
|
89
|
+
merge?: 'crud' | 'latest' | 'set' | 'presence' | 'cursor';
|
|
90
|
+
key?: string;
|
|
91
|
+
prepend?: boolean;
|
|
92
|
+
max?: number;
|
|
93
|
+
replay?: boolean;
|
|
94
|
+
} | undefined,
|
|
95
|
+
isDynamic: true
|
|
96
|
+
): (...args: any[]) => StreamStore;
|
|
97
|
+
|
|
98
|
+
/**
|
|
99
|
+
* Group multiple RPC calls into a single WebSocket frame.
|
|
100
|
+
* All calls are sent together and responses come back in one frame.
|
|
101
|
+
*
|
|
102
|
+
* @param fn - Function that returns an array of RPC call promises
|
|
103
|
+
* @param options - Optional: `{ sequential: true }` to run in order on the server
|
|
104
|
+
* @returns Array of results in the same order as the calls
|
|
105
|
+
*
|
|
106
|
+
* @example
|
|
107
|
+
* ```js
|
|
108
|
+
* import { batch } from 'svelte-realtime/client';
|
|
109
|
+
* import { addTodo, assignUser } from '$live/boards';
|
|
110
|
+
*
|
|
111
|
+
* const [todo, user] = await batch(() => [
|
|
112
|
+
* addTodo('Buy milk'),
|
|
113
|
+
* assignUser(todoId, userId)
|
|
114
|
+
* ]);
|
|
115
|
+
* ```
|
|
116
|
+
*/
|
|
117
|
+
export function batch<T extends Promise<any>[]>(
|
|
118
|
+
fn: () => [...T],
|
|
119
|
+
options?: { sequential?: boolean }
|
|
120
|
+
): Promise<{ [K in keyof T]: Awaited<T[K]> }>;
|
|
121
|
+
|
|
122
|
+
/**
|
|
123
|
+
* Create a callable binary RPC function for a given path.
|
|
124
|
+
* Sends the first argument as raw binary and remaining args as JSON in a header.
|
|
125
|
+
* Used by generated client stubs for `live.binary()` exports.
|
|
126
|
+
*
|
|
127
|
+
* @param path - RPC path (e.g. `'upload/avatar'`)
|
|
128
|
+
* @returns A function that sends binary data over WebSocket
|
|
129
|
+
*
|
|
130
|
+
* @internal
|
|
131
|
+
*/
|
|
132
|
+
export function __binaryRpc(path: string): (buffer: ArrayBuffer, ...args: any[]) => Promise<any>;
|
|
133
|
+
|
|
134
|
+
/**
|
|
135
|
+
* Configure client-side connection hooks.
|
|
136
|
+
*
|
|
137
|
+
* @example
|
|
138
|
+
* ```js
|
|
139
|
+
* import { configure } from 'svelte-realtime/client';
|
|
140
|
+
*
|
|
141
|
+
* configure({
|
|
142
|
+
* onConnect() { console.log('Connected'); },
|
|
143
|
+
* onDisconnect() { console.log('Disconnected'); }
|
|
144
|
+
* });
|
|
145
|
+
* ```
|
|
146
|
+
*/
|
|
147
|
+
/**
|
|
148
|
+
* An entry in the offline mutation queue.
|
|
149
|
+
*/
|
|
150
|
+
export interface OfflineEntry {
|
|
151
|
+
path: string;
|
|
152
|
+
args: any[];
|
|
153
|
+
queuedAt: number;
|
|
154
|
+
resolve: Function;
|
|
155
|
+
reject: Function;
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
export function configure(config: {
|
|
159
|
+
/** Called when the WebSocket connection opens (not on initial connect, only reconnects). */
|
|
160
|
+
onConnect?(): void;
|
|
161
|
+
/** Called when the WebSocket connection closes. */
|
|
162
|
+
onDisconnect?(): void;
|
|
163
|
+
/** Offline mutation queue configuration. */
|
|
164
|
+
offline?: {
|
|
165
|
+
/** Enable queuing RPCs when disconnected. */
|
|
166
|
+
queue?: boolean;
|
|
167
|
+
/** Maximum queue size before oldest entries are dropped. @default 100 */
|
|
168
|
+
maxQueue?: number;
|
|
169
|
+
/** Replay strategy on reconnect: 'sequential' (default), 'concurrent' (10-at-a-time), or a custom filter function. 'batch' is accepted as an alias for 'concurrent'. */
|
|
170
|
+
replay?: 'sequential' | 'concurrent' | 'batch' | ((queue: OfflineEntry[]) => OfflineEntry[]);
|
|
171
|
+
/** Filter function called before replaying each queued call. Return false to drop. */
|
|
172
|
+
beforeReplay?(call: { path: string; args: any[]; queuedAt: number }): boolean;
|
|
173
|
+
/** Called when a replayed call fails. */
|
|
174
|
+
onReplayError?(call: { path: string; args: any[]; queuedAt: number }, error: any): void;
|
|
175
|
+
};
|
|
176
|
+
}): void;
|
|
177
|
+
|
|
178
|
+
/**
|
|
179
|
+
* Register a handler for point-to-point signals.
|
|
180
|
+
* Signals are sent by `ctx.signal(userId, event, data)` on the server.
|
|
181
|
+
*
|
|
182
|
+
* The userId must match the one used by `enableSignals()` on the server,
|
|
183
|
+
* because the server publishes to `__signal:${userId}`.
|
|
184
|
+
*
|
|
185
|
+
* @param userId - The current user's id (must match server-side enableSignals)
|
|
186
|
+
* @param callback - Called with (event, data) for each received signal
|
|
187
|
+
* @returns Unsubscribe function
|
|
188
|
+
*
|
|
189
|
+
* @example
|
|
190
|
+
* ```js
|
|
191
|
+
* import { onSignal } from 'svelte-realtime/client';
|
|
192
|
+
*
|
|
193
|
+
* const unsub = onSignal(currentUser.id, (event, data) => {
|
|
194
|
+
* if (event === 'call:offer') showIncomingCall(data);
|
|
195
|
+
* });
|
|
196
|
+
* ```
|
|
197
|
+
*/
|
|
198
|
+
export function onSignal(userId: string, callback: (event: string, data: any) => void): () => void;
|
|
199
|
+
/** @deprecated Pass userId as the first argument so the topic matches the server. */
|
|
200
|
+
export function onSignal(callback: (event: string, data: any) => void): () => void;
|
|
201
|
+
|
|
202
|
+
/**
|
|
203
|
+
* Combine multiple stores into a single derived store.
|
|
204
|
+
* When any source updates, the combining function re-runs.
|
|
205
|
+
*
|
|
206
|
+
* @example
|
|
207
|
+
* ```js
|
|
208
|
+
* import { combine } from 'svelte-realtime/client';
|
|
209
|
+
* import { orders, inventory } from '$live/dashboard';
|
|
210
|
+
*
|
|
211
|
+
* const dashboard = combine(orders, inventory, (o, i) => ({
|
|
212
|
+
* pendingOrders: o?.filter(x => x.status === 'pending').length ?? 0,
|
|
213
|
+
* lowStock: i?.filter(x => x.qty < 10) ?? []
|
|
214
|
+
* }));
|
|
215
|
+
* ```
|
|
216
|
+
*/
|
|
217
|
+
export function combine<A, B, R>(a: Readable<A>, b: Readable<B>, fn: (a: A, b: B) => R): Readable<R>;
|
|
218
|
+
export function combine<A, B, C, R>(a: Readable<A>, b: Readable<B>, c: Readable<C>, fn: (a: A, b: B, c: C) => R): Readable<R>;
|
|
219
|
+
export function combine<A, B, C, D, R>(a: Readable<A>, b: Readable<B>, c: Readable<C>, d: Readable<D>, fn: (a: A, b: B, c: C, d: D) => R): Readable<R>;
|
|
220
|
+
export function combine<A, B, C, D, E, R>(a: Readable<A>, b: Readable<B>, c: Readable<C>, d: Readable<D>, e: Readable<E>, fn: (a: A, b: B, c: C, d: D, e: E) => R): Readable<R>;
|
|
221
|
+
export function combine<A, B, C, D, E, F, R>(a: Readable<A>, b: Readable<B>, c: Readable<C>, d: Readable<D>, e: Readable<E>, f: Readable<F>, fn: (a: A, b: B, c: C, d: D, e: E, f: F) => R): Readable<R>;
|
|
222
|
+
export function combine(...args: [...Readable<any>[], (...values: any[]) => any]): Readable<any>;
|
|
223
|
+
|
|
224
|
+
/**
|
|
225
|
+
* Dev-mode instrumentation data.
|
|
226
|
+
* `null` in production builds (tree-shaken).
|
|
227
|
+
*/
|
|
228
|
+
export const __devtools: {
|
|
229
|
+
history: Array<{
|
|
230
|
+
path: string;
|
|
231
|
+
args: any[];
|
|
232
|
+
ok: boolean;
|
|
233
|
+
result: any;
|
|
234
|
+
duration: number;
|
|
235
|
+
time: number;
|
|
236
|
+
}>;
|
|
237
|
+
streams: Map<string, { path: string; topic: string | null; subCount: number }>;
|
|
238
|
+
pending: Map<string, { path: string; args: any[]; startTime: number }>;
|
|
239
|
+
} | null;
|