@enbox/api 0.6.31 → 0.6.33

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/src/live-query.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  import type { DwnMessageSubscription, EnboxAgent, PermissionsApi } from '@enbox/agent';
2
- import type { PaginationCursor, RecordsQueryReplyEntry } from '@enbox/dwn-sdk-js';
2
+ import type { PaginationCursor, ProgressToken, RecordsQueryReplyEntry } from '@enbox/dwn-sdk-js';
3
3
 
4
4
  import { getRecordAuthor } from '@enbox/agent';
5
5
 
@@ -21,6 +21,20 @@ export type RecordChange = {
21
21
  record: Record;
22
22
  };
23
23
 
24
+ /**
25
+ * Describes a terminal DWN subscription error surfaced by a {@link LiveQuery}.
26
+ */
27
+ export type LiveQueryError = {
28
+ /** The DWN error code associated with the terminal subscription failure. */
29
+ code: string;
30
+
31
+ /** Human-readable error detail from the DWN subscription. */
32
+ detail: string;
33
+
34
+ /** The event cursor that caused delivery to stop, when provided by the DWN. */
35
+ cursor?: ProgressToken;
36
+ };
37
+
24
38
  /**
25
39
  * A `CustomEvent` subclass carrying a {@link RecordChange} as its `detail`.
26
40
  *
@@ -77,10 +91,15 @@ export type RecordCatchAllEvent = 'change';
77
91
  */
78
92
  export type LiveQueryLifecycleEvent = 'disconnected' | 'reconnecting' | 'reconnected' | 'eose';
79
93
 
94
+ /**
95
+ * Terminal event type emitted when a DWN subscription fails after it is open.
96
+ */
97
+ export type LiveQueryTerminalEvent = 'error';
98
+
80
99
  /**
81
100
  * Union of all event types emitted by {@link LiveQuery}.
82
101
  */
83
- export type LiveQueryEventType = RecordChangeType | RecordCatchAllEvent | LiveQueryLifecycleEvent;
102
+ export type LiveQueryEventType = RecordChangeType | RecordCatchAllEvent | LiveQueryLifecycleEvent | LiveQueryTerminalEvent;
84
103
 
85
104
  /**
86
105
  * A live query that combines an initial snapshot of matching records with a
@@ -103,6 +122,7 @@ export type LiveQueryEventType = RecordChangeType | RecordCatchAllEvent | LiveQu
103
122
  * | `reconnecting` | `{ attempt: number }` | Reconnection attempt in progress |
104
123
  * | `reconnected` | — | Connection restored, subscription resubscribed |
105
124
  * | `eose` | — | End-of-stored-events: catch-up replay complete, events are now live |
125
+ * | `error` | {@link LiveQueryError} | Terminal subscription error; no further events will be delivered |
106
126
  *
107
127
  * @example
108
128
  * ```ts
@@ -269,6 +289,21 @@ export class LiveQuery extends EventTarget {
269
289
  this.dispatchEvent(new CustomEvent(type, { detail }));
270
290
  }
271
291
 
292
+ /**
293
+ * Handle a terminal DWN subscription error.
294
+ *
295
+ * @internal — Called by `DwnApi.records.subscribe()` before closing the
296
+ * underlying subscription.
297
+ */
298
+ public handleError(error: LiveQueryError): void {
299
+ if (this._closed) {
300
+ return;
301
+ }
302
+
303
+ this._connected = false;
304
+ this.dispatchEvent(new CustomEvent('error', { detail: error }));
305
+ }
306
+
272
307
  /**
273
308
  * Register a typed event handler. Returns an unsubscribe function.
274
309
  *
@@ -290,9 +325,15 @@ export class LiveQuery extends EventTarget {
290
325
  on(event: 'reconnecting', handler: (detail: { attempt: number }) => void): () => void;
291
326
  on(event: 'reconnected', handler: () => void): () => void;
292
327
  on(event: 'eose', handler: () => void): () => void;
328
+ on(event: 'error', handler: (error: LiveQueryError) => void): () => void;
293
329
  on(
294
330
  event: LiveQueryEventType,
295
- handler: ((change: RecordChange) => void) | ((record: Record) => void) | ((detail: { attempt: number }) => void) | (() => void),
331
+ handler:
332
+ | ((change: RecordChange) => void)
333
+ | ((record: Record) => void)
334
+ | ((detail: { attempt: number }) => void)
335
+ | ((error: LiveQueryError) => void)
336
+ | (() => void),
296
337
  ): () => void {
297
338
  const wrapper = (e: Event): void => {
298
339
  const detail = (e as CustomEvent).detail;
@@ -302,6 +343,8 @@ export class LiveQuery extends EventTarget {
302
343
  (handler as (record: Record) => void)(detail.record);
303
344
  } else if (event === 'reconnecting') {
304
345
  (handler as (detail: { attempt: number }) => void)(detail);
346
+ } else if (event === 'error') {
347
+ (handler as (error: LiveQueryError) => void)(detail);
305
348
  } else {
306
349
  // disconnected, reconnected, eose — no payload
307
350
  (handler as () => void)();
@@ -6,7 +6,7 @@
6
6
  * - `.records` returns `TypedRecord<T>[]` instead of `Record[]`.
7
7
  * - `.on('create', handler)` provides `TypedRecord<T>` in the callback.
8
8
  * - `.on('change', handler)` provides `TypedRecordChange<T>` in the callback.
9
- * - `.on('disconnected' | 'reconnecting' | 'reconnected' | 'eose', handler)`
9
+ * - `.on('disconnected' | 'reconnecting' | 'reconnected' | 'eose' | 'error', handler)`
10
10
  * forwards transport lifecycle events from the underlying `LiveQuery`.
11
11
  *
12
12
  * @example
@@ -30,7 +30,7 @@
30
30
  */
