@tallyui/database 3.14.0 → 3.15.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.
- package/dist/index.d.ts +12 -7
- package/dist/index.js +66 -23
- package/package.json +2 -2
- package/src/connectivity.ts +14 -8
- package/src/replication.ts +73 -15
package/dist/index.d.ts
CHANGED
|
@@ -151,10 +151,13 @@ interface ConnectivitySnapshot {
|
|
|
151
151
|
type ConnectivityEvent = {
|
|
152
152
|
type: 'success';
|
|
153
153
|
at: number;
|
|
154
|
-
}
|
|
154
|
+
}
|
|
155
|
+
/** `answered`: the store sent a response (a 401, a 404, a 5xx), so the network is up whatever `failure` says. */
|
|
156
|
+
| {
|
|
155
157
|
type: 'failure';
|
|
156
158
|
at: number;
|
|
157
159
|
failure: FailureClass;
|
|
160
|
+
answered?: boolean;
|
|
158
161
|
}
|
|
159
162
|
/** The OS says the link is down: offline at once, as v2. */
|
|
160
163
|
| {
|
|
@@ -165,8 +168,9 @@ type ConnectivityEvent = {
|
|
|
165
168
|
declare const INITIAL_CONNECTIVITY: ConnectivitySnapshot;
|
|
166
169
|
/**
|
|
167
170
|
* v2's rule (`use-online-status.web.tsx`): the first success is online; a failure changes the verdict only once
|
|
168
|
-
* failures have run for the confirmation window.
|
|
169
|
-
* reset (v2: a fresh engine) clears it. Returns the same
|
|
171
|
+
* failures have run for the confirmation window. Any answer from the store ends offline. The latch holds across
|
|
172
|
+
* successes, first sight wins, and only a reset (v2: a fresh engine, which re-probes) clears it. Returns the same
|
|
173
|
+
* object when nothing changes.
|
|
170
174
|
*/
|
|
171
175
|
declare function reduceConnectivity(snapshot: ConnectivitySnapshot, event: ConnectivityEvent): ConnectivitySnapshot;
|
|
172
176
|
interface ConnectivityTracker {
|
|
@@ -174,10 +178,10 @@ interface ConnectivityTracker {
|
|
|
174
178
|
readonly snapshot$: Observable<ConnectivitySnapshot>;
|
|
175
179
|
current(): ConnectivitySnapshot;
|
|
176
180
|
recordSuccess(): void;
|
|
177
|
-
/** Classifies with the tracker's connector, reduces, and returns the class. */
|
|
181
|
+
/** Classifies with the tracker's connector, reduces, and returns the class. A numeric `status` on the error is an answer. */
|
|
178
182
|
recordFailure(error: unknown): FailureClass;
|
|
179
183
|
linkDown(): void;
|
|
180
|
-
/** Clears the update-required latch
|
|
184
|
+
/** Clears the update-required latch once the store or the till was updated; a replication it feeds re-probes. */
|
|
181
185
|
resetUpdateRequired(): void;
|
|
182
186
|
}
|
|
183
187
|
/** One tracker per connector session; a new one starts clear, as a fresh v2 engine does. */
|
|
@@ -199,8 +203,9 @@ interface StartReplicationOptions<RxDocType, CheckpointType = any> {
|
|
|
199
203
|
/** The backoff's ceiling and a store error's wait, in ms (default: MAX_RETRY_TIME_MS); tests shorten it. */
|
|
200
204
|
maxRetryTimeMs?: number;
|
|
201
205
|
/**
|
|
202
|
-
* Fed with every pull and push outcome. While its update-required latch is set, the pull
|
|
203
|
-
* neither calling the adapter (v2 sends nothing once latched). `
|
|
206
|
+
* Fed with every pull and push outcome. While its update-required latch is set, the pull returns empty pages and a
|
|
207
|
+
* push waits, neither calling the adapter (v2 sends nothing once latched). `resetUpdateRequired()` re-probes: the
|
|
208
|
+
* pull asks the store again and a held push goes. `resume()` keeps the latch.
|
|
204
209
|
*/
|
|
205
210
|
connectivity?: ConnectivityTracker;
|
|
206
211
|
}
|
package/dist/index.js
CHANGED
|
@@ -52,8 +52,8 @@ function withStorageWatchdog(storage, options = {}) {
|
|
|
52
52
|
readWatchdogMs = STORAGE_READ_WATCHDOG_MS,
|
|
53
53
|
now = Date.now,
|
|
54
54
|
setTimer = (fn, ms) => {
|
|
55
|
-
const
|
|
56
|
-
return () => clearTimeout(
|
|
55
|
+
const timer2 = setTimeout(fn, ms);
|
|
56
|
+
return () => clearTimeout(timer2);
|
|
57
57
|
}
|
|
58
58
|
} = options;
|
|
59
59
|
const health$ = new BehaviorSubject({ status: "ok", stalledWrites: 0 });
|
|
@@ -189,7 +189,16 @@ function getStorage() {
|
|
|
189
189
|
|
|
190
190
|
// src/replication.ts
|
|
191
191
|
import { replicateRxCollection } from "rxdb/plugins/replication";
|
|
192
|
-
import {
|
|
192
|
+
import { errorToPlainJson, newRxError, toArray } from "rxdb";
|
|
193
|
+
import {
|
|
194
|
+
BehaviorSubject as BehaviorSubject2,
|
|
195
|
+
EMPTY,
|
|
196
|
+
filter,
|
|
197
|
+
firstValueFrom,
|
|
198
|
+
fromEvent,
|
|
199
|
+
merge,
|
|
200
|
+
timer
|
|
201
|
+
} from "rxjs";
|
|
193
202
|
import { errorKind } from "@tallyui/core";
|
|
194
203
|
var MAX_RETRY_TIME_MS = 5 * 60 * 1e3;
|
|
195
204
|
var MAX_RETRY_AFTER_MS = 60 * 60 * 1e3;
|
|
@@ -221,6 +230,17 @@ function retryAfter(error) {
|
|
|
221
230
|
const value = error?.retryAfterMs;
|
|
222
231
|
return typeof value === "number" && Number.isFinite(value) && value >= 0 ? value : 0;
|
|
223
232
|
}
|
|
233
|
+
async function hold(events, ready, wake$ = EMPTY) {
|
|
234
|
+
const done = () => events.canceled.getValue() || ready();
|
|
235
|
+
if (done()) return;
|
|
236
|
+
await firstValueFrom(merge(events.paused, events.canceled, wake$).pipe(filter(done)), { defaultValue: void 0 });
|
|
237
|
+
}
|
|
238
|
+
function retryWait(events, ms) {
|
|
239
|
+
if (events.canceled.getValue()) return Promise.resolve();
|
|
240
|
+
const offline = typeof window === "object" && typeof window.addEventListener === "function" && !navigator.onLine;
|
|
241
|
+
const wake$ = merge(timer(ms), events.canceled.pipe(filter(Boolean)), offline ? fromEvent(window, "online") : EMPTY);
|
|
242
|
+
return firstValueFrom(wake$, { defaultValue: void 0 });
|
|
243
|
+
}
|
|
224
244
|
function noticeFor(error, fixedBy) {
|
|
225
245
|
const fields = error;
|
|
226
246
|
const notice = { code: fields.code, since: Date.now(), fixedBy };
|
|
@@ -249,10 +269,11 @@ function startReplication({
|
|
|
249
269
|
if (state) state.retryTime = ms;
|
|
250
270
|
};
|
|
251
271
|
const pullHandler = async (checkpoint, batchSize) => {
|
|
252
|
-
if (tillStopped
|
|
272
|
+
if (tillStopped) {
|
|
253
273
|
void state?.pause();
|
|
254
274
|
return { documents: [], checkpoint };
|
|
255
275
|
}
|
|
276
|
+
if (connectivity?.current().updateRequired) return { documents: [], checkpoint };
|
|
256
277
|
const wait = storeRetryAt - Date.now();
|
|
257
278
|
if (wait > TIMER_SLACK_MS) {
|
|
258
279
|
setRetryTime(wait);
|
|
@@ -300,16 +321,29 @@ function startReplication({
|
|
|
300
321
|
batchSize: adapter.pull.batchSize
|
|
301
322
|
},
|
|
302
323
|
push: adapter.push ? {
|
|
324
|
+
// A push is held, never thrown or dropped, while latched or paused: before the send, and after it until
|
|
325
|
+
// the pause ends. RxDB 17.5 drops a paused push's answer (plugins/replication/index.js 269, upstream.js
|
|
326
|
+
// 287), so its rows go twice. And the pages queued behind it (upstream.js 90-100) then find no rows and
|
|
327
|
+
// store the up checkpoint anyway (upstream.js 211), so those rows never go. A failed push is retried
|
|
328
|
+
// here, never thrown: RxDB's own retry wait (index.js 266) ends unsent if paused by then, with the same
|
|
329
|
+
// loss. Only a cancel ends a hold, with nothing sent.
|
|
303
330
|
handler: async (rows) => {
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
331
|
+
const events = state.internalReplicationState.events;
|
|
332
|
+
const isLatched = () => Boolean(connectivity?.current().updateRequired);
|
|
333
|
+
for (; ; ) {
|
|
334
|
+
await hold(events, () => !isLatched() && !events.paused.getValue(), connectivity?.snapshot$);
|
|
335
|
+
if (events.canceled.getValue()) return [];
|
|
336
|
+
const sent = await adapter.push.handler(rows, context).then((conflicts) => ({ conflicts }), (error) => {
|
|
337
|
+
connectivity?.recordFailure(error);
|
|
338
|
+
const errors = toArray(error).map(errorToPlainJson);
|
|
339
|
+
events.error.next(newRxError("RC_PUSH", { pushRows: rows, errors, direction: "push" }));
|
|
340
|
+
});
|
|
341
|
+
if (sent) {
|
|
342
|
+
connectivity?.recordSuccess();
|
|
343
|
+
await hold(events, () => !events.paused.getValue());
|
|
344
|
+
return sent.conflicts;
|
|
345
|
+
}
|
|
346
|
+
await retryWait(events, state.retryTime ?? retryTime);
|
|
313
347
|
}
|
|
314
348
|
},
|
|
315
349
|
batchSize: adapter.push.batchSize
|
|
@@ -320,8 +354,15 @@ function startReplication({
|
|
|
320
354
|
});
|
|
321
355
|
state = replication;
|
|
322
356
|
const release = watchShared(adapter, collection);
|
|
357
|
+
let latched = Boolean(connectivity?.current().updateRequired);
|
|
358
|
+
const probe = connectivity?.snapshot$.subscribe(({ updateRequired }) => {
|
|
359
|
+
if (latched && !updateRequired && !tillStopped && !replication.isPaused()) replication.reSync();
|
|
360
|
+
latched = updateRequired !== null;
|
|
361
|
+
});
|
|
323
362
|
replication.canceled$.subscribe((canceled) => {
|
|
324
|
-
if (canceled)
|
|
363
|
+
if (!canceled) return;
|
|
364
|
+
release();
|
|
365
|
+
probe?.unsubscribe();
|
|
325
366
|
});
|
|
326
367
|
const resume = async () => {
|
|
327
368
|
notice$.next(void 0);
|
|
@@ -352,14 +393,15 @@ function reduceConnectivity(snapshot, event) {
|
|
|
352
393
|
return snapshot.updateRequired === null ? snapshot : { ...snapshot, updateRequired: null };
|
|
353
394
|
case "failure": {
|
|
354
395
|
const { failure, at } = event;
|
|
355
|
-
if (failure === null) return snapshot;
|
|
396
|
+
if (failure === null) return event.answered ? answered(snapshot) : snapshot;
|
|
356
397
|
if (typeof failure === "object") {
|
|
357
398
|
const next = answered(snapshot);
|
|
358
399
|
return next.updateRequired ? next : { ...next, updateRequired: failure.updateRequired };
|
|
359
400
|
}
|
|
360
401
|
const failingSince = snapshot.failingSince === null || snapshot.failingSince > at ? at : snapshot.failingSince;
|
|
361
402
|
const confirmed = at - failingSince >= FAILURE_CONFIRMATION_MS - FAILURE_CONFIRMATION_TOLERANCE_MS;
|
|
362
|
-
const
|
|
403
|
+
const verdict = confirmed ? failure : snapshot.state;
|
|
404
|
+
const state = event.answered && verdict === "offline" ? "degraded" : verdict;
|
|
363
405
|
return state === snapshot.state && failingSince === snapshot.failingSince ? snapshot : { ...snapshot, state, failingSince };
|
|
364
406
|
}
|
|
365
407
|
}
|
|
@@ -377,7 +419,8 @@ function createConnectivityTracker(options = {}) {
|
|
|
377
419
|
recordSuccess: () => dispatch({ type: "success", at: now() }),
|
|
378
420
|
recordFailure: (error) => {
|
|
379
421
|
const failure = classifyFailure(connector, error);
|
|
380
|
-
|
|
422
|
+
const answered2 = typeof error?.status === "number";
|
|
423
|
+
dispatch({ type: "failure", at: now(), failure, answered: answered2 });
|
|
381
424
|
return failure;
|
|
382
425
|
},
|
|
383
426
|
linkDown: () => dispatch({ type: "link-down" }),
|
|
@@ -435,12 +478,12 @@ function startLiveTab(options) {
|
|
|
435
478
|
});
|
|
436
479
|
});
|
|
437
480
|
const waitForAck = (id) => new Promise((resolve) => {
|
|
438
|
-
const
|
|
481
|
+
const timer2 = setTimeout(() => {
|
|
439
482
|
ackWaiters.delete(id);
|
|
440
483
|
resolve(false);
|
|
441
484
|
}, ackTimeoutMs);
|
|
442
485
|
ackWaiters.set(id, () => {
|
|
443
|
-
clearTimeout(
|
|
486
|
+
clearTimeout(timer2);
|
|
444
487
|
ackWaiters.delete(id);
|
|
445
488
|
resolve(true);
|
|
446
489
|
});
|
|
@@ -680,11 +723,11 @@ function startStockReconcile({
|
|
|
680
723
|
}
|
|
681
724
|
};
|
|
682
725
|
const reconcileStock = createPassQueue(tracked);
|
|
683
|
-
const
|
|
726
|
+
const timer2 = setInterval(() => {
|
|
684
727
|
reconcileStock().catch((error) => console.warn("Stock reconcile failed:", error));
|
|
685
728
|
}, intervalMs);
|
|
686
729
|
const stop = () => {
|
|
687
|
-
clearInterval(
|
|
730
|
+
clearInterval(timer2);
|
|
688
731
|
context.signal?.removeEventListener("abort", stop);
|
|
689
732
|
controller.abort();
|
|
690
733
|
state.complete();
|
|
@@ -732,8 +775,8 @@ function startCatalogueRunner({
|
|
|
732
775
|
now = Date.now,
|
|
733
776
|
stateId = "catalogue-reconcile",
|
|
734
777
|
setTimer = (fn, ms) => {
|
|
735
|
-
const
|
|
736
|
-
return () => clearTimeout(
|
|
778
|
+
const timer2 = setTimeout(fn, ms);
|
|
779
|
+
return () => clearTimeout(timer2);
|
|
737
780
|
},
|
|
738
781
|
keepCandidates = false
|
|
739
782
|
}) {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tallyui/database",
|
|
3
|
-
"version": "3.
|
|
3
|
+
"version": "3.15.0",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"description": "RxDB database factory for Tally UI — accepts pluggable connector schemas",
|
|
6
6
|
"main": "./dist/index.js",
|
|
@@ -30,7 +30,7 @@
|
|
|
30
30
|
"rxjs": "7.8.2"
|
|
31
31
|
},
|
|
32
32
|
"peerDependencies": {
|
|
33
|
-
"@tallyui/core": "^3.
|
|
33
|
+
"@tallyui/core": "^3.15.0"
|
|
34
34
|
},
|
|
35
35
|
"scripts": {
|
|
36
36
|
"build": "tsup",
|
package/src/connectivity.ts
CHANGED
|
@@ -24,7 +24,8 @@ export interface ConnectivitySnapshot {
|
|
|
24
24
|
|
|
25
25
|
export type ConnectivityEvent =
|
|
26
26
|
| { type: 'success'; at: number }
|
|
27
|
-
|
|
27
|
+
/** `answered`: the store sent a response (a 401, a 404, a 5xx), so the network is up whatever `failure` says. */
|
|
28
|
+
| { type: 'failure'; at: number; failure: FailureClass; answered?: boolean }
|
|
28
29
|
/** The OS says the link is down: offline at once, as v2. */
|
|
29
30
|
| { type: 'link-down' }
|
|
30
31
|
| { type: 'reset-update-required' };
|
|
@@ -36,8 +37,9 @@ const answered = (snapshot: ConnectivitySnapshot): ConnectivitySnapshot =>
|
|
|
36
37
|
|
|
37
38
|
/**
|
|
38
39
|
* v2's rule (`use-online-status.web.tsx`): the first success is online; a failure changes the verdict only once
|
|
39
|
-
* failures have run for the confirmation window.
|
|
40
|
-
* reset (v2: a fresh engine) clears it. Returns the same
|
|
40
|
+
* failures have run for the confirmation window. Any answer from the store ends offline. The latch holds across
|
|
41
|
+
* successes, first sight wins, and only a reset (v2: a fresh engine, which re-probes) clears it. Returns the same
|
|
42
|
+
* object when nothing changes.
|
|
41
43
|
*/
|
|
42
44
|
export function reduceConnectivity(snapshot: ConnectivitySnapshot, event: ConnectivityEvent): ConnectivitySnapshot {
|
|
43
45
|
switch (event.type) {
|
|
@@ -49,7 +51,8 @@ export function reduceConnectivity(snapshot: ConnectivitySnapshot, event: Connec
|
|
|
49
51
|
return snapshot.updateRequired === null ? snapshot : { ...snapshot, updateRequired: null };
|
|
50
52
|
case 'failure': {
|
|
51
53
|
const { failure, at } = event;
|
|
52
|
-
|
|
54
|
+
// An answer that says nothing about connectivity (a 401, a 404) still proves the store is reachable.
|
|
55
|
+
if (failure === null) return event.answered ? answered(snapshot) : snapshot;
|
|
53
56
|
if (typeof failure === 'object') {
|
|
54
57
|
// The store answered with its refusal, so it is reachable.
|
|
55
58
|
const next = answered(snapshot);
|
|
@@ -58,7 +61,9 @@ export function reduceConnectivity(snapshot: ConnectivitySnapshot, event: Connec
|
|
|
58
61
|
// A clock that went back would hold the verdict off for ever: re-anchor instead.
|
|
59
62
|
const failingSince = snapshot.failingSince === null || snapshot.failingSince > at ? at : snapshot.failingSince;
|
|
60
63
|
const confirmed = at - failingSince >= FAILURE_CONFIRMATION_MS - FAILURE_CONFIRMATION_TOLERANCE_MS;
|
|
61
|
-
const
|
|
64
|
+
const verdict = confirmed ? failure : snapshot.state;
|
|
65
|
+
// An answer is never offline: a 5xx while offline is degraded at once.
|
|
66
|
+
const state = event.answered && verdict === 'offline' ? 'degraded' : verdict;
|
|
62
67
|
return state === snapshot.state && failingSince === snapshot.failingSince ? snapshot : { ...snapshot, state, failingSince };
|
|
63
68
|
}
|
|
64
69
|
}
|
|
@@ -69,10 +74,10 @@ export interface ConnectivityTracker {
|
|
|
69
74
|
readonly snapshot$: Observable<ConnectivitySnapshot>;
|
|
70
75
|
current(): ConnectivitySnapshot;
|
|
71
76
|
recordSuccess(): void;
|
|
72
|
-
/** Classifies with the tracker's connector, reduces, and returns the class. */
|
|
77
|
+
/** Classifies with the tracker's connector, reduces, and returns the class. A numeric `status` on the error is an answer. */
|
|
73
78
|
recordFailure(error: unknown): FailureClass;
|
|
74
79
|
linkDown(): void;
|
|
75
|
-
/** Clears the update-required latch
|
|
80
|
+
/** Clears the update-required latch once the store or the till was updated; a replication it feeds re-probes. */
|
|
76
81
|
resetUpdateRequired(): void;
|
|
77
82
|
}
|
|
78
83
|
|
|
@@ -92,7 +97,8 @@ export function createConnectivityTracker(
|
|
|
92
97
|
recordSuccess: () => dispatch({ type: 'success', at: now() }),
|
|
93
98
|
recordFailure: (error) => {
|
|
94
99
|
const failure = classifyFailure(connector, error);
|
|
95
|
-
|
|
100
|
+
const answered = typeof (error as { status?: unknown } | null | undefined)?.status === 'number';
|
|
101
|
+
dispatch({ type: 'failure', at: now(), failure, answered });
|
|
96
102
|
return failure;
|
|
97
103
|
},
|
|
98
104
|
linkDown: () => dispatch({ type: 'link-down' }),
|
package/src/replication.ts
CHANGED
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
import { replicateRxCollection, type RxReplicationState } from 'rxdb/plugins/replication';
|
|
2
|
-
import type
|
|
3
|
-
import {
|
|
2
|
+
import { errorToPlainJson, newRxError, toArray, type RxCollection, type RxError } from 'rxdb';
|
|
3
|
+
import {
|
|
4
|
+
BehaviorSubject, EMPTY, filter, firstValueFrom, fromEvent, merge, timer, type Observable, type Subject, type Subscription,
|
|
5
|
+
} from 'rxjs';
|
|
4
6
|
|
|
5
7
|
import { errorKind, type ReplicationAdapter, type SyncContext, type SyncNotice } from '@tallyui/core';
|
|
6
8
|
|
|
@@ -61,8 +63,9 @@ export interface StartReplicationOptions<RxDocType, CheckpointType = any> {
|
|
|
61
63
|
/** The backoff's ceiling and a store error's wait, in ms (default: MAX_RETRY_TIME_MS); tests shorten it. */
|
|
62
64
|
maxRetryTimeMs?: number;
|
|
63
65
|
/**
|
|
64
|
-
* Fed with every pull and push outcome. While its update-required latch is set, the pull
|
|
65
|
-
* neither calling the adapter (v2 sends nothing once latched). `
|
|
66
|
+
* Fed with every pull and push outcome. While its update-required latch is set, the pull returns empty pages and a
|
|
67
|
+
* push waits, neither calling the adapter (v2 sends nothing once latched). `resetUpdateRequired()` re-probes: the
|
|
68
|
+
* pull asks the store again and a held push goes. `resume()` keeps the latch.
|
|
66
69
|
*/
|
|
67
70
|
connectivity?: ConnectivityTracker;
|
|
68
71
|
}
|
|
@@ -81,6 +84,31 @@ export function retryAfter(error: unknown): number {
|
|
|
81
84
|
return typeof value === 'number' && Number.isFinite(value) && value >= 0 ? value : 0;
|
|
82
85
|
}
|
|
83
86
|
|
|
87
|
+
/** The subjects of RxDB's protocol state the push reads, and its error feed (17.5 types/replication-protocol.d.ts 200-219). */
|
|
88
|
+
type ProtocolEvents = { paused: BehaviorSubject<boolean>; canceled: BehaviorSubject<boolean>; error: Subject<RxError> };
|
|
89
|
+
|
|
90
|
+
/**
|
|
91
|
+
* Resolves once `ready()` holds or the replication is cancelled, waking on a pause change, the cancel and `wake$`.
|
|
92
|
+
* The protocol's own `canceled` is set before a cancel awaits anything (17.5 replication-protocol/index.js 216), and
|
|
93
|
+
* completes after, hence the check first and the default value.
|
|
94
|
+
*/
|
|
95
|
+
async function hold(events: ProtocolEvents, ready: () => boolean, wake$: Observable<unknown> = EMPTY) {
|
|
96
|
+
const done = () => events.canceled.getValue() || ready();
|
|
97
|
+
if (done()) return;
|
|
98
|
+
await firstValueFrom(merge(events.paused, events.canceled, wake$).pipe(filter(done)), { defaultValue: undefined });
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
/**
|
|
102
|
+
* RxDB's own push retry wait (17.5 plugins/replication/replication-helper.js 51-65): `ms`, cut short by the browser's
|
|
103
|
+
* `online` event while it is offline, and here also by a cancel.
|
|
104
|
+
*/
|
|
105
|
+
function retryWait(events: ProtocolEvents, ms: number) {
|
|
106
|
+
if (events.canceled.getValue()) return Promise.resolve();
|
|
107
|
+
const offline = typeof window === 'object' && typeof window.addEventListener === 'function' && !navigator.onLine;
|
|
108
|
+
const wake$ = merge(timer(ms), events.canceled.pipe(filter(Boolean)), offline ? fromEvent(window, 'online') : EMPTY);
|
|
109
|
+
return firstValueFrom(wake$, { defaultValue: undefined });
|
|
110
|
+
}
|
|
111
|
+
|
|
84
112
|
/** The notice for a till or store error, with the error's string `software`, `minVersion` and `fix`. */
|
|
85
113
|
function noticeFor(error: unknown, fixedBy: 'till' | 'store'): SyncNotice {
|
|
86
114
|
const fields = error as Record<string, unknown>;
|
|
@@ -127,10 +155,13 @@ export function startReplication<RxDocType, CheckpointType = any>({
|
|
|
127
155
|
if (state) state.retryTime = ms;
|
|
128
156
|
};
|
|
129
157
|
const pullHandler = async (checkpoint: CheckpointType | undefined, batchSize: number) => {
|
|
130
|
-
if (tillStopped
|
|
158
|
+
if (tillStopped) {
|
|
131
159
|
void state?.pause();
|
|
132
160
|
return { documents: [], checkpoint };
|
|
133
161
|
}
|
|
162
|
+
// Latched: an empty page, no request, and no pause. A pause while a push is in flight makes RxDB drop that push's
|
|
163
|
+
// answer and send its rows again after the restart (17.5 replication-protocol/upstream.js), a doubled write.
|
|
164
|
+
if (connectivity?.current().updateRequired) return { documents: [], checkpoint };
|
|
134
165
|
const wait = storeRetryAt - Date.now();
|
|
135
166
|
if (wait > TIMER_SLACK_MS) {
|
|
136
167
|
setRetryTime(wait);
|
|
@@ -186,16 +217,30 @@ export function startReplication<RxDocType, CheckpointType = any>({
|
|
|
186
217
|
},
|
|
187
218
|
push: adapter.push
|
|
188
219
|
? {
|
|
220
|
+
// A push is held, never thrown or dropped, while latched or paused: before the send, and after it until
|
|
221
|
+
// the pause ends. RxDB 17.5 drops a paused push's answer (plugins/replication/index.js 269, upstream.js
|
|
222
|
+
// 287), so its rows go twice. And the pages queued behind it (upstream.js 90-100) then find no rows and
|
|
223
|
+
// store the up checkpoint anyway (upstream.js 211), so those rows never go. A failed push is retried
|
|
224
|
+
// here, never thrown: RxDB's own retry wait (index.js 266) ends unsent if paused by then, with the same
|
|
225
|
+
// loss. Only a cancel ends a hold, with nothing sent.
|
|
189
226
|
handler: async (rows) => {
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
227
|
+
const events = state!.internalReplicationState!.events as ProtocolEvents;
|
|
228
|
+
const isLatched = () => Boolean(connectivity?.current().updateRequired);
|
|
229
|
+
for (;;) {
|
|
230
|
+
await hold(events, () => !isLatched() && !events.paused.getValue(), connectivity?.snapshot$);
|
|
231
|
+
if (events.canceled.getValue()) return [];
|
|
232
|
+
const sent = await adapter.push!.handler(rows, context).then(conflicts => ({ conflicts }), (error: unknown) => {
|
|
233
|
+
connectivity?.recordFailure(error);
|
|
234
|
+
// RxDB's own push error (index.js 260-265), on the feed its error$ forwards (index.js 281).
|
|
235
|
+
const errors = toArray(error as Error).map(errorToPlainJson);
|
|
236
|
+
events.error.next(newRxError('RC_PUSH', { pushRows: rows, errors, direction: 'push' }));
|
|
237
|
+
});
|
|
238
|
+
if (sent) {
|
|
239
|
+
connectivity?.recordSuccess();
|
|
240
|
+
await hold(events, () => !events.paused.getValue());
|
|
241
|
+
return sent.conflicts;
|
|
242
|
+
}
|
|
243
|
+
await retryWait(events, state!.retryTime ?? retryTime);
|
|
199
244
|
}
|
|
200
245
|
},
|
|
201
246
|
batchSize: adapter.push.batchSize,
|
|
@@ -208,7 +253,20 @@ export function startReplication<RxDocType, CheckpointType = any>({
|
|
|
208
253
|
state = replication;
|
|
209
254
|
// Cancelled or, for a one-shot replication, completed: RxDB cancels it either way.
|
|
210
255
|
const release = watchShared(adapter, collection);
|
|
211
|
-
|
|
256
|
+
// The re-probe (v2: a fresh engine re-probes the store, so only it clears the latch; apps/main/lib/create-app-engine.ts):
|
|
257
|
+
// when the latch is reset, pull again from the checkpoint and re-run the push.
|
|
258
|
+
let latched = Boolean(connectivity?.current().updateRequired);
|
|
259
|
+
// Not while paused: an upstream pass run while paused sends nothing yet stores its checkpoint (upstream.js 211,
|
|
260
|
+
// 287), and RxDB's start() re-syncs after a pause anyway (plugins/replication/index.js 110).
|
|
261
|
+
const probe: Subscription | undefined = connectivity?.snapshot$.subscribe(({ updateRequired }) => {
|
|
262
|
+
if (latched && !updateRequired && !tillStopped && !replication.isPaused()) replication.reSync();
|
|
263
|
+
latched = updateRequired !== null;
|
|
264
|
+
});
|
|
265
|
+
replication.canceled$.subscribe((canceled) => {
|
|
266
|
+
if (!canceled) return;
|
|
267
|
+
release();
|
|
268
|
+
probe?.unsubscribe();
|
|
269
|
+
});
|
|
212
270
|
const resume = async () => {
|
|
213
271
|
notice$.next(undefined);
|
|
214
272
|
tillStopped = false;
|