@electric-sql/client 0.5.0 → 0.5.1

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/dist/index.d.ts CHANGED
@@ -72,6 +72,7 @@ type TypedMessages<T extends Row = Row> = {
72
72
  messages: Array<Message<T>>;
73
73
  schema: ColumnInfo;
74
74
  };
75
+ type MaybePromise<T> = T | Promise<T>;
75
76
 
76
77
  type NullToken = null | `NULL`;
77
78
  type Token = Exclude<string, NullToken>;
@@ -80,18 +81,35 @@ type Parser = {
80
81
  [key: string]: ParseFunction;
81
82
  };
82
83
 
83
- type ShapeData<T extends Row = Row> = Map<string, T>;
84
- type ShapeChangedCallback<T extends Row = Row> = (value: ShapeData<T>) => void;
84
+ declare class FetchError extends Error {
85
+ url: string;
86
+ status: number;
87
+ text?: string;
88
+ json?: object;
89
+ headers: Record<string, string>;
90
+ constructor(status: number, text: string | undefined, json: object | undefined, headers: Record<string, string>, url: string, message?: string);
91
+ static fromResponse(response: Response, url: string): Promise<FetchError>;
92
+ }
93
+
85
94
  interface BackoffOptions {
95
+ /**
96
+ * Initial delay before retrying in milliseconds
97
+ */
86
98
  initialDelay: number;
99
+ /**
100
+ * Maximum retry delay in milliseconds
101
+ */
87
102
  maxDelay: number;
88
103
  multiplier: number;
104
+ onFailedAttempt?: () => void;
105
+ debug?: boolean;
89
106
  }
90
107
  declare const BackoffDefaults: {
91
108
  initialDelay: number;
92
109
  maxDelay: number;
93
110
  multiplier: number;
94
111
  };
112
+
95
113
  /**
96
114
  * Options for constructing a ShapeStream.
97
115
  */
@@ -129,14 +147,17 @@ interface ShapeStreamOptions {
129
147
  fetchClient?: typeof fetch;
130
148
  parser?: Parser;
131
149
  }
