experimental-a2 0.9.0 → 0.11.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.
Files changed (123) hide show
  1. package/CHANGELOG.md +53 -0
  2. package/dist/actor-D_54lz_1.d.ts +310 -0
  3. package/dist/actor-D_54lz_1.d.ts.map +1 -0
  4. package/dist/actor-client.d.ts +13 -4
  5. package/dist/actor-client.d.ts.map +1 -1
  6. package/dist/actor-client.js +63 -7
  7. package/dist/actor-client.js.map +1 -1
  8. package/dist/actor-react.d.ts +5 -4
  9. package/dist/actor-react.d.ts.map +1 -1
  10. package/dist/actor-react.js +16 -2
  11. package/dist/actor-react.js.map +1 -1
  12. package/dist/actor.d.ts +2 -176
  13. package/dist/actor.js +13 -2
  14. package/dist/actor.js.map +1 -1
  15. package/dist/ai-server.d.ts +1 -1
  16. package/dist/ai-server.js +2 -2
  17. package/dist/ai.d.ts +1 -1
  18. package/dist/ai.js +1 -1
  19. package/dist/client-Bf6uSEAk.js +1342 -0
  20. package/dist/client-Bf6uSEAk.js.map +1 -0
  21. package/dist/client-P_NNNRM-.d.ts +243 -0
  22. package/dist/client-P_NNNRM-.d.ts.map +1 -0
  23. package/dist/client.d.ts +2 -202
  24. package/dist/client.js +2 -1026
  25. package/dist/errors-DCk6ch5n.js.map +1 -1
  26. package/dist/errors-DvhSXnxk.d.ts +28 -0
  27. package/dist/errors-DvhSXnxk.d.ts.map +1 -0
  28. package/dist/index.d.ts +3 -35
  29. package/dist/{internal-DRXJ56EI.js → internal-Dq2qYxou.js} +2 -2
  30. package/dist/{internal-DRXJ56EI.js.map → internal-Dq2qYxou.js.map} +1 -1
  31. package/dist/platform-B4TnJtWu.js +34 -0
  32. package/dist/platform-B4TnJtWu.js.map +1 -0
  33. package/dist/react.d.ts +3 -1
  34. package/dist/react.d.ts.map +1 -1
  35. package/dist/react.js +2 -1
  36. package/dist/react.js.map +1 -1
  37. package/dist/scheduler-qstash.d.ts +2 -2
  38. package/dist/scheduler-qstash.js +3 -2
  39. package/dist/scheduler-qstash.js.map +1 -1
  40. package/dist/scheduler-vercel.d.ts +2 -2
  41. package/dist/scheduler-vercel.js +2 -2
  42. package/dist/{server-CBET-jSz.js → server-Dkz2a84E.js} +295 -133
  43. package/dist/server-Dkz2a84E.js.map +1 -0
  44. package/dist/{server-CKY3_lbw.d.ts → server-DwPrMqHB.d.ts} +4 -2
  45. package/dist/server-DwPrMqHB.d.ts.map +1 -0
  46. package/dist/server.d.ts +2 -2
  47. package/dist/server.js +1 -1
  48. package/dist/{store-DGHeBtIQ.d.ts → store-DtDOWLSn.d.ts} +4 -5
  49. package/dist/{store-DGHeBtIQ.d.ts.map → store-DtDOWLSn.d.ts.map} +1 -1
  50. package/dist/store-N8PXxDAS.js.map +1 -1
  51. package/dist/store-memory.d.ts +1 -1
  52. package/dist/store-memory.js +1 -1
  53. package/dist/{store-polling-6DW7F1DT.js → store-polling-CmxUbV93.js} +56 -7
  54. package/dist/store-polling-CmxUbV93.js.map +1 -0
  55. package/dist/store-postgres.d.ts +6 -4
  56. package/dist/store-postgres.d.ts.map +1 -1
  57. package/dist/store-postgres.js +701 -53
  58. package/dist/store-postgres.js.map +1 -1
  59. package/dist/store-presence-polling-C7-XZyW9.js +94 -0
  60. package/dist/store-presence-polling-C7-XZyW9.js.map +1 -0
  61. package/dist/store-redis-http.d.ts +2 -2
  62. package/dist/store-redis-http.d.ts.map +1 -1
  63. package/dist/store-redis-http.js +184 -38
  64. package/dist/store-redis-http.js.map +1 -1
  65. package/dist/{store-redis-core-z-ykbyMg.js → store-redis-notify-BUCyXOn0.js} +491 -27
  66. package/dist/store-redis-notify-BUCyXOn0.js.map +1 -0
  67. package/dist/store-redis.d.ts +5 -13
  68. package/dist/store-redis.d.ts.map +1 -1
  69. package/dist/store-redis.js +27 -271
  70. package/dist/store-redis.js.map +1 -1
  71. package/dist/store-sqlite.d.ts +1 -1
  72. package/dist/store-sqlite.js +1 -1
  73. package/dist/{wire--yji6mO3.js → wire-BO5wWCb1.js} +18 -16
  74. package/dist/wire-BO5wWCb1.js.map +1 -0
  75. package/docs/actors/04-routes.mdx +40 -8
  76. package/docs/guides/03-react.mdx +52 -10
  77. package/docs/guides/05-production.mdx +54 -5
  78. package/docs/guides/09-presence.mdx +12 -7
  79. package/docs/guides/10-transports.mdx +85 -22
  80. package/docs/reference/01-api.mdx +90 -28
  81. package/docs/reference/02-errors.mdx +45 -11
  82. package/examples/playground/package.json +1 -1
  83. package/package.json +1 -1
  84. package/src/actor-client.ts +114 -14
  85. package/src/actor-react.ts +18 -3
  86. package/src/actor.ts +16 -1
  87. package/src/client-errors.ts +51 -0
  88. package/src/client.ts +577 -175
  89. package/src/errors.ts +4 -9
  90. package/src/index.ts +1 -1
  91. package/src/internal.ts +1 -1
  92. package/src/postgres-notification-scope.ts +134 -0
  93. package/src/postgres-notifications.ts +244 -0
  94. package/src/postgres-pool.ts +65 -0
  95. package/src/postgres-resources.ts +185 -0
  96. package/src/presence-recovery.ts +120 -0
  97. package/src/react.ts +3 -0
  98. package/src/redis-http-subscriptions.ts +199 -0
  99. package/src/server-fetch.ts +51 -23
  100. package/src/server.ts +146 -19
  101. package/src/session-socket.ts +241 -88
  102. package/src/sse.ts +17 -0
  103. package/src/store-polling.ts +32 -12
  104. package/src/store-postgres.ts +292 -75
  105. package/src/store-presence-polling.ts +125 -0
  106. package/src/store-redis-core.ts +37 -30
  107. package/src/store-redis-http.ts +51 -45
  108. package/src/store-redis-notify.ts +464 -0
  109. package/src/store-redis.ts +9 -364
  110. package/src/store.ts +3 -4
  111. package/src/stream-activity.ts +32 -0
  112. package/src/wire.ts +39 -15
  113. package/dist/actor-shared-BACubf4x.d.ts +0 -136
  114. package/dist/actor-shared-BACubf4x.d.ts.map +0 -1
  115. package/dist/actor.d.ts.map +0 -1
  116. package/dist/client.d.ts.map +0 -1
  117. package/dist/client.js.map +0 -1
  118. package/dist/index.d.ts.map +0 -1
  119. package/dist/server-CBET-jSz.js.map +0 -1
  120. package/dist/server-CKY3_lbw.d.ts.map +0 -1
  121. package/dist/store-polling-6DW7F1DT.js.map +0 -1
  122. package/dist/store-redis-core-z-ykbyMg.js.map +0 -1
  123. package/dist/wire--yji6mO3.js.map +0 -1
