@supacloud/js 0.11.3 → 0.11.4
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 +2 -0
- package/dist/index.js +54 -0
- package/package.json +1 -1
package/dist/index.d.ts
CHANGED
|
@@ -64,6 +64,8 @@ export type SupaCloudTaskWaitOptions = {
|
|
|
64
64
|
export type SupaCloudTaskSubscribeState = "connecting" | "realtime" | "polling" | "closed";
|
|
65
65
|
export type SupaCloudTaskSubscribeOptions = {
|
|
66
66
|
pollingIntervalMs?: number;
|
|
67
|
+
realtimeTimeoutMs?: number;
|
|
68
|
+
reconcileIntervalMs?: number;
|
|
67
69
|
onUpdate: (task: SupaCloudTaskSnapshot) => void;
|
|
68
70
|
onStateChange?: (state: SupaCloudTaskSubscribeState, details?: {
|
|
69
71
|
error?: unknown;
|
package/dist/index.js
CHANGED
|
@@ -152,8 +152,12 @@ class SupaCloudTasksClient {
|
|
|
152
152
|
let closed = false;
|
|
153
153
|
let channel = null;
|
|
154
154
|
let pollTimer = null;
|
|
155
|
+
let realtimeTimeoutTimer = null;
|
|
156
|
+
let reconcileTimer = null;
|
|
155
157
|
let mode = "connecting";
|
|
156
158
|
const pollingIntervalMs = options.pollingIntervalMs ?? this.options.pollingIntervalMs;
|
|
159
|
+
const realtimeTimeoutMs = options.realtimeTimeoutMs ?? 1e4;
|
|
160
|
+
const reconcileIntervalMs = options.reconcileIntervalMs ?? Math.max(30000, pollingIntervalMs * 10);
|
|
157
161
|
const stopOnTerminal = options.stopOnTerminal ?? true;
|
|
158
162
|
const setMode = (next, details) => {
|
|
159
163
|
if (closed || mode === next)
|
|
@@ -167,6 +171,18 @@ class SupaCloudTasksClient {
|
|
|
167
171
|
pollTimer = null;
|
|
168
172
|
}
|
|
169
173
|
};
|
|
174
|
+
const stopRealtimeTimeout = () => {
|
|
175
|
+
if (realtimeTimeoutTimer) {
|
|
176
|
+
clearTimeout(realtimeTimeoutTimer);
|
|
177
|
+
realtimeTimeoutTimer = null;
|
|
178
|
+
}
|
|
179
|
+
};
|
|
180
|
+
const stopReconcile = () => {
|
|
181
|
+
if (reconcileTimer) {
|
|
182
|
+
clearTimeout(reconcileTimer);
|
|
183
|
+
reconcileTimer = null;
|
|
184
|
+
}
|
|
185
|
+
};
|
|
170
186
|
const teardownRealtime = async () => {
|
|
171
187
|
if (channel) {
|
|
172
188
|
const current = channel;
|
|
@@ -178,6 +194,8 @@ class SupaCloudTasksClient {
|
|
|
178
194
|
if (closed)
|
|
179
195
|
return;
|
|
180
196
|
stopPolling();
|
|
197
|
+
stopRealtimeTimeout();
|
|
198
|
+
stopReconcile();
|
|
181
199
|
await teardownRealtime();
|
|
182
200
|
setMode("closed");
|
|
183
201
|
closed = true;
|
|
@@ -205,10 +223,35 @@ class SupaCloudTasksClient {
|
|
|
205
223
|
}
|
|
206
224
|
}
|
|
207
225
|
};
|
|
226
|
+
const reconcileOnce = async () => {
|
|
227
|
+
if (closed || mode !== "realtime")
|
|
228
|
+
return;
|
|
229
|
+
try {
|
|
230
|
+
emitTask(await this.get(taskId));
|
|
231
|
+
} catch (error) {
|
|
232
|
+
options.onError?.(error);
|
|
233
|
+
} finally {
|
|
234
|
+
if (!closed && mode === "realtime") {
|
|
235
|
+
reconcileTimer = setTimeout(() => {
|
|
236
|
+
reconcileOnce();
|
|
237
|
+
}, reconcileIntervalMs);
|
|
238
|
+
}
|
|
239
|
+
}
|
|
240
|
+
};
|
|
241
|
+
const startReconcile = () => {
|
|
242
|
+
if (closed || reconcileIntervalMs <= 0)
|
|
243
|
+
return;
|
|
244
|
+
stopReconcile();
|
|
245
|
+
reconcileTimer = setTimeout(() => {
|
|
246
|
+
reconcileOnce();
|
|
247
|
+
}, reconcileIntervalMs);
|
|
248
|
+
};
|
|
208
249
|
const startPolling = (error) => {
|
|
209
250
|
if (closed)
|
|
210
251
|
return;
|
|
211
252
|
stopPolling();
|
|
253
|
+
stopRealtimeTimeout();
|
|
254
|
+
stopReconcile();
|
|
212
255
|
setMode("polling", { error });
|
|
213
256
|
pollOnce();
|
|
214
257
|
};
|
|
@@ -226,12 +269,14 @@ class SupaCloudTasksClient {
|
|
|
226
269
|
return;
|
|
227
270
|
if (status === "SUBSCRIBED") {
|
|
228
271
|
stopPolling();
|
|
272
|
+
stopRealtimeTimeout();
|
|
229
273
|
setMode("realtime");
|
|
230
274
|
try {
|
|
231
275
|
emitTask(await this.get(taskId));
|
|
232
276
|
} catch (err) {
|
|
233
277
|
options.onError?.(err);
|
|
234
278
|
}
|
|
279
|
+
startReconcile();
|
|
235
280
|
return;
|
|
236
281
|
}
|
|
237
282
|
if (status === "TIMED_OUT" || status === "CHANNEL_ERROR" || status === "CLOSED") {
|
|
@@ -239,6 +284,15 @@ class SupaCloudTasksClient {
|
|
|
239
284
|
startPolling(error);
|
|
240
285
|
}
|
|
241
286
|
});
|
|
287
|
+
if (realtimeTimeoutMs > 0) {
|
|
288
|
+
realtimeTimeoutTimer = setTimeout(() => {
|
|
289
|
+
if (!closed && mode === "connecting") {
|
|
290
|
+
teardownRealtime().finally(() => {
|
|
291
|
+
startPolling(new Error("SupaCloud Realtime subscription timed out"));
|
|
292
|
+
});
|
|
293
|
+
}
|
|
294
|
+
}, realtimeTimeoutMs);
|
|
295
|
+
}
|
|
242
296
|
setMode("connecting");
|
|
243
297
|
return {
|
|
244
298
|
get connectionState() {
|