@electric-sql/client 0.4.1 → 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/cjs/index.cjs +350 -223
- package/dist/cjs/index.cjs.map +1 -1
- package/dist/index.browser.mjs +1 -1
- package/dist/index.browser.mjs.map +1 -1
- package/dist/index.d.ts +59 -48
- package/dist/index.legacy-esm.js +348 -221
- package/dist/index.legacy-esm.js.map +1 -1
- package/dist/index.mjs +350 -223
- package/dist/index.mjs.map +1 -1
- package/package.json +1 -1
- package/src/client.ts +173 -416
- package/src/constants.ts +7 -0
- package/src/error.ts +50 -0
- package/src/fetch.ts +79 -0
- package/src/helpers.ts +6 -0
- package/src/index.ts +4 -1
- package/src/queue.ts +62 -0
- package/src/shape.ts +197 -0
- package/src/types.ts +2 -0
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
|
-
|
|
84
|
-
|
|
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
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
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,40 +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
|
|
173
|
-
|
|
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>[]) =>
|
|
199
|
+
subscribe(callback: (messages: Message<T>[]) => MaybePromise<void>, onError?: (error: FetchError | Error) => void): () => void;
|
|
187
200
|
unsubscribeAll(): void;
|
|
188
|
-
|
|
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
|
-
|
|
196
|
-
|
|
197
|
-
/**
|
|
198
|
-
* Resets the state of the stream, optionally with a provided
|
|
199
|
-
* shape ID
|
|
200
|
-
*/
|
|
201
|
-
private reset;
|
|
202
|
-
private validateOptions;
|
|
203
|
-
private fetchWithBackoff;
|
|
209
|
+
/** True during initial fetch. False afterwise. */
|
|
210
|
+
isLoading(): boolean;
|
|
204
211
|
}
|
|
212
|
+
|
|
213
|
+
type ShapeData<T extends Row = Row> = Map<string, T>;
|
|
214
|
+
type ShapeChangedCallback<T extends Row = Row> = (value: ShapeData<T>) => void;
|
|
205
215
|
/**
|
|
206
216
|
* A Shape is an object that subscribes to a shape log,
|
|
207
217
|
* keeps a materialised shape `.value` in memory and
|
|
@@ -234,22 +244,23 @@ declare class ShapeStream<T extends Row = Row> {
|
|
|
234
244
|
* })
|
|
235
245
|
*/
|
|
236
246
|
declare class Shape<T extends Row = Row> {
|
|
237
|
-
private
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
error: FetchError | false;
|
|
241
|
-
private hasNotifiedSubscribersUpToDate;
|
|
242
|
-
constructor(stream: ShapeStream<T>);
|
|
243
|
-
lastSynced(): number;
|
|
244
|
-
isConnected(): boolean;
|
|
247
|
+
#private;
|
|
248
|
+
constructor(stream: ShapeStreamInterface<T>);
|
|
249
|
+
get isUpToDate(): boolean;
|
|
245
250
|
get value(): Promise<ShapeData<T>>;
|
|
246
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;
|
|
247
261
|
subscribe(callback: ShapeChangedCallback<T>): () => void;
|
|
248
262
|
unsubscribeAll(): void;
|
|
249
263
|
get numSubscribers(): number;
|
|
250
|
-
private process;
|
|
251
|
-
private handleError;
|
|
252
|
-
private notify;
|
|
253
264
|
}
|
|
254
265
|
|
|
255
266
|
/**
|
|
@@ -289,4 +300,4 @@ declare function isChangeMessage<T extends Row = Row>(message: Message<T>): mess
|
|
|
289
300
|
*/
|
|
290
301
|
declare function isControlMessage<T extends Row = Row>(message: Message<T>): message is ControlMessage;
|
|
291
302
|
|
|
292
|
-
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 };
|