132
- declare class FetchError extends Error {
133
- url: string;
134
- status: number;
135
- text?: string;
136
- json?: object;
137
- headers: Record<string, string>;
138
- constructor(status: number, text: string | undefined, json: object | undefined, headers: Record<string, string>, url: string, message?: string);
139
- static fromResponse(response: Response, url: string): Promise<FetchError>;
150
+ interface ShapeStreamInterface<T extends Row = Row> {
151
+ subscribe(callback: (messages: Message<T>[]) => MaybePromise<void>, onError?: (error: FetchError | Error) => void): void;
152
+ unsubscribeAllUpToDateSubscribers(): void;
153
+ unsubscribeAll(): void;
154
+ subscribeOnceToUpToDate(callback: () => MaybePromise<void>, error: (err: FetchError | Error) => void): () => void;
155
+ isLoading(): boolean;
156
+ lastSyncedAt(): number | undefined;
157
+ lastSynced(): number;
158
+ isConnected(): boolean;
159
+ isUpToDate: boolean;
160
+ shapeId?: string;
140
161
  }
141
162
  /**
142
163
  * Reads updates to a shape from Electric using HTTP requests and long polling. Notifies subscribers
@@ -168,42 +189,29 @@ declare class FetchError extends Error {
168
189
  * aborter.abort()
169
190
  * ```
170
191
  */
171
- declare class ShapeStream<T extends Row = Row> {
172
- private options;
173
- private backoffOptions;
174
- private fetchClient;
175
- private schema?;
176
- private subscribers;
177
- private upToDateSubscribers;
178
- private lastOffset;
179
- private messageParser;
180
- private lastSyncedAt?;
181
- isUpToDate: boolean;
182
- private connected;
183
- shapeId?: string;
192
+ declare class ShapeStream<T extends Row = Row> implements ShapeStreamInterface<T> {
193
+ #private;
194
+ readonly options: ShapeStreamOptions;
184
195
  constructor(options: ShapeStreamOptions);
196
+ get shapeId(): string;
197
+ get isUpToDate(): boolean;
185
198
  start(): Promise<void>;
186
- subscribe(callback: (messages: Message<T>[]) => void | Promise<void>, onError?: (error: FetchError | Error) => void): () => void;
199
+ subscribe(callback: (messages: Message<T>[]) => MaybePromise<void>, onError?: (error: FetchError | Error) => void): () => void;
187
200
  unsubscribeAll(): void;
188
- private publish;
189
- private sendErrorToSubscribers;
190
- subscribeOnceToUpToDate(callback: () => void | Promise<void>, error: (err: FetchError | Error) => void): () => void;
201
+ subscribeOnceToUpToDate(callback: () => MaybePromise<void>, error: (err: FetchError | Error) => void): () => void;
191
202
  unsubscribeAllUpToDateSubscribers(): void;
203
+ /** Unix time at which we last synced. Undefined when `isLoading` is true. */
204
+ lastSyncedAt(): number | undefined;
192
205
  /** Time elapsed since last sync (in ms). Infinity if we did not yet sync. */
193
206
  lastSynced(): number;
207
+ /** Indicates if we are connected to the Electric sync service. */
194
208
  isConnected(): boolean;
195
209
  /** True during initial fetch. False afterwise. */
196
210
  isLoading(): boolean;
197
- private notifyUpToDateSubscribers;
198
- private sendErrorToUpToDateSubscribers;
199
- /**
200
- * Resets the state of the stream, optionally with a provided
201
- * shape ID
202
- */
203
- private reset;
204
- private validateOptions;
205
- private fetchWithBackoff;
206
211
  }
212
+
213
+ type ShapeData<T extends Row = Row> = Map<string, T>;
214
+ type ShapeChangedCallback<T extends Row = Row> = (value: ShapeData<T>) => void;
207
215
  /**
208
216
  * A Shape is an object that subscribes to a shape log,
209
217
  * keeps a materialised shape `.value` in memory and
@@ -236,24 +244,23 @@ declare class ShapeStream<T extends Row = Row> {
236
244
  * })
237
245
  */
238
246
  declare class Shape<T extends Row = Row> {
239
- private stream;
240
- private data;
241
- private subscribers;
242
- error: FetchError | false;
243
- private hasNotifiedSubscribersUpToDate;
244
- constructor(stream: ShapeStream<T>);
245
- lastSynced(): number;
246
- isConnected(): boolean;
247
- /** True during initial fetch. False afterwise. */
248
- isLoading(): boolean;
247
+ #private;
248
+ constructor(stream: ShapeStreamInterface<T>);
249
+ get isUpToDate(): boolean;
249
250
  get value(): Promise<ShapeData<T>>;
250
251
  get valueSync(): ShapeData<T>;
252
+ get error(): false | FetchError;
253
+ /** Unix time at which we last synced. Undefined when `isLoading` is true. */
254
+ lastSyncedAt(): number | undefined;
255
+ /** Time elapsed since last sync (in ms). Infinity if we did not yet sync. */
256
+ lastSynced(): number;
257
+ /** True during initial fetch. False afterwise. */
258
+ isLoading(): boolean;
259
+ /** Indicates if we are connected to the Electric sync service. */
260
+ isConnected(): boolean;
251
261
  subscribe(callback: ShapeChangedCallback<T>): () => void;
252
262
  unsubscribeAll(): void;
253
263
  get numSubscribers(): number;
254
- private process;
255
- private handleError;
256
- private notify;
257
264
  }
258
265
 
259
266
  /**
@@ -293,4 +300,4 @@ declare function isChangeMessage<T extends Row = Row>(message: Message<T>): mess
293
300
  */
294
301
  declare function isControlMessage<T extends Row = Row>(message: Message<T>): message is ControlMessage;
295
302
 
296
- export { BackoffDefaults, type BackoffOptions, type BitColumn, type BpcharColumn, type ChangeMessage, type ColumnInfo, type CommonColumnProps, type ControlMessage, FetchError, type IntervalColumn, type IntervalColumnWithPrecision, type Message, type NumericColumn, type Offset, type RegularColumn, type Row, type Schema, Shape, type ShapeChangedCallback, type ShapeData, ShapeStream, type ShapeStreamOptions, type TimeColumn, type TypedMessages, type Value, type VarcharColumn, isChangeMessage, isControlMessage };
303
+ export { BackoffDefaults, type BackoffOptions, type BitColumn, type BpcharColumn, type ChangeMessage, type ColumnInfo, type CommonColumnProps, type ControlMessage, FetchError, type IntervalColumn, type IntervalColumnWithPrecision, type MaybePromise, type Message, type NumericColumn, type Offset, type RegularColumn, type Row, type Schema, Shape, type ShapeChangedCallback, type ShapeData, ShapeStream, type ShapeStreamInterface, type ShapeStreamOptions, type TimeColumn, type TypedMessages, type Value, type VarcharColumn, isChangeMessage, isControlMessage };