@absolutejs/sync 0.0.1 → 0.2.0
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/README.md +281 -24
- package/dist/adapters/drizzle/collection.d.ts +27 -0
- package/dist/adapters/drizzle/index.d.ts +20 -0
- package/dist/adapters/drizzle/index.js +265 -0
- package/dist/adapters/drizzle/index.js.map +14 -0
- package/dist/adapters/drizzle/predicate.d.ts +20 -0
- package/dist/adapters/drizzle/read.d.ts +31 -0
- package/dist/adapters/drizzle/topics.d.ts +41 -0
- package/dist/adapters/drizzle/write.d.ts +69 -0
- package/dist/adapters/mysql/index.d.ts +75 -0
- package/dist/adapters/mysql/index.js +171 -0
- package/dist/adapters/mysql/index.js.map +11 -0
- package/dist/adapters/postgres/index.d.ts +53 -0
- package/dist/adapters/postgres/index.js +86 -0
- package/dist/adapters/postgres/index.js.map +10 -0
- package/dist/adapters/prisma/collection.d.ts +39 -0
- package/dist/adapters/prisma/index.d.ts +23 -0
- package/dist/adapters/prisma/index.js +231 -0
- package/dist/adapters/prisma/index.js.map +14 -0
- package/dist/adapters/prisma/predicate.d.ts +20 -0
- package/dist/adapters/prisma/read.d.ts +28 -0
- package/dist/adapters/prisma/topics.d.ts +29 -0
- package/dist/adapters/prisma/write.d.ts +65 -0
- package/dist/adapters/sqlite/index.d.ts +32 -0
- package/dist/adapters/sqlite/index.js +128 -0
- package/dist/adapters/sqlite/index.js.map +11 -0
- package/dist/angular/index.d.ts +1 -0
- package/dist/angular/index.js +347 -0
- package/dist/angular/index.js.map +11 -0
- package/dist/angular/sync-collection.service.d.ts +20 -0
- package/dist/client/index.d.ts +12 -30
- package/dist/client/index.js +1099 -3
- package/dist/client/index.js.map +10 -4
- package/dist/client/liveQuery.d.ts +75 -0
- package/dist/client/presence.d.ts +37 -0
- package/dist/client/subscriber.d.ts +30 -0
- package/dist/client/syncClient.d.ts +53 -0
- package/dist/client/syncCollection.d.ts +102 -0
- package/dist/client/syncStore.d.ts +81 -0
- package/dist/engine/aggregate.d.ts +45 -0
- package/dist/engine/cluster.d.ts +41 -0
- package/dist/engine/collection.d.ts +87 -0
- package/dist/engine/connection.d.ts +103 -0
- package/dist/engine/dataflow.d.ts +109 -0
- package/dist/engine/equiJoin.d.ts +51 -0
- package/dist/engine/graph.d.ts +85 -0
- package/dist/engine/index.d.ts +40 -0
- package/dist/engine/index.js +1774 -0
- package/dist/engine/index.js.map +23 -0
- package/dist/engine/materializedView.d.ts +53 -0
- package/dist/engine/mutation.d.ts +66 -0
- package/dist/engine/pollingSource.d.ts +42 -0
- package/dist/engine/presence.d.ts +46 -0
- package/dist/engine/reactive.d.ts +67 -0
- package/dist/engine/routes.d.ts +40 -0
- package/dist/engine/socket.d.ts +67 -0
- package/dist/engine/syncEngine.d.ts +132 -0
- package/dist/engine/types.d.ts +45 -0
- package/dist/index.d.ts +4 -0
- package/dist/index.js +327 -3
- package/dist/index.js.map +8 -5
- package/dist/react/index.d.ts +1 -0
- package/dist/react/index.js +332 -0
- package/dist/react/index.js.map +11 -0
- package/dist/react/useSyncCollection.d.ts +16 -0
- package/dist/reactiveHub.d.ts +6 -0
- package/dist/svelte/createSyncCollectionStore.d.ts +15 -0
- package/dist/svelte/index.d.ts +1 -0
- package/dist/svelte/index.js +338 -0
- package/dist/svelte/index.js.map +11 -0
- package/dist/vue/index.d.ts +1 -0
- package/dist/vue/index.js +331 -0
- package/dist/vue/index.js.map +11 -0
- package/dist/vue/useSyncCollection.d.ts +17 -0
- package/package.json +104 -6
package/dist/client/index.js.map
CHANGED
|
@@ -1,10 +1,16 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
|
-
"sources": ["
|
|
3
|
+
"sources": ["../src/client/subscriber.ts", "../src/reactiveHub.ts", "../src/client/liveQuery.ts", "../src/client/syncCollection.ts", "../src/client/presence.ts", "../src/client/syncClient.ts", "../src/client/syncStore.ts"],
|
|
4
4
|
"sourcesContent": [
|
|
5
|
-
"import type { ReactiveEvent } from '../reactiveHub';\n\nexport type
|
|
5
|
+
"import type { ReactiveEvent } from '../reactiveHub';\n\nexport type SyncSubscriberOptions = {\n\t/** Topics to subscribe to. A trailing `*` matches by prefix server-side. */\n\ttopics: string[];\n\t/** Called for every reactive event pushed from the server. */\n\tonEvent: (event: ReactiveEvent) => void;\n\t/** SSE endpoint mounted by the {@link sync} plugin. Defaults to `/sync`. */\n\turl?: string;\n\tonOpen?: () => void;\n\tonError?: (event: Event) => void;\n\t/** Send cookies with the SSE request (cross-origin auth). */\n\twithCredentials?: boolean;\n\t/**\n\t * EventSource implementation to use. Defaults to the global one; pass a polyfill\n\t * for non-browser runtimes.\n\t */\n\teventSourceImpl?: typeof EventSource;\n};\n\nexport type SyncSubscriber = {\n\tclose: () => void;\n\t/** The underlying EventSource, for advanced listeners. */\n\tsource: EventSource;\n};\n\n/**\n * Subscribe a browser to the server's {@link ReactiveHub} over SSE. `onEvent` fires\n * whenever a subscribed topic is published — the cue to refetch (or read the pushed\n * payload) instead of polling. EventSource reconnects automatically on transient\n * network drops.\n */\nexport const createSyncSubscriber = ({\n\ttopics,\n\tonEvent,\n\turl = '/sync',\n\tonOpen,\n\tonError,\n\twithCredentials,\n\teventSourceImpl\n}: SyncSubscriberOptions): SyncSubscriber => {\n\tconst Impl = eventSourceImpl ?? globalThis.EventSource;\n\tif (!Impl) {\n\t\tthrow new Error(\n\t\t\t'createSyncSubscriber requires EventSource. Run in a browser or pass eventSourceImpl.'\n\t\t);\n\t}\n\n\tconst params = new URLSearchParams({ topics: topics.join(',') });\n\tconst separator = url.includes('?') ? '&' : '?';\n\tconst source = new Impl(`${url}${separator}${params.toString()}`, {\n\t\twithCredentials: withCredentials ?? false\n\t});\n\n\tsource.onmessage = (event) => {\n\t\ttry {\n\t\t\tonEvent(JSON.parse(event.data) as ReactiveEvent);\n\t\t} catch {\n\t\t\t// ignore heartbeats / non-JSON frames\n\t\t}\n\t};\n\tif (onOpen) {\n\t\tsource.onopen = () => onOpen();\n\t}\n\tif (onError) {\n\t\tsource.onerror = (event) => onError(event);\n\t}\n\n\treturn {\n\t\tclose: () => source.close(),\n\t\tsource\n\t};\n};\n",
|
|
6
|
+
"/**\n * Topic of the synthetic frame the SSE plugin emits when a stream opens (and\n * re-opens after a reconnect). Clients use it to tell \"the stream connected\"\n * apart from a real data-change event.\n */\nexport const SYNC_OPEN_TOPIC = '@absolutejs/sync:open';\n\nexport type ReactiveEvent<TPayload = unknown> = {\n\ttopic: string;\n\tat: number;\n\tpayload?: TPayload;\n};\n\nexport type ReactiveListener<TPayload = unknown> = (\n\tevent: ReactiveEvent<TPayload>\n) => void;\n\nexport type ReactiveHub = {\n\t/**\n\t * Notify every subscriber of `topic` (and any prefix-wildcard subscriber that\n\t * matches it). Call this from a mutation after the durable write commits.\n\t */\n\tpublish: (topic: string, payload?: unknown) => void;\n\t/**\n\t * Listen on one or more topics. A topic ending in `*` matches every topic that\n\t * starts with the prefix before it (e.g. `voice:session:*`). Returns an\n\t * unsubscribe function.\n\t */\n\tsubscribe: (topics: string[], listener: ReactiveListener) => () => void;\n\t/** Number of active subscribers, optionally for a single exact topic. */\n\tsubscriberCount: (topic?: string) => number;\n};\n\ntype Subscription = {\n\texact: Set<string>;\n\tprefixes: string[];\n\tlistener: ReactiveListener;\n};\n\n/**\n * An in-memory topic pub/sub for reactive, push-on-change updates.\n *\n * The pattern that replaces polling: a query/widget subscribes to the topics its\n * data depends on; a mutation `publish`es those topics after it writes; subscribers\n * are notified immediately and refetch (or receive the pushed payload) — instead of\n * every client hammering the server on a timer.\n *\n * Dependencies are explicit (you name the topics) rather than auto-tracked from a\n * query's read set — deliberately small, with no sandbox or query interception.\n * Pair it with the {@link sync} Elysia plugin to stream events to browsers over SSE.\n */\nexport const createReactiveHub = (): ReactiveHub => {\n\tconst subscriptions = new Set<Subscription>();\n\n\tconst matches = (subscription: Subscription, topic: string) => {\n\t\tif (subscription.exact.has(topic)) {\n\t\t\treturn true;\n\t\t}\n\t\treturn subscription.prefixes.some((prefix) => topic.startsWith(prefix));\n\t};\n\n\treturn {\n\t\tpublish: (topic, payload) => {\n\t\t\tconst event: ReactiveEvent = { topic, at: Date.now(), payload };\n\t\t\tfor (const subscription of subscriptions) {\n\t\t\t\tif (matches(subscription, topic)) {\n\t\t\t\t\tsubscription.listener(event);\n\t\t\t\t}\n\t\t\t}\n\t\t},\n\t\tsubscribe: (topics, listener) => {\n\t\t\tconst exact = new Set<string>();\n\t\t\tconst prefixes: string[] = [];\n\t\t\tfor (const topic of topics) {\n\t\t\t\tif (topic.endsWith('*')) {\n\t\t\t\t\tprefixes.push(topic.slice(0, -1));\n\t\t\t\t} else {\n\t\t\t\t\texact.add(topic);\n\t\t\t\t}\n\t\t\t}\n\t\t\tconst subscription: Subscription = { exact, prefixes, listener };\n\t\t\tsubscriptions.add(subscription);\n\t\t\treturn () => {\n\t\t\t\tsubscriptions.delete(subscription);\n\t\t\t};\n\t\t},\n\t\tsubscriberCount: (topic) => {\n\t\t\tif (topic === undefined) {\n\t\t\t\treturn subscriptions.size;\n\t\t\t}\n\t\t\tlet count = 0;\n\t\t\tfor (const subscription of subscriptions) {\n\t\t\t\tif (matches(subscription, topic)) {\n\t\t\t\t\tcount += 1;\n\t\t\t\t}\n\t\t\t}\n\t\t\treturn count;\n\t\t}\n\t};\n};\n",
|
|
7
|
+
"import { SYNC_OPEN_TOPIC } from '../reactiveHub';\nimport type { ReactiveEvent } from '../reactiveHub';\nimport { createSyncSubscriber } from './subscriber';\n\nexport type LiveQueryState<T> = {\n\t/** Latest query result, or `undefined` before the first successful fetch. */\n\tdata: T | undefined;\n\t/** Error from the most recent fetch, or `undefined` if it succeeded. */\n\terror: unknown;\n\t/** `true` until the first result arrives (no data yet). */\n\tloading: boolean;\n\t/** `true` while a (re)fetch is in flight — data may still be present. */\n\tfetching: boolean;\n};\n\nexport type LiveQueryOptions<T> = {\n\t/**\n\t * Topics this query depends on — typically the server's\n\t * `deriveReadTopics(...).topics`. Any event on one of them triggers a\n\t * refetch. A trailing `*` matches by prefix server-side.\n\t */\n\ttopics: string[];\n\t/**\n\t * Runs the read and resolves the query result. Receives an `AbortSignal`\n\t * that fires when a newer fetch supersedes this one or the query is closed.\n\t */\n\tfetcher: (signal: AbortSignal) => Promise<T>;\n\t/** SSE endpoint mounted by the {@link sync} plugin. Defaults to `/sync`. */\n\turl?: string;\n\t/** Send cookies with the SSE request (cross-origin auth). */\n\twithCredentials?: boolean;\n\t/** EventSource implementation; defaults to the global one. */\n\teventSourceImpl?: typeof EventSource;\n\t/**\n\t * Seed data (e.g. from SSR). When provided, the initial fetch is skipped —\n\t * the query trusts this until an event or a manual {@link LiveQuery.refetch}.\n\t */\n\tinitialData?: T;\n\t/**\n\t * Skip the initial fetch and stay idle until the first event or a manual\n\t * refetch. Reconnects still re-hydrate.\n\t */\n\tmanual?: boolean;\n\t/**\n\t * Coalesce a burst of events into one refetch within this window (ms).\n\t * Defaults to 0 — refetch once per event.\n\t */\n\tdebounceMs?: number;\n\t/** Called when a fetch rejects (stale data is retained). */\n\tonError?: (error: unknown) => void;\n};\n\nexport type LiveQuery<T> = {\n\t/** Current state snapshot (stable reference until the next change). */\n\tget: () => LiveQueryState<T>;\n\t/** Subscribe to state changes; returns an unsubscribe. */\n\tsubscribe: (listener: (state: LiveQueryState<T>) => void) => () => void;\n\t/** Force a refetch now. Resolves when this fetch settles. */\n\trefetch: () => Promise<void>;\n\t/** Stop the SSE subscription, cancel any in-flight fetch, drop listeners. */\n\tclose: () => void;\n};\n\n/**\n * A live, self-refreshing query: hydrate once via `fetcher`, then refetch\n * whenever the server publishes one of `topics` — the read half of Tier 2,\n * built on {@link createSyncSubscriber}. Framework-agnostic: `get` + `subscribe`\n * plug straight into React's `useSyncExternalStore` or any equivalent.\n *\n * Pair it with the Drizzle adapter's `deriveReadTopics` (server) and\n * `publishChange`/`publishWhere` (mutations) so a write invalidates exactly the\n * queries that read the changed rows.\n */\nexport const createLiveQuery = <T>(\n\toptions: LiveQueryOptions<T>\n): LiveQuery<T> => {\n\tconst hasSeed = options.initialData !== undefined;\n\tlet state: LiveQueryState<T> = {\n\t\tdata: options.initialData,\n\t\terror: undefined,\n\t\tloading: !options.manual && !hasSeed,\n\t\tfetching: false\n\t};\n\n\tconst listeners = new Set<(state: LiveQueryState<T>) => void>();\n\tconst setState = (patch: Partial<LiveQueryState<T>>) => {\n\t\tstate = { ...state, ...patch };\n\t\tfor (const listener of listeners) {\n\t\t\tlistener(state);\n\t\t}\n\t};\n\n\tlet requestSeq = 0;\n\tlet inFlight: AbortController | undefined;\n\tlet closed = false;\n\n\tconst refetch = async () => {\n\t\tif (closed) {\n\t\t\treturn;\n\t\t}\n\t\tconst seq = (requestSeq += 1);\n\t\tinFlight?.abort();\n\t\tconst controller = new AbortController();\n\t\tinFlight = controller;\n\t\tsetState({ fetching: true });\n\n\t\ttry {\n\t\t\tconst data = await options.fetcher(controller.signal);\n\t\t\tif (seq !== requestSeq) {\n\t\t\t\treturn; // superseded by a newer fetch\n\t\t\t}\n\t\t\tsetState({\n\t\t\t\tdata,\n\t\t\t\terror: undefined,\n\t\t\t\tloading: false,\n\t\t\t\tfetching: false\n\t\t\t});\n\t\t} catch (error) {\n\t\t\tif (controller.signal.aborted || seq !== requestSeq) {\n\t\t\t\treturn; // aborted or superseded — leave state to the winner\n\t\t\t}\n\t\t\tsetState({ error, loading: false, fetching: false });\n\t\t\toptions.onError?.(error);\n\t\t} finally {\n\t\t\tif (inFlight === controller) {\n\t\t\t\tinFlight = undefined;\n\t\t\t}\n\t\t}\n\t};\n\n\tlet debounceTimer: ReturnType<typeof setTimeout> | undefined;\n\tconst scheduleRefetch = () => {\n\t\tif (closed) {\n\t\t\treturn;\n\t\t}\n\t\tif (!options.debounceMs) {\n\t\t\tvoid refetch();\n\t\t\treturn;\n\t\t}\n\t\tif (debounceTimer !== undefined) {\n\t\t\treturn; // a refetch is already queued for this window\n\t\t}\n\t\tdebounceTimer = setTimeout(() => {\n\t\t\tdebounceTimer = undefined;\n\t\t\tvoid refetch();\n\t\t}, options.debounceMs);\n\t};\n\n\tlet opened = false;\n\tconst onEvent = (event: ReactiveEvent) => {\n\t\tif (event.topic === SYNC_OPEN_TOPIC) {\n\t\t\t// First open is the initial connect (already hydrated); a later open\n\t\t\t// is a reconnect, so re-hydrate to catch events missed while down.\n\t\t\tif (opened) {\n\t\t\t\tscheduleRefetch();\n\t\t\t}\n\t\t\topened = true;\n\t\t\treturn;\n\t\t}\n\t\tscheduleRefetch();\n\t};\n\n\tconst subscriber = createSyncSubscriber({\n\t\ttopics: options.topics,\n\t\tonEvent,\n\t\turl: options.url,\n\t\twithCredentials: options.withCredentials,\n\t\teventSourceImpl: options.eventSourceImpl\n\t});\n\n\tif (!options.manual && !hasSeed) {\n\t\tvoid refetch();\n\t}\n\n\tconst close = () => {\n\t\tif (closed) {\n\t\t\treturn;\n\t\t}\n\t\tclosed = true;\n\t\tsubscriber.close();\n\t\tinFlight?.abort();\n\t\tif (debounceTimer !== undefined) {\n\t\t\tclearTimeout(debounceTimer);\n\t\t\tdebounceTimer = undefined;\n\t\t}\n\t\tlisteners.clear();\n\t};\n\n\treturn {\n\t\tget: () => state,\n\t\tsubscribe: (listener) => {\n\t\t\tlisteners.add(listener);\n\t\t\treturn () => {\n\t\t\t\tlisteners.delete(listener);\n\t\t\t};\n\t\t},\n\t\trefetch,\n\t\tclose\n\t};\n};\n\n/**\n * A small default `fetcher`: GET `url` and parse JSON. Forwards the live query's\n * abort signal and throws on a non-2xx response.\n *\n * @example\n * createLiveQuery({ topics, fetcher: jsonFetcher<User[]>('/api/users') })\n */\nexport const jsonFetcher =\n\t<T>(url: string, init?: RequestInit) =>\n\tasync (signal: AbortSignal): Promise<T> => {\n\t\tconst response = await fetch(url, { ...init, signal });\n\t\tif (!response.ok) {\n\t\t\tthrow new Error(`${response.status} ${response.statusText}`);\n\t\t}\n\t\treturn (await response.json()) as T;\n\t};\n",
|
|
8
|
+
"import type { ServerFrame } from '../engine/connection';\nimport type { RowKey } from '../engine/types';\n\nexport type { ServerFrame } from '../engine/connection';\n\nexport type SyncCollectionStatus = 'connecting' | 'ready' | 'closed';\n\nexport type SyncCollectionState<T> = {\n\t/** Visible rows: the server state with pending optimistic mutations applied. */\n\tdata: T[];\n\t/** Connection/sync status. */\n\tstatus: SyncCollectionStatus;\n\t/** Last error message from the server, or `undefined`. */\n\terror: unknown;\n};\n\n/** A working set a mutation's optimistic effect edits in place. */\nexport type OptimisticDraft<T> = {\n\t/** Insert or replace a row by key. */\n\tset: (row: T) => void;\n\t/** Remove a row by key. */\n\tdelete: (key: RowKey) => void;\n};\n\nexport type MutateOptions<T> = {\n\t/** Registered server mutation name. */\n\tname: string;\n\t/** Arguments forwarded to the mutation handler. */\n\targs?: unknown;\n\t/**\n\t * Apply this mutation's effect to the local set immediately for instant UI.\n\t * Reverted automatically if the server rejects it. Omit for a non-optimistic\n\t * mutation (UI updates only once the authoritative diff arrives).\n\t */\n\toptimistic?: (draft: OptimisticDraft<T>) => void;\n};\n\n/** A pending mutation persisted for replay across reloads. */\nexport type PendingMutationRecord = {\n\tmutationId: number;\n\tname: string;\n\targs: unknown;\n};\n\n/**\n * Durable storage for the pending-mutation queue, so unconfirmed mutations\n * survive a page reload (offline). The queue is replayed when the socket\n * connects; records are dropped as they're acked.\n */\nexport type MutationStorage = {\n\tload: () => PendingMutationRecord[] | Promise<PendingMutationRecord[]>;\n\tsave: (records: PendingMutationRecord[]) => void | Promise<void>;\n};\n\n/**\n * A {@link MutationStorage} backed by `localStorage` under `key`. No-ops where\n * `localStorage` is unavailable (e.g. SSR).\n */\nexport const localStorageMutationStorage = (key: string): MutationStorage => ({\n\tload: () => {\n\t\tconst raw = globalThis.localStorage?.getItem(key);\n\t\treturn raw ? (JSON.parse(raw) as PendingMutationRecord[]) : [];\n\t},\n\tsave: (records) => {\n\t\tglobalThis.localStorage?.setItem(key, JSON.stringify(records));\n\t}\n});\n\nexport type SyncCollectionOptions<T> = {\n\t/** WebSocket URL of the {@link syncSocket} endpoint (e.g. `ws://host/sync/ws`). */\n\turl: string;\n\t/** Registered collection name to subscribe to. */\n\tcollection: string;\n\t/** Query params forwarded to the server collection's hydrate/match/authorize. */\n\tparams?: unknown;\n\t/** Row identity, used to apply diffs and optimistic edits. Defaults to `row.id`. */\n\tkey?: (row: T) => RowKey;\n\t/** WebSocket implementation; defaults to the global one (pass for tests/SSR). */\n\twebSocketImpl?: typeof WebSocket;\n\t/**\n\t * Base reconnect delay (ms), doubled each attempt up to `maxReconnectMs`.\n\t * Set 0 to disable auto-reconnect. Defaults to 500.\n\t */\n\treconnectMs?: number;\n\t/** Maximum reconnect backoff (ms). Defaults to 10000. */\n\tmaxReconnectMs?: number;\n\t/**\n\t * Persist the pending-mutation queue so it survives a reload (offline) and\n\t * replays on connect. See {@link localStorageMutationStorage}.\n\t */\n\tstorage?: MutationStorage;\n\t/** Called with each server error message. */\n\tonError?: (error: unknown) => void;\n};\n\nexport type SyncCollection<T> = {\n\t/** Current state snapshot (stable reference until the next change). */\n\tget: () => SyncCollectionState<T>;\n\t/** Subscribe to state changes; returns an unsubscribe. */\n\tsubscribe: (\n\t\tlistener: (state: SyncCollectionState<T>) => void\n\t) => () => void;\n\t/**\n\t * Run a server mutation, optionally applying it optimistically. Resolves with\n\t * the server's result on ack, rejects (and rolls back) on reject. Pending\n\t * mutations are replayed when the socket reconnects, so they survive a drop.\n\t */\n\tmutate: <R = unknown>(options: MutateOptions<T>) => Promise<R>;\n\t/** Unsubscribe on the server, close the socket, and stop reconnecting. */\n\tclose: () => void;\n};\n\n// One store subscribes to exactly one collection, so a fixed frame id suffices.\nconst SUBSCRIPTION_ID = 's';\n\ntype PendingMutation<T> = {\n\tmutationId: number;\n\tname: string;\n\targs: unknown;\n\toptimistic?: (draft: OptimisticDraft<T>) => void;\n\tresolve: (result: unknown) => void;\n\treject: (error: unknown) => void;\n};\n\n/**\n * A live collection backed by the WebSocket sync engine. Reads: connect,\n * subscribe, apply the server's snapshot then row-level diffs, re-sync on\n * reconnect. Writes: {@link SyncCollection.mutate} applies an optimistic overlay\n * immediately, sends the mutation, and reconciles on ack (drop the overlay — the\n * authoritative diff already arrived) or reject (roll back). Framework-agnostic\n * (`get` + `subscribe`).\n *\n * Mutations are replayed on reconnect, so make server mutations idempotent —\n * delivery is at-least-once if an ack is lost across a drop.\n */\nexport const createSyncCollection = <T>(\n\toptions: SyncCollectionOptions<T>\n): SyncCollection<T> => {\n\tconst key = options.key ?? ((row: T) => (row as { id: RowKey }).id);\n\tconst reconnectMs = options.reconnectMs ?? 500;\n\tconst maxReconnectMs = options.maxReconnectMs ?? 10_000;\n\tconst Impl = options.webSocketImpl ?? globalThis.WebSocket;\n\tif (!Impl) {\n\t\tthrow new Error(\n\t\t\t'createSyncCollection requires WebSocket. Run in a browser or pass webSocketImpl.'\n\t\t);\n\t}\n\n\t// Server-authoritative rows; `pending` is the optimistic overlay on top.\n\tconst confirmed = new Map<RowKey, T>();\n\tconst pending: PendingMutation<T>[] = [];\n\tlet mutationSeq = 0;\n\n\tlet state: SyncCollectionState<T> = {\n\t\tdata: [],\n\t\tstatus: 'connecting',\n\t\terror: undefined\n\t};\n\tconst listeners = new Set<(state: SyncCollectionState<T>) => void>();\n\tconst setState = (patch: Partial<SyncCollectionState<T>>) => {\n\t\tstate = { ...state, ...patch };\n\t\tfor (const listener of listeners) {\n\t\t\tlistener(state);\n\t\t}\n\t};\n\n\t/** Recompute visible rows = confirmed + pending optimistic effects. */\n\tconst recompute = (patch: Partial<SyncCollectionState<T>> = {}) => {\n\t\tconst working = new Map(confirmed);\n\t\tconst draft: OptimisticDraft<T> = {\n\t\t\tset: (row) => working.set(key(row), row),\n\t\t\tdelete: (rowKey) => working.delete(rowKey)\n\t\t};\n\t\tfor (const mutation of pending) {\n\t\t\tmutation.optimistic?.(draft);\n\t\t}\n\t\tsetState({ ...patch, data: [...working.values()] });\n\t};\n\n\tlet socket: WebSocket | undefined;\n\tlet connected = false;\n\tlet closed = false;\n\tlet attempt = 0;\n\tlet reconnectTimer: ReturnType<typeof setTimeout> | undefined;\n\t// Highest change-feed version applied; sent as `since` to resume on reconnect.\n\tlet appliedVersion = 0;\n\n\tconst persist = () => {\n\t\tvoid options.storage?.save(\n\t\t\tpending.map((mutation) => ({\n\t\t\t\tmutationId: mutation.mutationId,\n\t\t\t\tname: mutation.name,\n\t\t\t\targs: mutation.args\n\t\t\t}))\n\t\t);\n\t};\n\n\tconst settlePending = (mutationId: number) => {\n\t\tconst index = pending.findIndex(\n\t\t\t(mutation) => mutation.mutationId === mutationId\n\t\t);\n\t\tif (index === -1) {\n\t\t\treturn undefined;\n\t\t}\n\t\tconst [mutation] = pending.splice(index, 1);\n\t\tpersist();\n\t\treturn mutation;\n\t};\n\n\tconst applyFrame = (frame: ServerFrame<T>) => {\n\t\tif (frame.type === 'snapshot') {\n\t\t\tconfirmed.clear();\n\t\t\tfor (const row of frame.rows) {\n\t\t\t\tconfirmed.set(key(row), row);\n\t\t\t}\n\t\t\tif (frame.version !== undefined) {\n\t\t\t\tappliedVersion = frame.version;\n\t\t\t}\n\t\t\trecompute({ status: 'ready', error: undefined });\n\t\t} else if (frame.type === 'diff') {\n\t\t\tfor (const row of frame.removed) {\n\t\t\t\tconfirmed.delete(key(row));\n\t\t\t}\n\t\t\tfor (const row of frame.added) {\n\t\t\t\tconfirmed.set(key(row), row);\n\t\t\t}\n\t\t\tfor (const row of frame.changed) {\n\t\t\t\tconfirmed.set(key(row), row);\n\t\t\t}\n\t\t\tif (frame.version !== undefined) {\n\t\t\t\tappliedVersion = Math.max(appliedVersion, frame.version);\n\t\t\t}\n\t\t\trecompute();\n\t\t} else if (frame.type === 'error') {\n\t\t\tsetState({ error: frame.message });\n\t\t\toptions.onError?.(frame.message);\n\t\t} else if (frame.type === 'ack') {\n\t\t\t// The authoritative diff already arrived (ordered before the ack), so\n\t\t\t// dropping the overlay leaves the confirmed row in place — no flicker.\n\t\t\tconst mutation = settlePending(frame.mutationId);\n\t\t\tif (mutation !== undefined) {\n\t\t\t\trecompute();\n\t\t\t\tmutation.resolve(frame.result);\n\t\t\t}\n\t\t} else if (frame.type === 'reject') {\n\t\t\t// roll the optimistic overlay back.\n\t\t\tconst mutation = settlePending(frame.mutationId);\n\t\t\tif (mutation !== undefined) {\n\t\t\t\trecompute();\n\t\t\t\tmutation.reject(new Error(String(frame.message)));\n\t\t\t}\n\t\t}\n\t\t// A `frame` (multi-collection batch) never reaches a single-collection\n\t\t// store — that's the multiplexed createSyncClient's job — so ignore it.\n\t};\n\n\tconst sendMutate = (mutation: PendingMutation<T>) => {\n\t\tif (connected) {\n\t\t\tsocket?.send(\n\t\t\t\tJSON.stringify({\n\t\t\t\t\ttype: 'mutate',\n\t\t\t\t\tmutationId: mutation.mutationId,\n\t\t\t\t\tname: mutation.name,\n\t\t\t\t\targs: mutation.args\n\t\t\t\t})\n\t\t\t);\n\t\t}\n\t};\n\n\tconst connect = () => {\n\t\tif (closed) {\n\t\t\treturn;\n\t\t}\n\t\tsetState({ status: 'connecting' });\n\t\tconst ws = new Impl(options.url);\n\t\tsocket = ws;\n\t\tws.onopen = () => {\n\t\t\tattempt = 0;\n\t\t\tconnected = true;\n\t\t\tws.send(\n\t\t\t\tJSON.stringify({\n\t\t\t\t\ttype: 'subscribe',\n\t\t\t\t\tid: SUBSCRIPTION_ID,\n\t\t\t\t\tcollection: options.collection,\n\t\t\t\t\tparams: options.params,\n\t\t\t\t\t// Resume from what we've applied (catch-up instead of snapshot).\n\t\t\t\t\tsince: appliedVersion > 0 ? appliedVersion : undefined\n\t\t\t\t})\n\t\t\t);\n\t\t\t// Replay anything still pending across the (re)connect.\n\t\t\tfor (const mutation of pending) {\n\t\t\t\tsendMutate(mutation);\n\t\t\t}\n\t\t};\n\t\tws.onmessage = (event) => {\n\t\t\ttry {\n\t\t\t\tapplyFrame(JSON.parse(event.data as string) as ServerFrame<T>);\n\t\t\t} catch {\n\t\t\t\t// ignore non-JSON frames\n\t\t\t}\n\t\t};\n\t\tws.onclose = () => {\n\t\t\tconnected = false;\n\t\t\tif (closed || reconnectMs <= 0) {\n\t\t\t\treturn;\n\t\t\t}\n\t\t\tconst delay = Math.min(reconnectMs * 2 ** attempt, maxReconnectMs);\n\t\t\tattempt += 1;\n\t\t\treconnectTimer = setTimeout(connect, delay);\n\t\t};\n\t};\n\n\tconnect();\n\n\t// Reload recovery: re-queue persisted unconfirmed mutations and replay them.\n\t// They carry no optimistic effect or promise (the fresh snapshot is\n\t// authoritative); resending produces the server diffs that bring them in.\n\tconst hydratePersisted = async () => {\n\t\tif (options.storage === undefined) {\n\t\t\treturn;\n\t\t}\n\t\tconst records = await options.storage.load();\n\t\tfor (const record of records) {\n\t\t\tif (pending.some((m) => m.mutationId === record.mutationId)) {\n\t\t\t\tcontinue;\n\t\t\t}\n\t\t\tpending.push({\n\t\t\t\tmutationId: record.mutationId,\n\t\t\t\tname: record.name,\n\t\t\t\targs: record.args,\n\t\t\t\tresolve: () => {},\n\t\t\t\treject: () => {}\n\t\t\t});\n\t\t\tmutationSeq = Math.max(mutationSeq, record.mutationId);\n\t\t}\n\t\tif (connected) {\n\t\t\tfor (const mutation of pending) {\n\t\t\t\tsendMutate(mutation);\n\t\t\t}\n\t\t}\n\t};\n\tvoid hydratePersisted();\n\n\treturn {\n\t\tget: () => state,\n\t\tsubscribe: (listener) => {\n\t\t\tlisteners.add(listener);\n\t\t\treturn () => {\n\t\t\t\tlisteners.delete(listener);\n\t\t\t};\n\t\t},\n\t\tmutate: <R = unknown>(mutateOptions: MutateOptions<T>) =>\n\t\t\tnew Promise<R>((resolve, reject) => {\n\t\t\t\tconst mutation: PendingMutation<T> = {\n\t\t\t\t\tmutationId: (mutationSeq += 1),\n\t\t\t\t\tname: mutateOptions.name,\n\t\t\t\t\targs: mutateOptions.args,\n\t\t\t\t\toptimistic: mutateOptions.optimistic,\n\t\t\t\t\tresolve: (result) => resolve(result as R),\n\t\t\t\t\treject\n\t\t\t\t};\n\t\t\t\tpending.push(mutation);\n\t\t\t\tpersist();\n\t\t\t\trecompute(); // apply the optimistic overlay immediately\n\t\t\t\tsendMutate(mutation);\n\t\t\t}),\n\t\tclose: () => {\n\t\t\tif (closed) {\n\t\t\t\treturn;\n\t\t\t}\n\t\t\tclosed = true;\n\t\t\tconnected = false;\n\t\t\tif (reconnectTimer !== undefined) {\n\t\t\t\tclearTimeout(reconnectTimer);\n\t\t\t}\n\t\t\ttry {\n\t\t\t\tsocket?.send(\n\t\t\t\t\tJSON.stringify({ type: 'unsubscribe', id: SUBSCRIPTION_ID })\n\t\t\t\t);\n\t\t\t\tsocket?.close();\n\t\t\t} catch {\n\t\t\t\t// socket already closing/closed\n\t\t\t}\n\t\t\t// Fail any still-pending mutations so their promises don't hang.\n\t\t\tfor (const mutation of pending.splice(0)) {\n\t\t\t\tmutation.reject(new Error('sync collection closed'));\n\t\t\t}\n\t\t\tpersist();\n\t\t\tsetState({ status: 'closed' });\n\t\t\tlisteners.clear();\n\t\t}\n\t};\n};\n",
|
|
9
|
+
"import type { PresenceMember } from '../engine/presence';\n\nexport type { PresenceMember } from '../engine/presence';\n\nexport type PresenceClientOptions<S> = {\n\t/** WebSocket URL of the {@link syncSocket} endpoint (e.g. `ws://host/sync/ws`). */\n\turl: string;\n\t/** Presence room to join (e.g. a document id or channel). */\n\troom: string;\n\t/** This member's initial state (e.g. `{ name, typing: false }`). */\n\tstate: S;\n\t/** Stable id for this member; defaults to a random one per client. */\n\tmemberId?: string;\n\t/** WebSocket implementation; defaults to the global one. */\n\twebSocketImpl?: typeof WebSocket;\n\t/** Initial reconnect backoff (ms); doubles per attempt. Defaults to 500. */\n\treconnectMs?: number;\n\t/** Max reconnect backoff (ms). Defaults to 10000. */\n\tmaxReconnectMs?: number;\n};\n\nexport type PresenceClient<S> = {\n\t/** This member's id. */\n\tid: string;\n\t/** Current members in the room (including this one). */\n\tget: () => PresenceMember<S>[];\n\t/** Subscribe to member changes; returns an unsubscribe. */\n\tsubscribe: (listener: (members: PresenceMember<S>[]) => void) => () => void;\n\t/** Update this member's own state (e.g. set `typing: true`). */\n\tset: (state: S) => void;\n\t/** Leave the room and close the socket. */\n\tclose: () => void;\n};\n\n/**\n * Browser client for {@link createPresenceHub} presence: join a room, see who's\n * present (and their state — typing, cursor…), and publish your own. Opens its\n * own small socket to the sync endpoint and re-joins on reconnect.\n * Framework-agnostic (`get` + `subscribe`, ready for `useSyncExternalStore`).\n */\nexport const createPresence = <S>(\n\toptions: PresenceClientOptions<S>\n): PresenceClient<S> => {\n\tconst reconnectMs = options.reconnectMs ?? 500;\n\tconst maxReconnectMs = options.maxReconnectMs ?? 10_000;\n\tconst Impl = options.webSocketImpl ?? globalThis.WebSocket;\n\tif (!Impl) {\n\t\tthrow new Error(\n\t\t\t'createPresence requires WebSocket. Run in a browser or pass webSocketImpl.'\n\t\t);\n\t}\n\tconst id =\n\t\toptions.memberId ??\n\t\tglobalThis.crypto?.randomUUID?.() ??\n\t\t`m${Math.random()}`;\n\n\tconst members = new Map<string, S>();\n\tlet state = options.state;\n\tlet snapshot: PresenceMember<S>[] = [];\n\tconst listeners = new Set<(members: PresenceMember<S>[]) => void>();\n\n\tconst emit = () => {\n\t\tsnapshot = [...members].map(([memberId, memberState]) => ({\n\t\t\tid: memberId,\n\t\t\tstate: memberState\n\t\t}));\n\t\tfor (const listener of listeners) {\n\t\t\tlistener(snapshot);\n\t\t}\n\t};\n\n\tlet socket: WebSocket | undefined;\n\tlet connected = false;\n\tlet closed = false;\n\tlet attempt = 0;\n\tlet reconnectTimer: ReturnType<typeof setTimeout> | undefined;\n\n\tconst send = (frame: unknown) => {\n\t\tif (connected) {\n\t\t\tsocket?.send(JSON.stringify(frame));\n\t\t}\n\t};\n\n\tconst connect = () => {\n\t\tif (closed) {\n\t\t\treturn;\n\t\t}\n\t\tconst ws = new Impl(options.url);\n\t\tsocket = ws;\n\t\tws.onopen = () => {\n\t\t\tattempt = 0;\n\t\t\tconnected = true;\n\t\t\tws.send(\n\t\t\t\tJSON.stringify({\n\t\t\t\t\ttype: 'presence-join',\n\t\t\t\t\troom: options.room,\n\t\t\t\t\tmemberId: id,\n\t\t\t\t\tstate\n\t\t\t\t})\n\t\t\t);\n\t\t};\n\t\tws.onmessage = (event) => {\n\t\t\tlet frame: {\n\t\t\t\ttype?: string;\n\t\t\t\troom?: string;\n\t\t\t\tjoined?: PresenceMember<S>[];\n\t\t\t\tupdated?: PresenceMember<S>[];\n\t\t\t\tleft?: string[];\n\t\t\t};\n\t\t\ttry {\n\t\t\t\tframe = JSON.parse(event.data as string);\n\t\t\t} catch {\n\t\t\t\treturn;\n\t\t\t}\n\t\t\tif (frame.type !== 'presence' || frame.room !== options.room) {\n\t\t\t\treturn;\n\t\t\t}\n\t\t\tfor (const member of frame.joined ?? []) {\n\t\t\t\tmembers.set(member.id, member.state);\n\t\t\t}\n\t\t\tfor (const member of frame.updated ?? []) {\n\t\t\t\tmembers.set(member.id, member.state);\n\t\t\t}\n\t\t\tfor (const memberId of frame.left ?? []) {\n\t\t\t\tmembers.delete(memberId);\n\t\t\t}\n\t\t\temit();\n\t\t};\n\t\tws.onclose = () => {\n\t\t\tconnected = false;\n\t\t\tif (closed || reconnectMs <= 0) {\n\t\t\t\treturn;\n\t\t\t}\n\t\t\tconst delay = Math.min(reconnectMs * 2 ** attempt, maxReconnectMs);\n\t\t\tattempt += 1;\n\t\t\treconnectTimer = setTimeout(connect, delay);\n\t\t};\n\t};\n\n\tconnect();\n\n\treturn {\n\t\tid,\n\t\tget: () => snapshot,\n\t\tsubscribe: (listener) => {\n\t\t\tlisteners.add(listener);\n\t\t\tlistener(snapshot);\n\n\t\t\treturn () => {\n\t\t\t\tlisteners.delete(listener);\n\t\t\t};\n\t\t},\n\t\tset: (next) => {\n\t\t\tstate = next;\n\t\t\tsend({ type: 'presence-set', room: options.room, state: next });\n\t\t},\n\t\tclose: () => {\n\t\t\tclosed = true;\n\t\t\tif (reconnectTimer !== undefined) {\n\t\t\t\tclearTimeout(reconnectTimer);\n\t\t\t}\n\t\t\tsend({ type: 'presence-leave', room: options.room });\n\t\t\tsocket?.close();\n\t\t\tmembers.clear();\n\t\t}\n\t};\n};\n",
|
|
10
|
+
"import type { ServerFrame } from '../engine/connection';\nimport type { RowKey } from '../engine/types';\nimport type {\n\tMutateOptions,\n\tOptimisticDraft,\n\tSyncCollectionState,\n\tSyncCollectionStatus\n} from './syncCollection';\n\nexport type SyncClientOptions = {\n\t/** WebSocket URL of the {@link syncSocket} endpoint (e.g. `ws://host/sync/ws`). */\n\turl: string;\n\t/** WebSocket implementation; defaults to the global one (pass for tests/SSR). */\n\twebSocketImpl?: typeof WebSocket;\n\t/** Initial reconnect backoff (ms); doubles per attempt. Defaults to 500. */\n\treconnectMs?: number;\n\t/** Max reconnect backoff (ms). Defaults to 10000. */\n\tmaxReconnectMs?: number;\n\t/** Called with the message of any server `error` frame. */\n\tonError?: (message: unknown) => void;\n};\n\nexport type SyncCollectionHandleOptions<T> = {\n\t/** Registered collection name to subscribe to. */\n\tcollection: string;\n\t/** Query params forwarded to the server collection. */\n\tparams?: unknown;\n\t/** Row identity. Defaults to `row.id`. */\n\tkey?: (row: T) => RowKey;\n};\n\nexport type SyncCollectionHandle<T> = {\n\t/** Current state snapshot (stable until the next change). */\n\tget: () => SyncCollectionState<T>;\n\t/** Subscribe to state changes; returns an unsubscribe. */\n\tsubscribe: (\n\t\tlistener: (state: SyncCollectionState<T>) => void\n\t) => () => void;\n\t/** Run a server mutation, optionally applying it optimistically. */\n\tmutate: <R = unknown>(options: MutateOptions<T>) => Promise<R>;\n\t/** Unsubscribe this collection (the socket stays open for others). */\n\tclose: () => void;\n};\n\nexport type SyncClient = {\n\t/** Subscribe to a collection over the shared socket. */\n\tcollection: <T>(\n\t\toptions: SyncCollectionHandleOptions<T>\n\t) => SyncCollectionHandle<T>;\n\t/** Close the socket and every handle. */\n\tclose: () => void;\n};\n\ntype PendingMutation = {\n\tmutationId: number;\n\tname: string;\n\targs: unknown;\n\toptimistic?: (draft: OptimisticDraft<unknown>) => void;\n\tresolve: (result: unknown) => void;\n\treject: (error: unknown) => void;\n};\n\ntype Entry = {\n\tid: string;\n\tcollection: string;\n\tparams: unknown;\n\tkey: (row: unknown) => RowKey;\n\tconfirmed: Map<RowKey, unknown>;\n\tpending: PendingMutation[];\n\tstate: SyncCollectionState<unknown>;\n\tlisteners: Set<(state: SyncCollectionState<unknown>) => void>;\n\tappliedVersion: number;\n\tclosed: boolean;\n};\n\n/**\n * A multiplexed sync client: one WebSocket serving many live collections. Its\n * reason to exist over per-collection {@link createSyncCollection} is the\n * **consistent frame** — when one atomic mutation touches several collections,\n * the server bundles the diffs into a single `frame` and this client applies\n * them all (to every collection's confirmed state) before notifying any\n * listener, so a view reading multiple collections never paints a torn\n * intermediate where one moved and the other hasn't.\n *\n * Reads: subscribe, apply snapshot then diffs/frames, resume on reconnect.\n * Writes: per-collection optimistic overlay, reconciled on ack/reject and\n * replayed on reconnect (make server mutations idempotent).\n */\nexport const createSyncClient = (options: SyncClientOptions): SyncClient => {\n\tconst reconnectMs = options.reconnectMs ?? 500;\n\tconst maxReconnectMs = options.maxReconnectMs ?? 10_000;\n\tconst Impl = options.webSocketImpl ?? globalThis.WebSocket;\n\tif (!Impl) {\n\t\tthrow new Error(\n\t\t\t'createSyncClient requires WebSocket. Run in a browser or pass webSocketImpl.'\n\t\t);\n\t}\n\n\tconst entries = new Map<string, Entry>();\n\tconst mutationOwner = new Map<number, Entry>();\n\tlet nextEntryId = 0;\n\tlet mutationSeq = 0;\n\n\tlet socket: WebSocket | undefined;\n\tlet connected = false;\n\tlet closed = false;\n\tlet attempt = 0;\n\tlet reconnectTimer: ReturnType<typeof setTimeout> | undefined;\n\n\tconst notify = (entry: Entry) => {\n\t\tfor (const listener of entry.listeners) {\n\t\t\tlistener(entry.state);\n\t\t}\n\t};\n\n\t/** Recompute one entry's visible state (confirmed + optimistic), no notify. */\n\tconst rebuild = (\n\t\tentry: Entry,\n\t\tpatch: Partial<SyncCollectionState<unknown>> = {}\n\t) => {\n\t\tconst working = new Map(entry.confirmed);\n\t\tconst draft: OptimisticDraft<unknown> = {\n\t\t\tset: (row) => working.set(entry.key(row), row),\n\t\t\tdelete: (rowKey) => working.delete(rowKey)\n\t\t};\n\t\tfor (const mutation of entry.pending) {\n\t\t\tmutation.optimistic?.(draft);\n\t\t}\n\t\tentry.state = {\n\t\t\t...entry.state,\n\t\t\t...patch,\n\t\t\tdata: [...working.values()]\n\t\t};\n\t};\n\n\tconst recompute = (\n\t\tentry: Entry,\n\t\tpatch: Partial<SyncCollectionState<unknown>> = {}\n\t) => {\n\t\trebuild(entry, patch);\n\t\tnotify(entry);\n\t};\n\n\tconst applyDiffToConfirmed = (\n\t\tentry: Entry,\n\t\tdiff: { added: unknown[]; removed: unknown[]; changed: unknown[] }\n\t) => {\n\t\tfor (const row of diff.removed) {\n\t\t\tentry.confirmed.delete(entry.key(row));\n\t\t}\n\t\tfor (const row of diff.added) {\n\t\t\tentry.confirmed.set(entry.key(row), row);\n\t\t}\n\t\tfor (const row of diff.changed) {\n\t\t\tentry.confirmed.set(entry.key(row), row);\n\t\t}\n\t};\n\n\tconst settlePending = (mutationId: number) => {\n\t\tconst entry = mutationOwner.get(mutationId);\n\t\tmutationOwner.delete(mutationId);\n\t\tif (entry === undefined) {\n\t\t\treturn undefined;\n\t\t}\n\t\tconst index = entry.pending.findIndex(\n\t\t\t(mutation) => mutation.mutationId === mutationId\n\t\t);\n\t\tif (index === -1) {\n\t\t\treturn undefined;\n\t\t}\n\t\tconst [mutation] = entry.pending.splice(index, 1);\n\t\treturn { entry, mutation: mutation! };\n\t};\n\n\tconst applyFrame = (frame: ServerFrame) => {\n\t\tif (frame.type === 'snapshot') {\n\t\t\tconst entry = entries.get(frame.id);\n\t\t\tif (entry === undefined) {\n\t\t\t\treturn;\n\t\t\t}\n\t\t\tentry.confirmed.clear();\n\t\t\tfor (const row of frame.rows) {\n\t\t\t\tentry.confirmed.set(entry.key(row), row);\n\t\t\t}\n\t\t\tif (frame.version !== undefined) {\n\t\t\t\tentry.appliedVersion = frame.version;\n\t\t\t}\n\t\t\trecompute(entry, { status: 'ready', error: undefined });\n\t\t} else if (frame.type === 'diff') {\n\t\t\tconst entry = entries.get(frame.id);\n\t\t\tif (entry === undefined) {\n\t\t\t\treturn;\n\t\t\t}\n\t\t\tapplyDiffToConfirmed(entry, frame);\n\t\t\tif (frame.version !== undefined) {\n\t\t\t\tentry.appliedVersion = Math.max(\n\t\t\t\t\tentry.appliedVersion,\n\t\t\t\t\tframe.version\n\t\t\t\t);\n\t\t\t}\n\t\t\trecompute(entry);\n\t\t} else if (frame.type === 'frame') {\n\t\t\t// The consistent frame: update every affected collection's confirmed\n\t\t\t// state first, then notify — so no listener observes a partial batch.\n\t\t\tconst affected = new Set<Entry>();\n\t\t\tfor (const diff of frame.diffs) {\n\t\t\t\tconst entry = entries.get(diff.id);\n\t\t\t\tif (entry === undefined) {\n\t\t\t\t\tcontinue;\n\t\t\t\t}\n\t\t\t\tapplyDiffToConfirmed(entry, diff);\n\t\t\t\tif (frame.version !== undefined) {\n\t\t\t\t\tentry.appliedVersion = Math.max(\n\t\t\t\t\t\tentry.appliedVersion,\n\t\t\t\t\t\tframe.version\n\t\t\t\t\t);\n\t\t\t\t}\n\t\t\t\t// Update state now, but defer notifying until every collection in\n\t\t\t\t// the frame is updated — so no listener observes a partial batch.\n\t\t\t\trebuild(entry);\n\t\t\t\taffected.add(entry);\n\t\t\t}\n\t\t\tfor (const entry of affected) {\n\t\t\t\tnotify(entry);\n\t\t\t}\n\t\t} else if (frame.type === 'error') {\n\t\t\tif (frame.id !== undefined) {\n\t\t\t\tconst entry = entries.get(frame.id);\n\t\t\t\tif (entry !== undefined) {\n\t\t\t\t\trecompute(entry, { error: frame.message });\n\t\t\t\t}\n\t\t\t}\n\t\t\toptions.onError?.(frame.message);\n\t\t} else if (frame.type === 'ack') {\n\t\t\tconst settled = settlePending(frame.mutationId);\n\t\t\tif (settled !== undefined) {\n\t\t\t\trecompute(settled.entry);\n\t\t\t\tsettled.mutation.resolve(frame.result);\n\t\t\t}\n\t\t} else if (frame.type === 'reject') {\n\t\t\tconst settled = settlePending(frame.mutationId);\n\t\t\tif (settled !== undefined) {\n\t\t\t\trecompute(settled.entry);\n\t\t\t\tsettled.mutation.reject(new Error(String(frame.message)));\n\t\t\t}\n\t\t}\n\t};\n\n\tconst sendSubscribe = (entry: Entry) => {\n\t\tsocket?.send(\n\t\t\tJSON.stringify({\n\t\t\t\ttype: 'subscribe',\n\t\t\t\tid: entry.id,\n\t\t\t\tcollection: entry.collection,\n\t\t\t\tparams: entry.params,\n\t\t\t\tsince:\n\t\t\t\t\tentry.appliedVersion > 0 ? entry.appliedVersion : undefined\n\t\t\t})\n\t\t);\n\t};\n\n\tconst sendMutate = (mutation: PendingMutation) => {\n\t\tif (connected) {\n\t\t\tsocket?.send(\n\t\t\t\tJSON.stringify({\n\t\t\t\t\ttype: 'mutate',\n\t\t\t\t\tmutationId: mutation.mutationId,\n\t\t\t\t\tname: mutation.name,\n\t\t\t\t\targs: mutation.args\n\t\t\t\t})\n\t\t\t);\n\t\t}\n\t};\n\n\tconst connect = () => {\n\t\tif (closed) {\n\t\t\treturn;\n\t\t}\n\t\tconst ws = new Impl(options.url);\n\t\tsocket = ws;\n\t\tws.onopen = () => {\n\t\t\tattempt = 0;\n\t\t\tconnected = true;\n\t\t\tfor (const entry of entries.values()) {\n\t\t\t\tsendSubscribe(entry);\n\t\t\t}\n\t\t\tfor (const entry of entries.values()) {\n\t\t\t\tfor (const mutation of entry.pending) {\n\t\t\t\t\tsendMutate(mutation);\n\t\t\t\t}\n\t\t\t}\n\t\t};\n\t\tws.onmessage = (event) => {\n\t\t\ttry {\n\t\t\t\tapplyFrame(JSON.parse(event.data as string) as ServerFrame);\n\t\t\t} catch {\n\t\t\t\t// ignore non-JSON frames\n\t\t\t}\n\t\t};\n\t\tws.onclose = () => {\n\t\t\tconnected = false;\n\t\t\tif (closed || reconnectMs <= 0) {\n\t\t\t\treturn;\n\t\t\t}\n\t\t\tconst delay = Math.min(reconnectMs * 2 ** attempt, maxReconnectMs);\n\t\t\tattempt += 1;\n\t\t\treconnectTimer = setTimeout(connect, delay);\n\t\t};\n\t};\n\n\tconnect();\n\n\tconst collection = <T>(\n\t\thandleOptions: SyncCollectionHandleOptions<T>\n\t): SyncCollectionHandle<T> => {\n\t\tconst entryId = `c${nextEntryId}`;\n\t\tnextEntryId += 1;\n\t\tconst entry: Entry = {\n\t\t\tid: entryId,\n\t\t\tcollection: handleOptions.collection,\n\t\t\tparams: handleOptions.params,\n\t\t\tkey:\n\t\t\t\t(handleOptions.key as ((row: unknown) => RowKey) | undefined) ??\n\t\t\t\t((row: unknown) => (row as { id: RowKey }).id),\n\t\t\tconfirmed: new Map(),\n\t\t\tpending: [],\n\t\t\tstate: { data: [], status: 'connecting', error: undefined },\n\t\t\tlisteners: new Set(),\n\t\t\tappliedVersion: 0,\n\t\t\tclosed: false\n\t\t};\n\t\tentries.set(entryId, entry);\n\t\tif (connected) {\n\t\t\tsendSubscribe(entry);\n\t\t}\n\n\t\treturn {\n\t\t\tget: () => entry.state as SyncCollectionState<T>,\n\t\t\tsubscribe: (listener) => {\n\t\t\t\tconst typed = listener as (\n\t\t\t\t\tstate: SyncCollectionState<unknown>\n\t\t\t\t) => void;\n\t\t\t\tentry.listeners.add(typed);\n\t\t\t\tlistener(entry.state as SyncCollectionState<T>);\n\t\t\t\treturn () => {\n\t\t\t\t\tentry.listeners.delete(typed);\n\t\t\t\t};\n\t\t\t},\n\t\t\tmutate: <R = unknown>(mutateOptions: MutateOptions<T>) =>\n\t\t\t\tnew Promise<R>((resolve, reject) => {\n\t\t\t\t\tmutationSeq += 1;\n\t\t\t\t\tconst mutation: PendingMutation = {\n\t\t\t\t\t\tmutationId: mutationSeq,\n\t\t\t\t\t\tname: mutateOptions.name,\n\t\t\t\t\t\targs: mutateOptions.args,\n\t\t\t\t\t\toptimistic: mutateOptions.optimistic as\n\t\t\t\t\t\t\t| ((draft: OptimisticDraft<unknown>) => void)\n\t\t\t\t\t\t\t| undefined,\n\t\t\t\t\t\tresolve: (result) => resolve(result as R),\n\t\t\t\t\t\treject\n\t\t\t\t\t};\n\t\t\t\t\tentry.pending.push(mutation);\n\t\t\t\t\tmutationOwner.set(mutation.mutationId, entry);\n\t\t\t\t\trecompute(entry);\n\t\t\t\t\tsendMutate(mutation);\n\t\t\t\t}),\n\t\t\tclose: () => {\n\t\t\t\tif (entry.closed) {\n\t\t\t\t\treturn;\n\t\t\t\t}\n\t\t\t\tentry.closed = true;\n\t\t\t\tentries.delete(entryId);\n\t\t\t\tif (connected) {\n\t\t\t\t\tsocket?.send(\n\t\t\t\t\t\tJSON.stringify({ type: 'unsubscribe', id: entryId })\n\t\t\t\t\t);\n\t\t\t\t}\n\t\t\t}\n\t\t};\n\t};\n\n\tconst close = () => {\n\t\tclosed = true;\n\t\tif (reconnectTimer !== undefined) {\n\t\t\tclearTimeout(reconnectTimer);\n\t\t}\n\t\tsocket?.close();\n\t\tentries.clear();\n\t\tmutationOwner.clear();\n\t};\n\n\treturn { collection, close };\n};\n\nexport type { SyncCollectionState, SyncCollectionStatus };\n",
|
|
11
|
+
"import type { ServerFrame } from '../engine/connection';\nimport type { RowKey } from '../engine/types';\nimport type { MutationStorage, PendingMutationRecord } from './syncCollection';\n\nexport type SyncStoreStatus = 'connecting' | 'ready' | 'closed';\n\nexport type SyncStoreState<Row> = {\n\t/** Visible rows: confirmed server state with pending optimistic edits applied. */\n\tdata: Row[];\n\tstatus: SyncStoreStatus;\n\terror: unknown;\n};\n\n/** A working set a mutation's optimistic effect edits in place. */\nexport type OptimisticDraft<Row> = {\n\tset: (row: Row) => void;\n\tdelete: (key: RowKey) => void;\n};\n\n/** A map of named server mutations — typically Eden calls. */\nexport type MutationMap = Record<string, (args: never) => Promise<unknown>>;\n\nexport type MutateOptions<Row> = {\n\t/** Apply the mutation's effect locally for instant UI (rolled back on reject). */\n\toptimistic?: (draft: OptimisticDraft<Row>) => void;\n};\n\nexport type SyncStoreOptions<Row, M extends MutationMap> = {\n\t/** WebSocket URL of the {@link syncSocket} endpoint. */\n\turl: string;\n\t/** Collection name to subscribe to for diffs. */\n\tcollection: string;\n\t/** Query params forwarded to the server collection. */\n\tparams?: unknown;\n\t/**\n\t * Typed read — typically an Eden call (`() => unwrapEden(api.sync.orders.get(...))`).\n\t * It is the source of the `Row` **type**, gives an eager first paint, and is\n\t * reusable for SSR. Live confirmed state then comes from the WS snapshot.\n\t */\n\thydrate?: () => Promise<Row[]>;\n\t/** Seed rows (e.g. from SSR); shown immediately, refreshed by the WS snapshot. */\n\tinitialData?: Row[];\n\t/** Typed server mutations (Eden calls). Enables `store.mutate(name, args, ...)`. */\n\tmutations?: M;\n\t/** Row identity. Defaults to `row.id`. */\n\tkey?: (row: Row) => RowKey;\n\twebSocketImpl?: typeof WebSocket;\n\treconnectMs?: number;\n\tmaxReconnectMs?: number;\n\t/**\n\t * After the server confirms a mutation, drop its optimistic overlay this long\n\t * after if no diff has reflected it (covers mutations that don't touch this\n\t * collection). Defaults to 3000.\n\t */\n\treconcileGraceMs?: number;\n\t/** Persist the pending-mutation queue across reloads (offline). */\n\tstorage?: MutationStorage;\n\tonError?: (error: unknown) => void;\n};\n\nexport type SyncStore<Row, M extends MutationMap> = {\n\tget: () => SyncStoreState<Row>;\n\tsubscribe: (listener: (state: SyncStoreState<Row>) => void) => () => void;\n\t/**\n\t * Run a named server mutation, optionally applying it optimistically. Resolves\n\t * with the server's result; rolls back and rejects if the server rejects it.\n\t * While offline (socket down) it stays queued and retries on reconnect.\n\t */\n\tmutate: <K extends keyof M>(\n\t\tname: K,\n\t\targs: Parameters<M[K]>[0],\n\t\toptions?: MutateOptions<Row>\n\t) => Promise<Awaited<ReturnType<M[K]>>>;\n\t/** Re-run `hydrate` and refresh confirmed state. */\n\trefetch: () => Promise<void>;\n\tclose: () => void;\n};\n\nconst SUBSCRIPTION_ID = 's';\n\ntype Pending<Row> = {\n\tid: number;\n\tname: string;\n\targs: unknown;\n\t/** Keys this mutation's optimistic effect touched, and how. */\n\ttouched: Map<RowKey, 'set' | 'delete'>;\n\toptimistic?: (draft: OptimisticDraft<Row>) => void;\n\tsettled: boolean;\n\tinFlight: boolean;\n\tresolve: (value: unknown) => void;\n\treject: (error: unknown) => void;\n\tgraceTimer?: ReturnType<typeof setTimeout>;\n};\n\n/**\n * A generic, Eden-fed live collection store (the typed Tier 3 client). Confirmed\n * state is maintained from the WS snapshot + diffs; mutations run over your typed\n * transport (Eden), apply an optimistic overlay, and reconcile against the diffs.\n * Types come entirely from the `hydrate`/`mutations` you pass — no `<T>`.\n */\nexport const syncStore = <Row, M extends MutationMap = MutationMap>(\n\toptions: SyncStoreOptions<Row, M>\n): SyncStore<Row, M> => {\n\tconst key = options.key ?? ((row: Row) => (row as { id: RowKey }).id);\n\tconst reconnectMs = options.reconnectMs ?? 500;\n\tconst maxReconnectMs = options.maxReconnectMs ?? 10_000;\n\tconst reconcileGraceMs = options.reconcileGraceMs ?? 3000;\n\tconst mutations = options.mutations ?? ({} as M);\n\tconst Impl = options.webSocketImpl ?? globalThis.WebSocket;\n\tif (!Impl) {\n\t\tthrow new Error(\n\t\t\t'syncStore requires WebSocket. Run in a browser or pass webSocketImpl.'\n\t\t);\n\t}\n\n\tconst confirmed = new Map<RowKey, Row>();\n\tconst pending: Pending<Row>[] = [];\n\tlet mutationSeq = 0;\n\n\tlet state: SyncStoreState<Row> = {\n\t\tdata: options.initialData ? [...options.initialData] : [],\n\t\tstatus: 'connecting',\n\t\terror: undefined\n\t};\n\tif (options.initialData) {\n\t\tfor (const row of options.initialData) {\n\t\t\tconfirmed.set(key(row), row);\n\t\t}\n\t}\n\n\tconst listeners = new Set<(state: SyncStoreState<Row>) => void>();\n\tconst setState = (patch: Partial<SyncStoreState<Row>>) => {\n\t\tstate = { ...state, ...patch };\n\t\tfor (const listener of listeners) {\n\t\t\tlistener(state);\n\t\t}\n\t};\n\tconst recompute = (patch: Partial<SyncStoreState<Row>> = {}) => {\n\t\tconst working = new Map(confirmed);\n\t\tconst draft: OptimisticDraft<Row> = {\n\t\t\tset: (row) => working.set(key(row), row),\n\t\t\tdelete: (rowKey) => working.delete(rowKey)\n\t\t};\n\t\tfor (const mutation of pending) {\n\t\t\tmutation.optimistic?.(draft);\n\t\t}\n\t\tsetState({ ...patch, data: [...working.values()] });\n\t};\n\n\tconst persist = () => {\n\t\tvoid options.storage?.save(\n\t\t\tpending.map((mutation) => ({\n\t\t\t\tmutationId: mutation.id,\n\t\t\t\tname: mutation.name,\n\t\t\t\targs: mutation.args\n\t\t\t}))\n\t\t);\n\t};\n\n\tconst dropPending = (mutation: Pending<Row>) => {\n\t\tconst index = pending.indexOf(mutation);\n\t\tif (index !== -1) {\n\t\t\tpending.splice(index, 1);\n\t\t}\n\t\tif (mutation.graceTimer !== undefined) {\n\t\t\tclearTimeout(mutation.graceTimer);\n\t\t}\n\t};\n\n\t/** Drop settled overlays whose touched keys are now reflected in confirmed. */\n\tconst reconcileSettled = () => {\n\t\tlet changed = false;\n\t\tfor (const mutation of [...pending]) {\n\t\t\tif (!mutation.settled) {\n\t\t\t\tcontinue;\n\t\t\t}\n\t\t\tlet reflected = true;\n\t\t\tfor (const [rowKey, kind] of mutation.touched) {\n\t\t\t\tconst present = confirmed.has(rowKey);\n\t\t\t\tif (kind === 'set' ? !present : present) {\n\t\t\t\t\treflected = false;\n\t\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\t}\n\t\t\tif (reflected) {\n\t\t\t\tdropPending(mutation);\n\t\t\t\tchanged = true;\n\t\t\t}\n\t\t}\n\t\tif (changed) {\n\t\t\trecompute();\n\t\t}\n\t};\n\n\tlet socket: WebSocket | undefined;\n\tlet connected = false;\n\tlet closed = false;\n\tlet attempt = 0;\n\tlet reconnectTimer: ReturnType<typeof setTimeout> | undefined;\n\t// Highest change-feed version applied; sent as `since` to resume on reconnect.\n\tlet appliedVersion = 0;\n\n\tconst applyFrame = (frame: ServerFrame<Row>) => {\n\t\tif (frame.type === 'snapshot') {\n\t\t\tconfirmed.clear();\n\t\t\tfor (const row of frame.rows) {\n\t\t\t\tconfirmed.set(key(row), row);\n\t\t\t}\n\t\t\tif (frame.version !== undefined) {\n\t\t\t\tappliedVersion = frame.version;\n\t\t\t}\n\t\t\trecompute({ status: 'ready', error: undefined });\n\t\t\treconcileSettled();\n\t\t} else if (frame.type === 'diff') {\n\t\t\tfor (const row of frame.removed) {\n\t\t\t\tconfirmed.delete(key(row));\n\t\t\t}\n\t\t\tfor (const row of frame.added) {\n\t\t\t\tconfirmed.set(key(row), row);\n\t\t\t}\n\t\t\tfor (const row of frame.changed) {\n\t\t\t\tconfirmed.set(key(row), row);\n\t\t\t}\n\t\t\tif (frame.version !== undefined) {\n\t\t\t\tappliedVersion = Math.max(appliedVersion, frame.version);\n\t\t\t}\n\t\t\trecompute();\n\t\t\treconcileSettled();\n\t\t} else if (frame.type === 'error') {\n\t\t\tsetState({ error: frame.message });\n\t\t\toptions.onError?.(frame.message);\n\t\t}\n\t\t// ack/reject frames are unused here — mutations run over the typed transport.\n\t};\n\n\tconst runMutation = async (mutation: Pending<Row>) => {\n\t\tif (mutation.inFlight || mutation.settled) {\n\t\t\treturn;\n\t\t}\n\t\tconst run = mutations[mutation.name];\n\t\tif (run === undefined) {\n\t\t\tdropPending(mutation);\n\t\t\trecompute();\n\t\t\tmutation.reject(new Error(`Unknown mutation \"${mutation.name}\"`));\n\t\t\treturn;\n\t\t}\n\t\tmutation.inFlight = true;\n\t\ttry {\n\t\t\tconst result = await (run as (args: unknown) => Promise<unknown>)(\n\t\t\t\tmutation.args\n\t\t\t);\n\t\t\tmutation.inFlight = false;\n\t\t\tmutation.settled = true;\n\t\t\tmutation.resolve(result);\n\t\t\tpersist();\n\t\t\treconcileSettled();\n\t\t\t// If the diff hasn't reflected it yet, drop the overlay after a grace.\n\t\t\tif (pending.includes(mutation)) {\n\t\t\t\tmutation.graceTimer = setTimeout(() => {\n\t\t\t\t\tdropPending(mutation);\n\t\t\t\t\trecompute();\n\t\t\t\t}, reconcileGraceMs);\n\t\t\t}\n\t\t} catch (error) {\n\t\t\tmutation.inFlight = false;\n\t\t\tif (connected) {\n\t\t\t\t// Server is reachable → a real rejection: roll back.\n\t\t\t\tdropPending(mutation);\n\t\t\t\trecompute();\n\t\t\t\tpersist();\n\t\t\t\tmutation.reject(error);\n\t\t\t} else {\n\t\t\t\t// Offline → keep queued; retry on reconnect.\n\t\t\t\toptions.onError?.(error);\n\t\t\t}\n\t\t}\n\t};\n\n\tconst connect = () => {\n\t\tif (closed) {\n\t\t\treturn;\n\t\t}\n\t\tsetState({ status: 'connecting' });\n\t\tconst ws = new Impl(options.url);\n\t\tsocket = ws;\n\t\tws.onopen = () => {\n\t\t\tattempt = 0;\n\t\t\tconnected = true;\n\t\t\tws.send(\n\t\t\t\tJSON.stringify({\n\t\t\t\t\ttype: 'subscribe',\n\t\t\t\t\tid: SUBSCRIPTION_ID,\n\t\t\t\t\tcollection: options.collection,\n\t\t\t\t\tparams: options.params,\n\t\t\t\t\t// Resume from what we've applied (catch-up instead of snapshot).\n\t\t\t\t\tsince: appliedVersion > 0 ? appliedVersion : undefined\n\t\t\t\t})\n\t\t\t);\n\t\t\t// Retry mutations that failed/queued while offline.\n\t\t\tfor (const mutation of pending) {\n\t\t\t\tif (!mutation.settled && !mutation.inFlight) {\n\t\t\t\t\tvoid runMutation(mutation);\n\t\t\t\t}\n\t\t\t}\n\t\t};\n\t\tws.onmessage = (event) => {\n\t\t\ttry {\n\t\t\t\tapplyFrame(\n\t\t\t\t\tJSON.parse(event.data as string) as ServerFrame<Row>\n\t\t\t\t);\n\t\t\t} catch {\n\t\t\t\t// ignore non-JSON frames\n\t\t\t}\n\t\t};\n\t\tws.onclose = () => {\n\t\t\tconnected = false;\n\t\t\tif (closed || reconnectMs <= 0) {\n\t\t\t\treturn;\n\t\t\t}\n\t\t\tconst delay = Math.min(reconnectMs * 2 ** attempt, maxReconnectMs);\n\t\t\tattempt += 1;\n\t\t\treconnectTimer = setTimeout(connect, delay);\n\t\t};\n\t};\n\n\tconst eagerHydrate = async () => {\n\t\tif (\n\t\t\toptions.hydrate === undefined ||\n\t\t\toptions.initialData !== undefined\n\t\t) {\n\t\t\treturn;\n\t\t}\n\t\ttry {\n\t\t\tconst rows = await options.hydrate();\n\t\t\t// Don't clobber a WS snapshot that already arrived.\n\t\t\tif (state.status !== 'ready') {\n\t\t\t\tconfirmed.clear();\n\t\t\t\tfor (const row of rows) {\n\t\t\t\t\tconfirmed.set(key(row), row);\n\t\t\t\t}\n\t\t\t\trecompute({ status: 'ready' });\n\t\t\t}\n\t\t} catch (error) {\n\t\t\toptions.onError?.(error);\n\t\t}\n\t};\n\n\tconst hydratePersisted = async () => {\n\t\tif (options.storage === undefined) {\n\t\t\treturn;\n\t\t}\n\t\tconst records = await options.storage.load();\n\t\tfor (const record of records as PendingMutationRecord[]) {\n\t\t\tif (pending.some((m) => m.id === record.mutationId)) {\n\t\t\t\tcontinue;\n\t\t\t}\n\t\t\tpending.push({\n\t\t\t\tid: record.mutationId,\n\t\t\t\tname: record.name,\n\t\t\t\targs: record.args,\n\t\t\t\ttouched: new Map(),\n\t\t\t\tsettled: false,\n\t\t\t\tinFlight: false,\n\t\t\t\tresolve: () => {},\n\t\t\t\treject: () => {}\n\t\t\t});\n\t\t\tmutationSeq = Math.max(mutationSeq, record.mutationId);\n\t\t}\n\t\tif (connected) {\n\t\t\tfor (const mutation of pending) {\n\t\t\t\tvoid runMutation(mutation);\n\t\t\t}\n\t\t}\n\t};\n\n\tconnect();\n\tvoid eagerHydrate();\n\tvoid hydratePersisted();\n\n\tconst collectTouched = (\n\t\toptimistic?: (draft: OptimisticDraft<Row>) => void\n\t) => {\n\t\tconst touched = new Map<RowKey, 'set' | 'delete'>();\n\t\toptimistic?.({\n\t\t\tset: (row) => touched.set(key(row), 'set'),\n\t\t\tdelete: (rowKey) => touched.set(rowKey, 'delete')\n\t\t});\n\t\treturn touched;\n\t};\n\n\treturn {\n\t\tget: () => state,\n\t\tsubscribe: (listener) => {\n\t\t\tlisteners.add(listener);\n\t\t\treturn () => {\n\t\t\t\tlisteners.delete(listener);\n\t\t\t};\n\t\t},\n\t\tmutate: ((name, args, mutateOptions) =>\n\t\t\tnew Promise((resolve, reject) => {\n\t\t\t\tconst mutation: Pending<Row> = {\n\t\t\t\t\tid: (mutationSeq += 1),\n\t\t\t\t\tname: name as string,\n\t\t\t\t\targs,\n\t\t\t\t\ttouched: collectTouched(mutateOptions?.optimistic),\n\t\t\t\t\toptimistic: mutateOptions?.optimistic,\n\t\t\t\t\tsettled: false,\n\t\t\t\t\tinFlight: false,\n\t\t\t\t\tresolve: resolve as (value: unknown) => void,\n\t\t\t\t\treject\n\t\t\t\t};\n\t\t\t\tpending.push(mutation);\n\t\t\t\tpersist();\n\t\t\t\trecompute();\n\t\t\t\tvoid runMutation(mutation);\n\t\t\t})) as SyncStore<Row, M>['mutate'],\n\t\trefetch: async () => {\n\t\t\tif (options.hydrate === undefined) {\n\t\t\t\treturn;\n\t\t\t}\n\t\t\tconst rows = await options.hydrate();\n\t\t\tconfirmed.clear();\n\t\t\tfor (const row of rows) {\n\t\t\t\tconfirmed.set(key(row), row);\n\t\t\t}\n\t\t\trecompute({ status: 'ready' });\n\t\t},\n\t\tclose: () => {\n\t\t\tif (closed) {\n\t\t\t\treturn;\n\t\t\t}\n\t\t\tclosed = true;\n\t\t\tconnected = false;\n\t\t\tif (reconnectTimer !== undefined) {\n\t\t\t\tclearTimeout(reconnectTimer);\n\t\t\t}\n\t\t\ttry {\n\t\t\t\tsocket?.send(\n\t\t\t\t\tJSON.stringify({ type: 'unsubscribe', id: SUBSCRIPTION_ID })\n\t\t\t\t);\n\t\t\t\tsocket?.close();\n\t\t\t} catch {\n\t\t\t\t// already closing\n\t\t\t}\n\t\t\tfor (const mutation of pending.splice(0)) {\n\t\t\t\tif (mutation.graceTimer !== undefined) {\n\t\t\t\t\tclearTimeout(mutation.graceTimer);\n\t\t\t\t}\n\t\t\t\tmutation.reject(new Error('sync store closed'));\n\t\t\t}\n\t\t\tpersist();\n\t\t\tsetState({ status: 'closed' });\n\t\t\tlisteners.clear();\n\t\t}\n\t};\n};\n\n/**\n * Unwrap an Eden treaty response (`{ data, error }`) to its data, throwing the\n * error. Use it to feed Eden calls to {@link syncStore}'s `hydrate`/`mutations`:\n * `hydrate: () => unwrapEden(api.sync.orders.get({ query }))`.\n */\nexport const unwrapEden = async <T>(\n\tresponse: Promise<{ data: T | null; error?: unknown }>\n): Promise<T> => {\n\tconst { data, error } = await response;\n\tif (error !== null && error !== undefined) {\n\t\tthrow error;\n\t}\n\treturn data as T;\n};\n"
|
|
6
12
|
],
|
|
7
|
-
"mappings": ";AAkCO,IAAM,uBAAuB;AAAA,EACnC;AAAA,EACA;AAAA,EACA,MAAM;AAAA,EACN;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,MAC4C;AAAA,EAC5C,MAAM,OAAO,mBAAmB,WAAW;AAAA,EAC3C,IAAI,CAAC,MAAM;AAAA,IACV,MAAM,IAAI,MACT,sFACD;AAAA,EACD;AAAA,EAEA,MAAM,SAAS,IAAI,gBAAgB,EAAE,QAAQ,OAAO,KAAK,GAAG,EAAE,CAAC;AAAA,EAC/D,MAAM,YAAY,IAAI,SAAS,GAAG,IAAI,MAAM;AAAA,EAC5C,MAAM,SAAS,IAAI,KAAK,GAAG,MAAM,YAAY,OAAO,SAAS,KAAK;AAAA,IACjE,iBAAiB,mBAAmB;AAAA,EACrC,CAAC;AAAA,EAED,OAAO,YAAY,CAAC,UAAU;AAAA,IAC7B,IAAI;AAAA,MACH,QAAQ,KAAK,MAAM,MAAM,IAAI,CAAkB;AAAA,MAC9C,MAAM;AAAA;AAAA,EAIT,IAAI,QAAQ;AAAA,IACX,OAAO,SAAS,MAAM,OAAO;AAAA,EAC9B;AAAA,EACA,IAAI,SAAS;AAAA,IACZ,OAAO,UAAU,CAAC,UAAU,QAAQ,KAAK;AAAA,EAC1C;AAAA,EAEA,OAAO;AAAA,IACN,OAAO,MA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,OAAO,CAAC,GAAG;AAAA,QACzC,IAAI,SAAS,eAAe,WAAW;AAAA,UACtC,aAAa,SAAS,UAAU;AAAA,QACjC;AAAA,QACA,SAAS,OAAO,IAAI,MAAM,mBAAmB,CAAC;AAAA,MAC/C;AAAA,MACA,QAAQ;AAAA,MACR,SAAS,EAAE,QAAQ,SAAS,CAAC;AAAA,MAC7B,UAAU,MAAM;AAAA;AAAA,EAElB;AAAA;AAQM,IAAM,aAAa,OACzB,aACgB;AAAA,EAChB,QAAQ,MAAM,UAAU,MAAM;AAAA,EAC9B,IAAI,UAAU,QAAQ,UAAU,WAAW;AAAA,IAC1C,MAAM;AAAA,EACP;AAAA,EACA,OAAO;AAAA;",
|
|
14
|
+
"debugId": "E83DAC6C389DAA6A64756E2164756E21",
|
|
9
15
|
"names": []
|
|
10
16
|
}
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
export type LiveQueryState<T> = {
|
|
2
|
+
/** Latest query result, or `undefined` before the first successful fetch. */
|
|
3
|
+
data: T | undefined;
|
|
4
|
+
/** Error from the most recent fetch, or `undefined` if it succeeded. */
|
|
5
|
+
error: unknown;
|
|
6
|
+
/** `true` until the first result arrives (no data yet). */
|
|
7
|
+
loading: boolean;
|
|
8
|
+
/** `true` while a (re)fetch is in flight — data may still be present. */
|
|
9
|
+
fetching: boolean;
|
|
10
|
+
};
|
|
11
|
+
export type LiveQueryOptions<T> = {
|
|
12
|
+
/**
|
|
13
|
+
* Topics this query depends on — typically the server's
|
|
14
|
+
* `deriveReadTopics(...).topics`. Any event on one of them triggers a
|
|
15
|
+
* refetch. A trailing `*` matches by prefix server-side.
|
|
16
|
+
*/
|
|
17
|
+
topics: string[];
|
|
18
|
+
/**
|
|
19
|
+
* Runs the read and resolves the query result. Receives an `AbortSignal`
|
|
20
|
+
* that fires when a newer fetch supersedes this one or the query is closed.
|
|
21
|
+
*/
|
|
22
|
+
fetcher: (signal: AbortSignal) => Promise<T>;
|
|
23
|
+
/** SSE endpoint mounted by the {@link sync} plugin. Defaults to `/sync`. */
|
|
24
|
+
url?: string;
|
|
25
|
+
/** Send cookies with the SSE request (cross-origin auth). */
|
|
26
|
+
withCredentials?: boolean;
|
|
27
|
+
/** EventSource implementation; defaults to the global one. */
|
|
28
|
+
eventSourceImpl?: typeof EventSource;
|
|
29
|
+
/**
|
|
30
|
+
* Seed data (e.g. from SSR). When provided, the initial fetch is skipped —
|
|
31
|
+
* the query trusts this until an event or a manual {@link LiveQuery.refetch}.
|
|
32
|
+
*/
|
|
33
|
+
initialData?: T;
|
|
34
|
+
/**
|
|
35
|
+
* Skip the initial fetch and stay idle until the first event or a manual
|
|
36
|
+
* refetch. Reconnects still re-hydrate.
|
|
37
|
+
*/
|
|
38
|
+
manual?: boolean;
|
|
39
|
+
/**
|
|
40
|
+
* Coalesce a burst of events into one refetch within this window (ms).
|
|
41
|
+
* Defaults to 0 — refetch once per event.
|
|
42
|
+
*/
|
|
43
|
+
debounceMs?: number;
|
|
44
|
+
/** Called when a fetch rejects (stale data is retained). */
|
|
45
|
+
onError?: (error: unknown) => void;
|
|
46
|
+
};
|
|
47
|
+
export type LiveQuery<T> = {
|
|
48
|
+
/** Current state snapshot (stable reference until the next change). */
|
|
49
|
+
get: () => LiveQueryState<T>;
|
|
50
|
+
/** Subscribe to state changes; returns an unsubscribe. */
|
|
51
|
+
subscribe: (listener: (state: LiveQueryState<T>) => void) => () => void;
|
|
52
|
+
/** Force a refetch now. Resolves when this fetch settles. */
|
|
53
|
+
refetch: () => Promise<void>;
|
|
54
|
+
/** Stop the SSE subscription, cancel any in-flight fetch, drop listeners. */
|
|
55
|
+
close: () => void;
|
|
56
|
+
};
|
|
57
|
+
/**
|
|
58
|
+
* A live, self-refreshing query: hydrate once via `fetcher`, then refetch
|
|
59
|
+
* whenever the server publishes one of `topics` — the read half of Tier 2,
|
|
60
|
+
* built on {@link createSyncSubscriber}. Framework-agnostic: `get` + `subscribe`
|
|
61
|
+
* plug straight into React's `useSyncExternalStore` or any equivalent.
|
|
62
|
+
*
|
|
63
|
+
* Pair it with the Drizzle adapter's `deriveReadTopics` (server) and
|
|
64
|
+
* `publishChange`/`publishWhere` (mutations) so a write invalidates exactly the
|
|
65
|
+
* queries that read the changed rows.
|
|
66
|
+
*/
|
|
67
|
+
export declare const createLiveQuery: <T>(options: LiveQueryOptions<T>) => LiveQuery<T>;
|
|
68
|
+
/**
|
|
69
|
+
* A small default `fetcher`: GET `url` and parse JSON. Forwards the live query's
|
|
70
|
+
* abort signal and throws on a non-2xx response.
|
|
71
|
+
*
|
|
72
|
+
* @example
|
|
73
|
+
* createLiveQuery({ topics, fetcher: jsonFetcher<User[]>('/api/users') })
|
|
74
|
+
*/
|
|
75
|
+
export declare const jsonFetcher: <T>(url: string, init?: RequestInit) => (signal: AbortSignal) => Promise<T>;
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import type { PresenceMember } from '../engine/presence';
|
|
2
|
+
export type { PresenceMember } from '../engine/presence';
|
|
3
|
+
export type PresenceClientOptions<S> = {
|
|
4
|
+
/** WebSocket URL of the {@link syncSocket} endpoint (e.g. `ws://host/sync/ws`). */
|
|
5
|
+
url: string;
|
|
6
|
+
/** Presence room to join (e.g. a document id or channel). */
|
|
7
|
+
room: string;
|
|
8
|
+
/** This member's initial state (e.g. `{ name, typing: false }`). */
|
|
9
|
+
state: S;
|
|
10
|
+
/** Stable id for this member; defaults to a random one per client. */
|
|
11
|
+
memberId?: string;
|
|
12
|
+
/** WebSocket implementation; defaults to the global one. */
|
|
13
|
+
webSocketImpl?: typeof WebSocket;
|
|
14
|
+
/** Initial reconnect backoff (ms); doubles per attempt. Defaults to 500. */
|
|
15
|
+
reconnectMs?: number;
|
|
16
|
+
/** Max reconnect backoff (ms). Defaults to 10000. */
|
|
17
|
+
maxReconnectMs?: number;
|
|
18
|
+
};
|
|
19
|
+
export type PresenceClient<S> = {
|
|
20
|
+
/** This member's id. */
|
|
21
|
+
id: string;
|
|
22
|
+
/** Current members in the room (including this one). */
|
|
23
|
+
get: () => PresenceMember<S>[];
|
|
24
|
+
/** Subscribe to member changes; returns an unsubscribe. */
|
|
25
|
+
subscribe: (listener: (members: PresenceMember<S>[]) => void) => () => void;
|
|
26
|
+
/** Update this member's own state (e.g. set `typing: true`). */
|
|
27
|
+
set: (state: S) => void;
|
|
28
|
+
/** Leave the room and close the socket. */
|
|
29
|
+
close: () => void;
|
|
30
|
+
};
|
|
31
|
+
/**
|
|
32
|
+
* Browser client for {@link createPresenceHub} presence: join a room, see who's
|
|
33
|
+
* present (and their state — typing, cursor…), and publish your own. Opens its
|
|
34
|
+
* own small socket to the sync endpoint and re-joins on reconnect.
|
|
35
|
+
* Framework-agnostic (`get` + `subscribe`, ready for `useSyncExternalStore`).
|
|
36
|
+
*/
|
|
37
|
+
export declare const createPresence: <S>(options: PresenceClientOptions<S>) => PresenceClient<S>;
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import type { ReactiveEvent } from '../reactiveHub';
|
|
2
|
+
export type SyncSubscriberOptions = {
|
|
3
|
+
/** Topics to subscribe to. A trailing `*` matches by prefix server-side. */
|
|
4
|
+
topics: string[];
|
|
5
|
+
/** Called for every reactive event pushed from the server. */
|
|
6
|
+
onEvent: (event: ReactiveEvent) => void;
|
|
7
|
+
/** SSE endpoint mounted by the {@link sync} plugin. Defaults to `/sync`. */
|
|
8
|
+
url?: string;
|
|
9
|
+
onOpen?: () => void;
|
|
10
|
+
onError?: (event: Event) => void;
|
|
11
|
+
/** Send cookies with the SSE request (cross-origin auth). */
|
|
12
|
+
withCredentials?: boolean;
|
|
13
|
+
/**
|
|
14
|
+
* EventSource implementation to use. Defaults to the global one; pass a polyfill
|
|
15
|
+
* for non-browser runtimes.
|
|
16
|
+
*/
|
|
17
|
+
eventSourceImpl?: typeof EventSource;
|
|
18
|
+
};
|
|
19
|
+
export type SyncSubscriber = {
|
|
20
|
+
close: () => void;
|
|
21
|
+
/** The underlying EventSource, for advanced listeners. */
|
|
22
|
+
source: EventSource;
|
|
23
|
+
};
|
|
24
|
+
/**
|
|
25
|
+
* Subscribe a browser to the server's {@link ReactiveHub} over SSE. `onEvent` fires
|
|
26
|
+
* whenever a subscribed topic is published — the cue to refetch (or read the pushed
|
|
27
|
+
* payload) instead of polling. EventSource reconnects automatically on transient
|
|
28
|
+
* network drops.
|
|
29
|
+
*/
|
|
30
|
+
export declare const createSyncSubscriber: ({ topics, onEvent, url, onOpen, onError, withCredentials, eventSourceImpl }: SyncSubscriberOptions) => SyncSubscriber;
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import type { RowKey } from '../engine/types';
|
|
2
|
+
import type { MutateOptions, SyncCollectionState, SyncCollectionStatus } from './syncCollection';
|
|
3
|
+
export type SyncClientOptions = {
|
|
4
|
+
/** WebSocket URL of the {@link syncSocket} endpoint (e.g. `ws://host/sync/ws`). */
|
|
5
|
+
url: string;
|
|
6
|
+
/** WebSocket implementation; defaults to the global one (pass for tests/SSR). */
|
|
7
|
+
webSocketImpl?: typeof WebSocket;
|
|
8
|
+
/** Initial reconnect backoff (ms); doubles per attempt. Defaults to 500. */
|
|
9
|
+
reconnectMs?: number;
|
|
10
|
+
/** Max reconnect backoff (ms). Defaults to 10000. */
|
|
11
|
+
maxReconnectMs?: number;
|
|
12
|
+
/** Called with the message of any server `error` frame. */
|
|
13
|
+
onError?: (message: unknown) => void;
|
|
14
|
+
};
|
|
15
|
+
export type SyncCollectionHandleOptions<T> = {
|
|
16
|
+
/** Registered collection name to subscribe to. */
|
|
17
|
+
collection: string;
|
|
18
|
+
/** Query params forwarded to the server collection. */
|
|
19
|
+
params?: unknown;
|
|
20
|
+
/** Row identity. Defaults to `row.id`. */
|
|
21
|
+
key?: (row: T) => RowKey;
|
|
22
|
+
};
|
|
23
|
+
export type SyncCollectionHandle<T> = {
|
|
24
|
+
/** Current state snapshot (stable until the next change). */
|
|
25
|
+
get: () => SyncCollectionState<T>;
|
|
26
|
+
/** Subscribe to state changes; returns an unsubscribe. */
|
|
27
|
+
subscribe: (listener: (state: SyncCollectionState<T>) => void) => () => void;
|
|
28
|
+
/** Run a server mutation, optionally applying it optimistically. */
|
|
29
|
+
mutate: <R = unknown>(options: MutateOptions<T>) => Promise<R>;
|
|
30
|
+
/** Unsubscribe this collection (the socket stays open for others). */
|
|
31
|
+
close: () => void;
|
|
32
|
+
};
|
|
33
|
+
export type SyncClient = {
|
|
34
|
+
/** Subscribe to a collection over the shared socket. */
|
|
35
|
+
collection: <T>(options: SyncCollectionHandleOptions<T>) => SyncCollectionHandle<T>;
|
|
36
|
+
/** Close the socket and every handle. */
|
|
37
|
+
close: () => void;
|
|
38
|
+
};
|
|
39
|
+
/**
|
|
40
|
+
* A multiplexed sync client: one WebSocket serving many live collections. Its
|
|
41
|
+
* reason to exist over per-collection {@link createSyncCollection} is the
|
|
42
|
+
* **consistent frame** — when one atomic mutation touches several collections,
|
|
43
|
+
* the server bundles the diffs into a single `frame` and this client applies
|
|
44
|
+
* them all (to every collection's confirmed state) before notifying any
|
|
45
|
+
* listener, so a view reading multiple collections never paints a torn
|
|
46
|
+
* intermediate where one moved and the other hasn't.
|
|
47
|
+
*
|
|
48
|
+
* Reads: subscribe, apply snapshot then diffs/frames, resume on reconnect.
|
|
49
|
+
* Writes: per-collection optimistic overlay, reconciled on ack/reject and
|
|
50
|
+
* replayed on reconnect (make server mutations idempotent).
|
|
51
|
+
*/
|
|
52
|
+
export declare const createSyncClient: (options: SyncClientOptions) => SyncClient;
|
|
53
|
+
export type { SyncCollectionState, SyncCollectionStatus };
|
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
import type { RowKey } from '../engine/types';
|
|
2
|
+
export type { ServerFrame } from '../engine/connection';
|
|
3
|
+
export type SyncCollectionStatus = 'connecting' | 'ready' | 'closed';
|
|
4
|
+
export type SyncCollectionState<T> = {
|
|
5
|
+
/** Visible rows: the server state with pending optimistic mutations applied. */
|
|
6
|
+
data: T[];
|
|
7
|
+
/** Connection/sync status. */
|
|
8
|
+
status: SyncCollectionStatus;
|
|
9
|
+
/** Last error message from the server, or `undefined`. */
|
|
10
|
+
error: unknown;
|
|
11
|
+
};
|
|
12
|
+
/** A working set a mutation's optimistic effect edits in place. */
|
|
13
|
+
export type OptimisticDraft<T> = {
|
|
14
|
+
/** Insert or replace a row by key. */
|
|
15
|
+
set: (row: T) => void;
|
|
16
|
+
/** Remove a row by key. */
|
|
17
|
+
delete: (key: RowKey) => void;
|
|
18
|
+
};
|
|
19
|
+
export type MutateOptions<T> = {
|
|
20
|
+
/** Registered server mutation name. */
|
|
21
|
+
name: string;
|
|
22
|
+
/** Arguments forwarded to the mutation handler. */
|
|
23
|
+
args?: unknown;
|
|
24
|
+
/**
|
|
25
|
+
* Apply this mutation's effect to the local set immediately for instant UI.
|
|
26
|
+
* Reverted automatically if the server rejects it. Omit for a non-optimistic
|
|
27
|
+
* mutation (UI updates only once the authoritative diff arrives).
|
|
28
|
+
*/
|
|
29
|
+
optimistic?: (draft: OptimisticDraft<T>) => void;
|
|
30
|
+
};
|
|
31
|
+
/** A pending mutation persisted for replay across reloads. */
|
|
32
|
+
export type PendingMutationRecord = {
|
|
33
|
+
mutationId: number;
|
|
34
|
+
name: string;
|
|
35
|
+
args: unknown;
|
|
36
|
+
};
|
|
37
|
+
/**
|
|
38
|
+
* Durable storage for the pending-mutation queue, so unconfirmed mutations
|
|
39
|
+
* survive a page reload (offline). The queue is replayed when the socket
|
|
40
|
+
* connects; records are dropped as they're acked.
|
|
41
|
+
*/
|
|
42
|
+
export type MutationStorage = {
|
|
43
|
+
load: () => PendingMutationRecord[] | Promise<PendingMutationRecord[]>;
|
|
44
|
+
save: (records: PendingMutationRecord[]) => void | Promise<void>;
|
|
45
|
+
};
|
|
46
|
+
/**
|
|
47
|
+
* A {@link MutationStorage} backed by `localStorage` under `key`. No-ops where
|
|
48
|
+
* `localStorage` is unavailable (e.g. SSR).
|
|
49
|
+
*/
|
|
50
|
+
export declare const localStorageMutationStorage: (key: string) => MutationStorage;
|
|
51
|
+
export type SyncCollectionOptions<T> = {
|
|
52
|
+
/** WebSocket URL of the {@link syncSocket} endpoint (e.g. `ws://host/sync/ws`). */
|
|
53
|
+
url: string;
|
|
54
|
+
/** Registered collection name to subscribe to. */
|
|
55
|
+
collection: string;
|
|
56
|
+
/** Query params forwarded to the server collection's hydrate/match/authorize. */
|
|
57
|
+
params?: unknown;
|
|
58
|
+
/** Row identity, used to apply diffs and optimistic edits. Defaults to `row.id`. */
|
|
59
|
+
key?: (row: T) => RowKey;
|
|
60
|
+
/** WebSocket implementation; defaults to the global one (pass for tests/SSR). */
|
|
61
|
+
webSocketImpl?: typeof WebSocket;
|
|
62
|
+
/**
|
|
63
|
+
* Base reconnect delay (ms), doubled each attempt up to `maxReconnectMs`.
|
|
64
|
+
* Set 0 to disable auto-reconnect. Defaults to 500.
|
|
65
|
+
*/
|
|
66
|
+
reconnectMs?: number;
|
|
67
|
+
/** Maximum reconnect backoff (ms). Defaults to 10000. */
|
|
68
|
+
maxReconnectMs?: number;
|
|
69
|
+
/**
|
|
70
|
+
* Persist the pending-mutation queue so it survives a reload (offline) and
|
|
71
|
+
* replays on connect. See {@link localStorageMutationStorage}.
|
|
72
|
+
*/
|
|
73
|
+
storage?: MutationStorage;
|
|
74
|
+
/** Called with each server error message. */
|
|
75
|
+
onError?: (error: unknown) => void;
|
|
76
|
+
};
|
|
77
|
+
export type SyncCollection<T> = {
|
|
78
|
+
/** Current state snapshot (stable reference until the next change). */
|
|
79
|
+
get: () => SyncCollectionState<T>;
|
|
80
|
+
/** Subscribe to state changes; returns an unsubscribe. */
|
|
81
|
+
subscribe: (listener: (state: SyncCollectionState<T>) => void) => () => void;
|
|
82
|
+
/**
|
|
83
|
+
* Run a server mutation, optionally applying it optimistically. Resolves with
|
|
84
|
+
* the server's result on ack, rejects (and rolls back) on reject. Pending
|
|
85
|
+
* mutations are replayed when the socket reconnects, so they survive a drop.
|
|
86
|
+
*/
|
|
87
|
+
mutate: <R = unknown>(options: MutateOptions<T>) => Promise<R>;
|
|
88
|
+
/** Unsubscribe on the server, close the socket, and stop reconnecting. */
|
|
89
|
+
close: () => void;
|
|
90
|
+
};
|
|
91
|
+
/**
|
|
92
|
+
* A live collection backed by the WebSocket sync engine. Reads: connect,
|
|
93
|
+
* subscribe, apply the server's snapshot then row-level diffs, re-sync on
|
|
94
|
+
* reconnect. Writes: {@link SyncCollection.mutate} applies an optimistic overlay
|
|
95
|
+
* immediately, sends the mutation, and reconciles on ack (drop the overlay — the
|
|
96
|
+
* authoritative diff already arrived) or reject (roll back). Framework-agnostic
|
|
97
|
+
* (`get` + `subscribe`).
|
|
98
|
+
*
|
|
99
|
+
* Mutations are replayed on reconnect, so make server mutations idempotent —
|
|
100
|
+
* delivery is at-least-once if an ack is lost across a drop.
|
|
101
|
+
*/
|
|
102
|
+
export declare const createSyncCollection: <T>(options: SyncCollectionOptions<T>) => SyncCollection<T>;
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
import type { RowKey } from '../engine/types';
|
|
2
|
+
import type { MutationStorage } from './syncCollection';
|
|
3
|
+
export type SyncStoreStatus = 'connecting' | 'ready' | 'closed';
|
|
4
|
+
export type SyncStoreState<Row> = {
|
|
5
|
+
/** Visible rows: confirmed server state with pending optimistic edits applied. */
|
|
6
|
+
data: Row[];
|
|
7
|
+
status: SyncStoreStatus;
|
|
8
|
+
error: unknown;
|
|
9
|
+
};
|
|
10
|
+
/** A working set a mutation's optimistic effect edits in place. */
|
|
11
|
+
export type OptimisticDraft<Row> = {
|
|
12
|
+
set: (row: Row) => void;
|
|
13
|
+
delete: (key: RowKey) => void;
|
|
14
|
+
};
|
|
15
|
+
/** A map of named server mutations — typically Eden calls. */
|
|
16
|
+
export type MutationMap = Record<string, (args: never) => Promise<unknown>>;
|
|
17
|
+
export type MutateOptions<Row> = {
|
|
18
|
+
/** Apply the mutation's effect locally for instant UI (rolled back on reject). */
|
|
19
|
+
optimistic?: (draft: OptimisticDraft<Row>) => void;
|
|
20
|
+
};
|
|
21
|
+
export type SyncStoreOptions<Row, M extends MutationMap> = {
|
|
22
|
+
/** WebSocket URL of the {@link syncSocket} endpoint. */
|
|
23
|
+
url: string;
|
|
24
|
+
/** Collection name to subscribe to for diffs. */
|
|
25
|
+
collection: string;
|
|
26
|
+
/** Query params forwarded to the server collection. */
|
|
27
|
+
params?: unknown;
|
|
28
|
+
/**
|
|
29
|
+
* Typed read — typically an Eden call (`() => unwrapEden(api.sync.orders.get(...))`).
|
|
30
|
+
* It is the source of the `Row` **type**, gives an eager first paint, and is
|
|
31
|
+
* reusable for SSR. Live confirmed state then comes from the WS snapshot.
|
|
32
|
+
*/
|
|
33
|
+
hydrate?: () => Promise<Row[]>;
|
|
34
|
+
/** Seed rows (e.g. from SSR); shown immediately, refreshed by the WS snapshot. */
|
|
35
|
+
initialData?: Row[];
|
|
36
|
+
/** Typed server mutations (Eden calls). Enables `store.mutate(name, args, ...)`. */
|
|
37
|
+
mutations?: M;
|
|
38
|
+
/** Row identity. Defaults to `row.id`. */
|
|
39
|
+
key?: (row: Row) => RowKey;
|
|
40
|
+
webSocketImpl?: typeof WebSocket;
|
|
41
|
+
reconnectMs?: number;
|
|
42
|
+
maxReconnectMs?: number;
|
|
43
|
+
/**
|
|
44
|
+
* After the server confirms a mutation, drop its optimistic overlay this long
|
|
45
|
+
* after if no diff has reflected it (covers mutations that don't touch this
|
|
46
|
+
* collection). Defaults to 3000.
|
|
47
|
+
*/
|
|
48
|
+
reconcileGraceMs?: number;
|
|
49
|
+
/** Persist the pending-mutation queue across reloads (offline). */
|
|
50
|
+
storage?: MutationStorage;
|
|
51
|
+
onError?: (error: unknown) => void;
|
|
52
|
+
};
|
|
53
|
+
export type SyncStore<Row, M extends MutationMap> = {
|
|
54
|
+
get: () => SyncStoreState<Row>;
|
|
55
|
+
subscribe: (listener: (state: SyncStoreState<Row>) => void) => () => void;
|
|
56
|
+
/**
|
|
57
|
+
* Run a named server mutation, optionally applying it optimistically. Resolves
|
|
58
|
+
* with the server's result; rolls back and rejects if the server rejects it.
|
|
59
|
+
* While offline (socket down) it stays queued and retries on reconnect.
|
|
60
|
+
*/
|
|
61
|
+
mutate: <K extends keyof M>(name: K, args: Parameters<M[K]>[0], options?: MutateOptions<Row>) => Promise<Awaited<ReturnType<M[K]>>>;
|
|
62
|
+
/** Re-run `hydrate` and refresh confirmed state. */
|
|
63
|
+
refetch: () => Promise<void>;
|
|
64
|
+
close: () => void;
|
|
65
|
+
};
|
|
66
|
+
/**
|
|
67
|
+
* A generic, Eden-fed live collection store (the typed Tier 3 client). Confirmed
|
|
68
|
+
* state is maintained from the WS snapshot + diffs; mutations run over your typed
|
|
69
|
+
* transport (Eden), apply an optimistic overlay, and reconcile against the diffs.
|
|
70
|
+
* Types come entirely from the `hydrate`/`mutations` you pass — no `<T>`.
|
|
71
|
+
*/
|
|
72
|
+
export declare const syncStore: <Row, M extends MutationMap = MutationMap>(options: SyncStoreOptions<Row, M>) => SyncStore<Row, M>;
|
|
73
|
+
/**
|
|
74
|
+
* Unwrap an Eden treaty response (`{ data, error }`) to its data, throwing the
|
|
75
|
+
* error. Use it to feed Eden calls to {@link syncStore}'s `hydrate`/`mutations`:
|
|
76
|
+
* `hydrate: () => unwrapEden(api.sync.orders.get({ query }))`.
|
|
77
|
+
*/
|
|
78
|
+
export declare const unwrapEden: <T>(response: Promise<{
|
|
79
|
+
data: T | null;
|
|
80
|
+
error?: unknown;
|
|
81
|
+
}>) => Promise<T>;
|