cry-synced-db-client 0.1.19 → 0.1.21

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.
@@ -52,6 +52,10 @@ export declare class SyncedDb implements I_SyncedDb {
52
52
  init(): Promise<void>;
53
53
  close(): Promise<void>;
54
54
  isOnline(): boolean;
55
+ /** Can we sync with server? Requires online AND not forced offline */
56
+ private canSync;
57
+ /** Can we process incoming server updates? Requires not forced offline */
58
+ private canReceiveServerUpdates;
55
59
  forceOffline(forced: boolean): void;
56
60
  isForcedOffline(): boolean;
57
61
  setOnline(online: boolean): Promise<void>;
@@ -60,10 +64,6 @@ export declare class SyncedDb implements I_SyncedDb {
60
64
  */
61
65
  private goOffline;
62
66
  private tryGoOnline;
63
- /**
64
- * Zažene avtomatsko periodično sinhronizacijo če je autoSyncIntervalMs konfiguriran.
65
- * Safe to call multiple times - bo najprej ustavil obstoječi timer.
66
- */
67
67
  private startAutoSync;
68
68
  /**
69
69
  * Ustavi avtomatsko periodično sinhronizacijo.
package/dist/index.js CHANGED
@@ -370,26 +370,31 @@ class SyncedDb {
370
370
  isOnline() {
371
371
  return this.online && !this.forcedOffline;
372
372
  }
373
+ canSync() {
374
+ return this.online && !this.forcedOffline;
375
+ }
376
+ canReceiveServerUpdates() {
377
+ return !this.forcedOffline;
378
+ }
373
379
  forceOffline(forced) {
374
- const wasForced = this.forcedOffline;
380
+ if (this.forcedOffline === forced)
381
+ return;
375
382
  this.forcedOffline = forced;
376
383
  if (forced) {
377
384
  this.stopAutoSync();
378
385
  } else if (this.online) {
379
- this.startAutoSync();
380
- if (wasForced) {
381
- this.sync("forceOffline released").catch((err) => {
382
- console.error("Sync after forceOffline release failed:", err);
383
- });
384
- }
386
+ this.tryGoOnline().catch((err) => {
387
+ console.error("Failed to go online after forceOffline release:", err);
388
+ });
385
389
  }
386
390
  }
387
391
  isForcedOffline() {
388
392
  return this.forcedOffline;
389
393
  }
390
394
  async setOnline(online) {
391
- const wasOffline = !this.online;
392
- if (online && wasOffline && this.initialized) {
395
+ if (this.online === online)
396
+ return;
397
+ if (online && this.initialized) {
393
398
  await this.tryGoOnline();
394
399
  } else {
395
400
  this.online = online;
@@ -410,6 +415,9 @@ class SyncedDb {
410
415
  }
411
416
  }
412
417
  async tryGoOnline() {
418
+ if (this.forcedOffline) {
419
+ return;
420
+ }
413
421
  try {
414
422
  const pingResult = await this.withSyncTimeout(this.restInterface.ping(), "ping");
415
423
  if (!pingResult) {
@@ -426,7 +434,7 @@ class SyncedDb {
426
434
  }
427
435
  startAutoSync() {
428
436
  this.stopAutoSync();
429
- if (!this.autoSyncIntervalMs || this.autoSyncIntervalMs <= 0) {
437
+ if (this.forcedOffline || !this.autoSyncIntervalMs || this.autoSyncIntervalMs <= 0) {
430
438
  return;
431
439
  }
432
440
  const intervalMs = this.autoSyncIntervalMs;
@@ -675,10 +683,13 @@ class SyncedDb {
675
683
  }
676
684
  }
677
685
  async sync(calledFrom) {
678
- if (this.forcedOffline) {
679
- throw new Error("Cannot sync while in forced offline mode");
686
+ if (!this.canSync()) {
687
+ if (this.forcedOffline) {
688
+ throw new Error("Cannot sync while in forced offline mode");
689
+ }
690
+ return;
680
691
  }
681
- if (!this.online || this.syncLock)
692
+ if (this.syncLock)
682
693
  return;
683
694
  this.syncLock = true;
684
695
  this.syncing = true;
@@ -979,7 +990,7 @@ class SyncedDb {
979
990
  }
980
991
  }
981
992
  scheduleRestUpload() {
982
- if (!this.isOnline() || this.isUploadingToRest || this.syncing) {
993
+ if (!this.canSync() || this.isUploadingToRest || this.syncing) {
983
994
  return;
984
995
  }
985
996
  if (this.restUploadTimer) {
@@ -990,7 +1001,7 @@ class SyncedDb {
990
1001
  }, this.debounceRestWritesMs);
991
1002
  }
992
1003
  async executeRestUpload() {
993
- if (this.isUploadingToRest || this.syncing || !this.isOnline()) {
1004
+ if (!this.canSync() || this.isUploadingToRest || this.syncing) {
994
1005
  return;
995
1006
  }
996
1007
  this.isUploadingToRest = true;
@@ -1288,7 +1299,7 @@ class SyncedDb {
1288
1299
  return aI - bI;
1289
1300
  }
1290
1301
  async handleServerUpdate(payload) {
1291
- if (this.forcedOffline)
1302
+ if (!this.canReceiveServerUpdates())
1292
1303
  return;
1293
1304
  const collectionName = payload.collection;
1294
1305
  if (!this.collections.has(collectionName))
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "cry-synced-db-client",
3
- "version": "0.1.19",
3
+ "version": "0.1.21",
4
4
  "type": "module",
5
5
  "main": "./dist/index.js",
6
6
  "module": "./dist/index.js",