cry-synced-db-client 0.1.9 → 0.1.10

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.
@@ -36,6 +36,8 @@ export declare class SyncedDb implements I_SyncedDb {
36
36
  private restUploadTimer?;
37
37
  /** Flag za preprečitev loopa - če je true, ne schedulamo REST uploada */
38
38
  private isUploadingToRest;
39
+ /** Promise ki se resolva ko se REST upload konča (za sync await) */
40
+ private restUploadPromise?;
39
41
  /** Timer za avtomatsko sinhronizacijo */
40
42
  private autoSyncTimer?;
41
43
  /** Unikatni ID te instance za detekcijo loopback */
@@ -44,7 +46,7 @@ export declare class SyncedDb implements I_SyncedDb {
44
46
  init(): Promise<void>;
45
47
  close(): Promise<void>;
46
48
  isOnline(): boolean;
47
- setOnline(online: boolean): void;
49
+ setOnline(online: boolean): Promise<void>;
48
50
  /**
49
51
  * Prisilno preide v offline stanje in pokliče callback če je nastavljen
50
52
  */
package/dist/index.js CHANGED
@@ -286,6 +286,7 @@ class SyncedDb {
286
286
  beforeUnloadHandler;
287
287
  restUploadTimer;
288
288
  isUploadingToRest = false;
289
+ restUploadPromise;
289
290
  autoSyncTimer;
290
291
  updaterId;
291
292
  constructor(config) {
@@ -354,12 +355,10 @@ class SyncedDb {
354
355
  isOnline() {
355
356
  return this.online;
356
357
  }
357
- setOnline(online) {
358
+ async setOnline(online) {
358
359
  const wasOffline = !this.online;
359
360
  if (online && wasOffline && this.initialized) {
360
- this.tryGoOnline().catch((err) => {
361
- console.error("Failed to go online:", err);
362
- });
361
+ await this.tryGoOnline();
363
362
  } else {
364
363
  this.online = online;
365
364
  if (!online) {
@@ -653,6 +652,13 @@ class SyncedDb {
653
652
  let sentCount = 0;
654
653
  let conflictsResolved = 0;
655
654
  try {
655
+ if (this.restUploadTimer) {
656
+ clearTimeout(this.restUploadTimer);
657
+ this.restUploadTimer = undefined;
658
+ }
659
+ if (this.restUploadPromise) {
660
+ await this.restUploadPromise;
661
+ }
656
662
  await this.flushAllPendingChanges();
657
663
  const syncSpecs = [];
658
664
  const configMap = new Map;
@@ -928,12 +934,18 @@ class SyncedDb {
928
934
  return;
929
935
  }
930
936
  this.isUploadingToRest = true;
937
+ let resolveUpload;
938
+ this.restUploadPromise = new Promise((resolve) => {
939
+ resolveUpload = resolve;
940
+ });
931
941
  try {
932
942
  await this.uploadDirtyItems();
933
943
  } catch (err) {
934
944
  console.error("REST upload failed:", err);
935
945
  } finally {
936
946
  this.isUploadingToRest = false;
947
+ resolveUpload();
948
+ this.restUploadPromise = undefined;
937
949
  }
938
950
  }
939
951
  async flushAllPendingChanges() {
@@ -981,7 +993,11 @@ class SyncedDb {
981
993
  const results = await this.withSyncTimeout(this.restInterface.updateCollections(collectionBatches), "updateCollections");
982
994
  let sentCount = 0;
983
995
  for (const result of results) {
984
- const { results: { updatedIds, deletedIds } } = result;
996
+ const { results: { insertedIds, updatedIds, deletedIds } } = result;
997
+ for (const id of insertedIds) {
998
+ await this.dexieDb.save(collection, id, { _dirty: false });
999
+ sentCount++;
1000
+ }
985
1001
  for (const id of updatedIds) {
986
1002
  await this.dexieDb.save(collection, id, { _dirty: false });
987
1003
  sentCount++;
@@ -1168,10 +1184,17 @@ class SyncedDb {
1168
1184
  }
1169
1185
  let sentCount = 0;
1170
1186
  for (const result of results) {
1171
- const { collection, results: { updatedIds, deletedIds, errors } } = result;
1187
+ const { collection, results: { insertedIds, updatedIds, deletedIds, errors } } = result;
1172
1188
  const dirtyData = dirtyItemsMap.get(collection);
1173
1189
  if (!dirtyData)
1174
1190
  continue;
1191
+ for (const id of insertedIds) {
1192
+ const item = dirtyData.updates.find((u) => String(u._id) === String(id));
1193
+ if (item) {
1194
+ await this.dexieDb.save(collection, id, { _dirty: false });
1195
+ sentCount++;
1196
+ }
1197
+ }
1175
1198
  for (const id of updatedIds) {
1176
1199
  const item = dirtyData.updates.find((u) => String(u._id) === String(id));
1177
1200
  if (item) {
@@ -54,6 +54,8 @@ export type CollectionUpdateRequest<T> = {
54
54
  export type CollectionUpdateResult = {
55
55
  collection: string;
56
56
  results: {
57
+ /** IDs of records actually inserted */
58
+ insertedIds: Id[];
57
59
  /** IDs of records actually updated */
58
60
  updatedIds: Id[];
59
61
  /** IDs of records actually deleted */
@@ -183,8 +183,8 @@ export interface I_SyncedDb {
183
183
  close(): Promise<void>;
184
184
  /** Ali je povezan na server */
185
185
  isOnline(): boolean;
186
- /** Nastavi online/offline status */
187
- setOnline(online: boolean): void;
186
+ /** Nastavi online/offline status. Returns a promise when going online. */
187
+ setOnline(online: boolean): Promise<void>;
188
188
  /** Poišče objekt po ID-ju */
189
189
  findById<T extends DbEntity>(collection: string, id: Id): Promise<T | null>;
190
190
  /** Poišče objekte po ID-jih */
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "cry-synced-db-client",
3
- "version": "0.1.9",
3
+ "version": "0.1.10",
4
4
  "type": "module",
5
5
  "main": "./dist/index.js",
6
6
  "module": "./dist/index.js",