31
31
 
32
32
  import type { PaginationCursor } from '@enbox/dwn-sdk-js';
33
- import type { LiveQuery, RecordChange } from './live-query.js';
33
+ import type { LiveQuery, LiveQueryError, RecordChange } from './live-query.js';
34
34
 
35
35
  import { TypedRecord } from './typed-record.js';
36
36
 
@@ -123,7 +123,7 @@ export class TypedLiveQuery<T> {
123
123
  *
124
124
  * Supports both record change events (`change`, `create`, `update`, `delete`)
125
125
  * and transport lifecycle events (`disconnected`, `reconnecting`,
126
- * `reconnected`, `eose`).
126
+ * `reconnected`, `eose`, `error`).
127
127
  *
128
128
  * @param event - The event type to listen for.
129
129
  * @param handler - The handler function with typed record(s) or lifecycle data.
@@ -148,11 +148,13 @@ export class TypedLiveQuery<T> {
148
148
  public on(event: 'reconnecting', handler: (detail: { attempt: number }) => void): () => void;
149
149
  public on(event: 'reconnected', handler: () => void): () => void;
150
150
  public on(event: 'eose', handler: () => void): () => void;
151
+ public on(event: 'error', handler: (error: LiveQueryError) => void): () => void;
151
152
  public on(
152
- event: 'change' | 'create' | 'update' | 'delete' | 'disconnected' | 'reconnecting' | 'reconnected' | 'eose',
153
+ event: 'change' | 'create' | 'update' | 'delete' | 'disconnected' | 'reconnecting' | 'reconnected' | 'eose' | 'error',
153
154
  handler: ((change: TypedRecordChange<T>) => void)
154
155
  | ((record: TypedRecord<T>) => void)
155
156
  | ((detail: { attempt: number }) => void)
157
+ | ((error: LiveQueryError) => void)
156
158
  | (() => void),
157
159
  ): () => void {
158
160
  // Lifecycle events: delegate directly to the underlying LiveQuery.
@@ -168,6 +170,9 @@ export class TypedLiveQuery<T> {
168
170
  if (event === 'reconnecting') {
169
171
  return this._liveQuery.on('reconnecting', handler as (detail: { attempt: number }) => void);
170
172
  }
173
+ if (event === 'error') {
174
+ return this._liveQuery.on('error', handler as (error: LiveQueryError) => void);
175
+ }
171
176
 
172
177
  // Record change events: wrap records in TypedRecord.
173
178
  if (event === 'change') {