@arcote.tech/arc 0.0.14 → 0.0.15

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.
@@ -16,5 +16,9 @@ export declare class MasterDataStorage implements DataStorage {
16
16
  _id: string;
17
17
  }>(storeName: string): StoreState<Item>;
18
18
  fork(): ForkedDataStorage;
19
+ sync(progressCallback: ({ store, size }: {
20
+ store: string;
21
+ size: number;
22
+ }) => void): Promise<void>;
19
23
  }
20
24
  //# sourceMappingURL=data-storage-master.d.ts.map
package/dist/index.js CHANGED
@@ -665,6 +665,9 @@ class MasterDataStorage {
665
665
  fork() {
666
666
  return new ForkedDataStorage(this);
667
667
  }
668
+ async sync(progressCallback) {
669
+ await this.rtcAdapter.sync(progressCallback);
670
+ }
668
671
  }
669
672
  // elements/optional.ts
670
673
  class ArcOptional {
@@ -934,9 +937,22 @@ class RTCClient {
934
937
  openSocket;
935
938
  reconnectAttempts = 0;
936
939
  maxReconnectAttempts = 5;
940
+ syncProgressCallback;
941
+ syncResolve;
942
+ syncPromise = null;
943
+ pendingStoreChanges = [];
937
944
  constructor(storage) {
938
945
  this.storage = storage;
939
- this.connect();
946
+ }
947
+ async sync(progressCallback) {
948
+ if (this.syncPromise)
949
+ return this.syncPromise;
950
+ this.syncProgressCallback = progressCallback;
951
+ this.syncPromise = new Promise((resolve) => {
952
+ this.syncResolve = resolve;
953
+ this.connect();
954
+ });
955
+ return this.syncPromise;
940
956
  }
941
957
  async connect() {
942
958
  this._socket = new WebSocket(`wss://${window.location.host}/ws`);
@@ -982,6 +998,7 @@ class RTCClient {
982
998
  case "sync-result":
983
999
  {
984
1000
  const { store, items } = message;
1001
+ this.syncProgressCallback?.({ store, size: items.length });
985
1002
  const storeState = this.storage.getStore(store);
986
1003
  if (!storeState) {
987
1004
  console.error(`Store ${store} not found`);
@@ -999,17 +1016,22 @@ class RTCClient {
999
1016
  data: item
1000
1017
  };
1001
1018
  });
1002
- storeState.applyChanges(changes);
1019
+ this.pendingStoreChanges.push(storeState.applyChanges(changes));
1003
1020
  }
1004
1021
  break;
1005
1022
  case "sync-done":
1006
- const stateStorage = this.storage.getStore("state");
1007
- stateStorage.applyChanges([
1008
- {
1009
- type: "set",
1010
- data: { _id: "$arc", lastSyncDate: message.date }
1011
- }
1012
- ]);
1023
+ Promise.all(this.pendingStoreChanges).then(() => {
1024
+ const stateStorage = this.storage.getStore("state");
1025
+ stateStorage.applyChanges([
1026
+ {
1027
+ type: "set",
1028
+ data: { _id: "$arc", lastSyncDate: message.date }
1029
+ }
1030
+ ]).then(() => {
1031
+ this.pendingStoreChanges = [];
1032
+ this.syncResolve?.();
1033
+ });
1034
+ });
1013
1035
  break;
1014
1036
  case "state-changes":
1015
1037
  break;
package/dist/rtc/rtc.d.ts CHANGED
@@ -1,6 +1,10 @@
1
1
  import type { DataStorage, StoreStateChange } from "../data-storage/data-storage.interface";
2
2
  export interface RealTimeCommunicationAdapter {
3
3
  commitChanges(changes: StoreStateChange<any>[]): void;
4
+ sync(progressCallback: ({ store, size }: {
5
+ store: string;
6
+ size: number;
7
+ }) => void): Promise<void>;
4
8
  }
5
9
  export type RealTimeCommunicationAdapterFactory = (storage: DataStorage) => RealTimeCommunicationAdapter;
6
10
  //# sourceMappingURL=rtc.d.ts.map
package/package.json CHANGED
@@ -4,7 +4,7 @@
4
4
  "main": "dist/index.js",
5
5
  "types": "dist/index.d.ts",
6
6
  "type": "module",
7
- "version": "0.0.14",
7
+ "version": "0.0.15",
8
8
  "private": false,
9
9
  "author": "Przemysław Krasiński [arcote.tech]",
10
10
  "description": "Arc is a framework designed to align code closely with business logic, streamlining development and enhancing productivity.",