package/dist/client.d.ts CHANGED
@@ -1,202 +1,2 @@
1
- import { a as ContractEvent, c as PresenceDefs, l as PresenceMap, p as WithPresence, r as AppendInput, s as EventDefs, t as Reducer, u as PresencePatch } from "./reducer-DJKWm3cp.js";
2
- //#region src/client.d.ts
3
- type ConnectionStatus = "idle" | "connecting" | "live" | "closed";
4
- /**
5
- * The connection, as a discriminated union — impossible states are
6
- * unrepresentable: an error only exists while disconnected,
7
- * `reconnects` only once a connection has been attempted.
8
- * "Reconnecting…" is `status === 'connecting' && reconnects > 0`.
9
- */
10
- type Connection = {
11
- status: "idle";
12
- } | {
13
- status: "connecting";
14
- /** Drops of an established stream so far. `0` = first connect. */
15
- reconnects: number;
16
- /** Why the last connection ended; `null` on the first connect. */
17
- error: Error | null;
18
- } | {
19
- status: "live";
20
- reconnects: number;
21
- } | {
22
- status: "closed";
23
- };
24
- /**
25
- * What `push` returns: resolves at the server ack (exactly like a
26
- * plain promise — `await push(...)` gives the acked events), and
27
- * carries `confirmed` for the later moment when the live stream has
28
- * delivered the whole batch back and the optimistic overlay entry
29
- * retired — the view now shows server truth. `confirmed` is lazy:
30
- * never accessed, never created. A rejected push rejects both.
31
- */
32
- type PushResult<D extends EventDefs> = Promise<ContractEvent<D>[]> & {
33
- readonly confirmed: Promise<ContractEvent<D>[]>;
34
- };
35
- type LoadHistoryOptions = {
36
- /**
37
- * Exclusive upper bound for the fetch. Defaults to the oldest
38
- * already-loaded index — successive calls page backward — or one
39
- * past the hydration frontier while nothing is loaded yet.
40
- */
41
- before?: number;
42
- /** Page size; defaults to 50. */
43
- limit?: number;
44
- };
45
- /** Backscroll progress, derived from the raw event feed. */
46
- type HistoryState = {
47
- /** A `loadHistory` fetch is in flight. */
48
- loading: boolean;
49
- /** The feed reaches index 1 — nothing older remains to fetch. */
50
- complete: boolean;
51
- /** The oldest loaded index; `null` while nothing is loaded. */
52
- oldestLoaded: number | null;
53
- };
54
- /** One immutable view of the session — stable identity between changes. */
55
- type SessionSnapshot<D extends EventDefs, S, P extends PresenceDefs = Record<never, never>> = WithPresence<P, {
56
- /** The replicated presence map, this client included (local echo). */
57
- presence: PresenceMap<P>;
58
- }> & {
59
- /** The live view: server events folded, optimistic pushes applied. */
60
- state: S;
61
- /** The raw event feed: server truth — observed, seeded, or
62
- * backscrolled via `loadHistory` — plus pending optimistic events
63
- * (provisional indexes past the frontier). `state` never folds
64
- * backscrolled events; they are display data below the frontier. */
65
- events: ContractEvent<D>[];
66
- /** The stream frontier: the last server-confirmed index. This is the
67
- * `lastSeenIndex` cancellation wants. */
68
- index: number;
69
- /** Backscroll progress — `loadHistory` moves it. */
70
- history: HistoryState;
71
- connection: Connection;
72
- };
73
- /**
74
- * The presence member of a client session — intersected in via
75
- * `WithPresence`, so it exists exactly when the contract declares
76
- * presence fields.
77
- */
78
- type SessionClientPresence<P extends PresenceDefs> = {
79
- /**
80
- * Fire-and-forget presence: validates locally against the reducer's
81
- * presence schemas, merges into the local map immediately (the
82
- * echo), and coalesces on the wire to a fixed cadence — a leading
83
- * send immediately, then at most one merged send per interval,
84
- * later values winning field-wise. `null` clears a field. No ack,
85
- * no retry, no rollback: a lost patch is repainted by the next one.
86
- * Requires the session's `participant`.
87
- */
88
- setPresence(values: PresencePatch<P>["values"]): void;
89
- };
90
- type SessionClient<D extends EventDefs, S, P extends PresenceDefs = Record<never, never>> = WithPresence<P, SessionClientPresence<P>> & {
91
- readonly sessionId: string;
92
- subscribe(listener: () => void): () => void;
93
- getSnapshot(): SessionSnapshot<D, S, P>;
94
- /**
95
- * Optimistic append: validates locally against the reducer's event
96
- * schemas (instant `INVALID_PAYLOAD`, no flicker), applies to the
97
- * local fold, POSTs, swaps in the ack, rolls back on rejection.
98
- * Auto-retries only `STORE_UNAVAILABLE`. Resolves with the appended
99
- * events as the server recorded them. Overlapping calls on this
100
- * handle enter the transport in invocation order without delaying
101
- * their optimistic folds.
102
- */
103
- push(...events: AppendInput<D>[]): PushResult<D>;
104
- /**
105
- * Backscroll: fetch a bounded slice of events from below the
106
- * frontier and merge it into `events` — deduped, ordered, shared by
107
- * every handle of this session. Folded state and the optimistic
108
- * overlay are never touched. Calls serialize per session and skip
109
- * already-loaded ranges, so overlapping backscrolls never fetch the
110
- * same range twice. Resolves with the events in the requested range.
111
- * Requires an http api; the `ws` transport has no history lane and
112
- * this throws a TypeError there.
113
- */
114
- loadHistory(options?: LoadHistoryOptions): Promise<ContractEvent<D>[]>;
115
- /**
116
- * Take a lease on the live stream and return its release. Leases
117
- * refcount per handle — the first connects, releasing the last
118
- * closes — so independent consumers of one identity-mapped handle
119
- * (two hooks, a hook plus vanilla code) never fight over the
120
- * stream. Releasing twice is a no-op. While any lease is held the
121
- * stream reconnects with backoff and resumes from the frontier.
122
- */
123
- connect(): () => void;
124
- /**
125
- * The hard stop: drops every outstanding lease and closes the
126
- * stream now. Not terminal — a later `connect()` starts fresh from
127
- * the current frontier.
128
- */
129
- close(): void;
130
- };
131
- type SessionOptions<D extends EventDefs, S> = {
132
- initialState?: S;
133
- initialIndex?: number;
134
- /** Server-rendered history through `initialIndex`. Seeds the event feed. */
135
- initialEvents?: ContractEvent<D>[];
136
- /** Overrides the client's `participant` for this session handle.
137
- * Bound once per handle. */
138
- participant?: string;
139
- };
140
- type A2Client<D extends EventDefs, S, P extends PresenceDefs = Record<never, never>> = {
141
- session(sessionId: string, options?: SessionOptions<D, S>): SessionClient<D, S, P>;
142
- };
143
- /**
144
- * The wire the client rides. The string form is one route serving GET
145
- * (SSE stream) + POST (push). `http` splits the verbs across two
146
- * routes, for when platform duration limits differ per verb. `ws`
147
- * rides everything — every session of this client — over one
148
- * multiplexed WebSocket, served by `sessionsSocket`.
149
- */
150
- type ClientApi = string | {
151
- type: "http";
152
- push: string;
153
- stream: string;
154
- } | {
155
- type: "ws";
156
- url: string;
157
- };
158
- /**
159
- * The socket surface the `ws` transport drives — satisfied structurally
160
- * by the DOM `WebSocket` and Node's alike, and small enough to fake in
161
- * tests.
162
- */
163
- type ClientWebSocket = {
164
- readonly readyState: number;
165
- send(data: string): void;
166
- close(code?: number, reason?: string): void;
167
- addEventListener(type: "open", listener: () => void): void;
168
- addEventListener(type: "message", listener: (event: {
169
- data: unknown;
170
- }) => void): void;
171
- addEventListener(type: "close", listener: (event: {
172
- code: number;
173
- reason: string;
174
- }) => void): void;
175
- addEventListener(type: "error", listener: () => void): void;
176
- };
177
- type ClientWebSocketConstructor = new (url: string) => ClientWebSocket;
178
- type CreateClientOptions<D extends EventDefs, S, P extends PresenceDefs = Record<never, never>> = {
179
- reducer: Reducer<D, S, P>;
180
- /** The route (or routes) the client speaks — see `ClientApi`. */
181
- api: ClientApi;
182
- /** Injectable fetch — defaults to the global. */
183
- fetch?: typeof globalThis.fetch;
184
- /** Injectable WebSocket constructor for the `ws` api — defaults to
185
- * the global. The http transports never touch it. */
186
- webSocket?: ClientWebSocketConstructor;
187
- /** How long an idle session keeps its in-memory identity, in
188
- * milliseconds. Defaults to five minutes; `Infinity` disables GC. */
189
- gcTime?: number;
190
- /**
191
- * This client's presence identity — required to call `setPresence`.
192
- * Caller-supplied (a user id, a tab nonce, a guest name): A2 does
193
- * not invent an identity story; the app states it once here. The
194
- * default for every session handle this client creates;
195
- * `session(id, { participant })` overrides it per handle.
196
- */
197
- participant?: string;
198
- };
199
- declare function createClient<D extends EventDefs, S, P extends PresenceDefs = Record<never, never>>(options: CreateClientOptions<D, S, P>): A2Client<D, S, P>;
200
- //#endregion
201
- export { A2Client, ClientApi, ClientWebSocket, ClientWebSocketConstructor, Connection, ConnectionStatus, CreateClientOptions, HistoryState, LoadHistoryOptions, PushResult, SessionClient, SessionClientPresence, SessionOptions, SessionSnapshot, createClient };
202
- //# sourceMappingURL=client.d.ts.map
1
+ import { _ as createClient, a as Connection, c as HistoryState, d as ReconnectPolicy, f as SessionClient, g as SessionUrl, h as SessionSnapshot, i as ClientWebSocketConstructor, l as LoadHistoryOptions, m as SessionOptions, n as ClientApi, o as ConnectionStatus, p as SessionClientPresence, r as ClientWebSocket, s as CreateClientOptions, t as A2Client, u as PushResult, v as A2ClientError, y as A2ClientErrorOptions } from "./client-P_NNNRM-.js";
2
+ export { A2Client, A2ClientError, type A2ClientErrorOptions, ClientApi, ClientWebSocket, ClientWebSocketConstructor, Connection, ConnectionStatus, CreateClientOptions, HistoryState, LoadHistoryOptions, PushResult, ReconnectPolicy, SessionClient, SessionClientPresence, SessionOptions, SessionSnapshot, SessionUrl, createClient };