@tallyui/database 3.15.0 → 3.17.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 CHANGED
@@ -205,7 +205,7 @@ interface StartReplicationOptions<RxDocType, CheckpointType = any> {
205
205
  /**
206
206
  * Fed with every pull and push outcome. While its update-required latch is set, the pull returns empty pages and a
207
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.
208
+ * pull asks the store again and a held push goes. `resume()` keeps the latch, and the reset keeps a sign-in hold.
209
209
  */
210
210
  connectivity?: ConnectivityTracker;
211
211
  }
package/dist/index.js CHANGED
@@ -262,17 +262,14 @@ function startReplication({
262
262
  let state;
263
263
  const notice$ = new BehaviorSubject2(void 0);
264
264
  let failures = 0;
265
- let tillStopped = false;
265
+ const tillHeld$ = new BehaviorSubject2(false);
266
266
  let storeError;
267
267
  let storeRetryAt = 0;
268
268
  const setRetryTime = (ms) => {
269
269
  if (state) state.retryTime = ms;
270
270
  };
271
271
  const pullHandler = async (checkpoint, batchSize) => {
272
- if (tillStopped) {
273
- void state?.pause();
274
- return { documents: [], checkpoint };
275
- }
272
+ if (tillHeld$.getValue()) return { documents: [], checkpoint };
276
273
  if (connectivity?.current().updateRequired) return { documents: [], checkpoint };
277
274
  const wait = storeRetryAt - Date.now();
278
275
  if (wait > TIMER_SLACK_MS) {
@@ -300,8 +297,7 @@ function startReplication({
300
297
  const current = notice$.getValue();
301
298
  if (current?.code !== code || current.fixedBy !== kind) notice$.next(noticeFor(error, kind));
302
299
  if (kind === "till") {
303
- tillStopped = true;
304
- void state?.pause();
300
+ tillHeld$.next(true);
305
301
  return { documents: [], checkpoint };
306
302
  }
307
303
  const delay = Math.min(Math.max(maxRetryTimeMs, retryAfter(error)), MAX_RETRY_AFTER_MS);
@@ -317,21 +313,22 @@ function startReplication({
317
313
  replicationIdentifier: `${context.connectorId}-${collection.name}${version > 0 ? `-v${version}` : ""}`,
318
314
  pull: {
319
315
  handler: pullHandler,
320
- stream$: adapter.pull.stream$,
316
+ stream$: adapter.pull.stream$?.pipe(filter((event) => event === "RESYNC" || !tillHeld$.getValue())),
321
317
  batchSize: adapter.pull.batchSize
322
318
  },
323
319
  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
320
+ // A push is held, never thrown or dropped, while latched, held for sign-in or paused: before the send, and
321
+ // after it until the pause ends. RxDB 17.5 drops a paused push's answer (plugins/replication/index.js 269, upstream.js
326
322
  // 287), so its rows go twice. And the pages queued behind it (upstream.js 90-100) then find no rows and
327
323
  // store the up checkpoint anyway (upstream.js 211), so those rows never go. A failed push is retried
328
324
  // here, never thrown: RxDB's own retry wait (index.js 266) ends unsent if paused by then, with the same
329
325
  // loss. Only a cancel ends a hold, with nothing sent.
330
326
  handler: async (rows) => {
331
327
  const events = state.internalReplicationState.events;
332
- const isLatched = () => Boolean(connectivity?.current().updateRequired);
328
+ const isHeld = () => tillHeld$.getValue() || Boolean(connectivity?.current().updateRequired);
329
+ const wake$ = connectivity ? merge(connectivity.snapshot$, tillHeld$) : tillHeld$;
333
330
  for (; ; ) {
334
- await hold(events, () => !isLatched() && !events.paused.getValue(), connectivity?.snapshot$);
331
+ await hold(events, () => !isHeld() && !events.paused.getValue(), wake$);
335
332
  if (events.canceled.getValue()) return [];
336
333
  const sent = await adapter.push.handler(rows, context).then((conflicts) => ({ conflicts }), (error) => {
337
334
  connectivity?.recordFailure(error);
@@ -356,7 +353,7 @@ function startReplication({
356
353
  const release = watchShared(adapter, collection);
357
354
  let latched = Boolean(connectivity?.current().updateRequired);
358
355
  const probe = connectivity?.snapshot$.subscribe(({ updateRequired }) => {
359
- if (latched && !updateRequired && !tillStopped && !replication.isPaused()) replication.reSync();
356
+ if (latched && !updateRequired && !tillHeld$.getValue() && !replication.isPaused()) replication.reSync();
360
357
  latched = updateRequired !== null;
361
358
  });
362
359
  replication.canceled$.subscribe((canceled) => {
@@ -366,7 +363,7 @@ function startReplication({
366
363
  });
367
364
  const resume = async () => {
368
365
  notice$.next(void 0);
369
- tillStopped = false;
366
+ tillHeld$.next(false);
370
367
  storeError = void 0;
371
368
  storeRetryAt = 0;
372
369
  failures = 0;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tallyui/database",
3
- "version": "3.15.0",
3
+ "version": "3.17.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.15.0"
33
+ "@tallyui/core": "^3.17.0"
34
34
  },
35
35
  "scripts": {
36
36
  "build": "tsup",
@@ -65,7 +65,7 @@ export interface StartReplicationOptions<RxDocType, CheckpointType = any> {
65
65
  /**
66
66
  * Fed with every pull and push outcome. While its update-required latch is set, the pull returns empty pages and a
67
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.
68
+ * pull asks the store again and a held push goes. `resume()` keeps the latch, and the reset keeps a sign-in hold.
69
69
  */
70
70
  connectivity?: ConnectivityTracker;
71
71
  }
@@ -136,10 +136,13 @@ export function startReplication<RxDocType, CheckpointType = any>({
136
136
  connectivity,
137
137
  }: StartReplicationOptions<RxDocType, CheckpointType>): TallyReplicationState<RxDocType, CheckpointType> {
138
138
  // Pull errors fall in three classes by who can fix them (errorKind):
139
- // - till (sign in again): one notice, pause, and an empty page (RxDB stores no
140
- // checkpoint for it), so it costs one request. Until resume() the handler
141
- // returns empty pages without calling the adapter, because RxDB restarts a
142
- // paused loop (start() on page visibility, reSync(), stream$).
139
+ // - till (sign in again): one notice and an empty page (RxDB stores no
140
+ // checkpoint for it), so it costs one request. Then a hold until resume():
141
+ // the pull returns empty pages without calling the adapter, a push waits in
142
+ // its handler, and stream$ documents are dropped. Never RxDB's pause(): a
143
+ // pause before a push page reaches the handler drops it unsent, and the
144
+ // pages queued behind it store the up checkpoint past its rows (17.5
145
+ // plugins/replication/index.js 240, 269; upstream.js 211, 287; #645).
143
146
  // - store (the owner fixes it elsewhere): one notice, then rethrown with the
144
147
  // store delay. An earlier call rethrows the stored error without calling the
145
148
  // adapter, with retryTime set to the time left, so RxDB retries exactly then.
@@ -148,17 +151,14 @@ export function startReplication<RxDocType, CheckpointType = any>({
148
151
  let state: RxReplicationState<RxDocType, CheckpointType> | undefined;
149
152
  const notice$ = new BehaviorSubject<SyncNotice | undefined>(undefined);
150
153
  let failures = 0;
151
- let tillStopped = false;
154
+ const tillHeld$ = new BehaviorSubject(false);
152
155
  let storeError: unknown;
153
156
  let storeRetryAt = 0;
154
157
  const setRetryTime = (ms: number) => {
155
158
  if (state) state.retryTime = ms;
156
159
  };
157
160
  const pullHandler = async (checkpoint: CheckpointType | undefined, batchSize: number) => {
158
- if (tillStopped) {
159
- void state?.pause();
160
- return { documents: [], checkpoint };
161
- }
161
+ if (tillHeld$.getValue()) return { documents: [], checkpoint };
162
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
163
  // answer and send its rows again after the restart (17.5 replication-protocol/upstream.js), a doubled write.
164
164
  if (connectivity?.current().updateRequired) return { documents: [], checkpoint };
@@ -189,8 +189,7 @@ export function startReplication<RxDocType, CheckpointType = any>({
189
189
  // One notice per cause: a repeat keeps the first `since`; another cause replaces it.
190
190
  if (current?.code !== code || current.fixedBy !== kind) notice$.next(noticeFor(error, kind));
191
191
  if (kind === 'till') {
192
- tillStopped = true;
193
- void state?.pause();
192
+ tillHeld$.next(true);
194
193
  return { documents: [], checkpoint };
195
194
  }
196
195
  const delay = Math.min(Math.max(maxRetryTimeMs, retryAfter(error)), MAX_RETRY_AFTER_MS);
@@ -212,22 +211,23 @@ export function startReplication<RxDocType, CheckpointType = any>({
212
211
  replicationIdentifier: `${context.connectorId}-${collection.name}${version > 0 ? `-v${version}` : ''}`,
213
212
  pull: {
214
213
  handler: pullHandler,
215
- stream$: adapter.pull.stream$,
214
+ stream$: adapter.pull.stream$?.pipe(filter(event => event === 'RESYNC' || !tillHeld$.getValue())),
216
215
  batchSize: adapter.pull.batchSize,
217
216
  },
218
217
  push: adapter.push
219
218
  ? {
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
219
+ // A push is held, never thrown or dropped, while latched, held for sign-in or paused: before the send, and
220
+ // after it until the pause ends. RxDB 17.5 drops a paused push's answer (plugins/replication/index.js 269, upstream.js
222
221
  // 287), so its rows go twice. And the pages queued behind it (upstream.js 90-100) then find no rows and
223
222
  // store the up checkpoint anyway (upstream.js 211), so those rows never go. A failed push is retried
224
223
  // here, never thrown: RxDB's own retry wait (index.js 266) ends unsent if paused by then, with the same
225
224
  // loss. Only a cancel ends a hold, with nothing sent.
226
225
  handler: async (rows) => {
227
226
  const events = state!.internalReplicationState!.events as ProtocolEvents;
228
- const isLatched = () => Boolean(connectivity?.current().updateRequired);
227
+ const isHeld = () => tillHeld$.getValue() || Boolean(connectivity?.current().updateRequired);
228
+ const wake$ = connectivity ? merge(connectivity.snapshot$, tillHeld$) : tillHeld$;
229
229
  for (;;) {
230
- await hold(events, () => !isLatched() && !events.paused.getValue(), connectivity?.snapshot$);
230
+ await hold(events, () => !isHeld() && !events.paused.getValue(), wake$);
231
231
  if (events.canceled.getValue()) return [];
232
232
  const sent = await adapter.push!.handler(rows, context).then(conflicts => ({ conflicts }), (error: unknown) => {
233
233
  connectivity?.recordFailure(error);
@@ -259,7 +259,7 @@ export function startReplication<RxDocType, CheckpointType = any>({
259
259
  // Not while paused: an upstream pass run while paused sends nothing yet stores its checkpoint (upstream.js 211,
260
260
  // 287), and RxDB's start() re-syncs after a pause anyway (plugins/replication/index.js 110).
261
261
  const probe: Subscription | undefined = connectivity?.snapshot$.subscribe(({ updateRequired }) => {
262
- if (latched && !updateRequired && !tillStopped && !replication.isPaused()) replication.reSync();
262
+ if (latched && !updateRequired && !tillHeld$.getValue() && !replication.isPaused()) replication.reSync();
263
263
  latched = updateRequired !== null;
264
264
  });
265
265
  replication.canceled$.subscribe((canceled) => {
@@ -269,7 +269,7 @@ export function startReplication<RxDocType, CheckpointType = any>({
269
269
  });
270
270
  const resume = async () => {
271
271
  notice$.next(undefined);
272
- tillStopped = false;
272
+ tillHeld$.next(false);
273
273
  storeError = undefined;
274
274
  storeRetryAt = 0;
275
275
  failures = 0;