@arcote.tech/arc 0.0.18 → 0.0.19

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.
Files changed (2) hide show
  1. package/dist/index.js +47 -51
  2. package/package.json +1 -1
package/dist/index.js CHANGED
@@ -1210,9 +1210,7 @@ class RTCClient {
1210
1210
  reconnectAttempts = 0;
1211
1211
  maxReconnectAttempts = 5;
1212
1212
  syncProgressCallback;
1213
- syncResolve;
1214
1213
  syncPromise = null;
1215
- pendingStoreChanges = [];
1216
1214
  constructor(storage) {
1217
1215
  this.storage = storage;
1218
1216
  }
@@ -1220,13 +1218,54 @@ class RTCClient {
1220
1218
  if (this.syncPromise)
1221
1219
  return this.syncPromise;
1222
1220
  this.syncProgressCallback = progressCallback;
1223
- this.syncPromise = new Promise((resolve) => {
1224
- this.syncResolve = resolve;
1225
- this.connect();
1226
- });
1221
+ this.syncPromise = this.performSync();
1227
1222
  return this.syncPromise;
1228
1223
  }
1229
- async connect() {
1224
+ async performSync() {
1225
+ this.connectWebSocket();
1226
+ const arcState = await this.storage.getStore("state").findById("$arc", (a) => a);
1227
+ const response = await fetch(`/ws/sync?lastSync=${arcState?.lastSyncDate || ""}`, {
1228
+ method: "GET",
1229
+ headers: {
1230
+ "Content-Type": "application/json"
1231
+ }
1232
+ });
1233
+ if (!response.ok) {
1234
+ throw new Error("Sync failed");
1235
+ }
1236
+ const { results, syncDate } = await response.json();
1237
+ const pendingStoreChanges = [];
1238
+ for (const { store, items } of results) {
1239
+ this.syncProgressCallback?.({ store, size: items.length });
1240
+ const storeState = this.storage.getStore(store);
1241
+ if (!storeState) {
1242
+ console.error(`Store ${store} not found`);
1243
+ continue;
1244
+ }
1245
+ const changes = items.map((item) => {
1246
+ if (item.deleted) {
1247
+ return {
1248
+ type: "delete",
1249
+ id: item._id
1250
+ };
1251
+ }
1252
+ return {
1253
+ type: "set",
1254
+ data: item
1255
+ };
1256
+ });
1257
+ pendingStoreChanges.push(storeState.applyChanges(changes));
1258
+ }
1259
+ await Promise.all(pendingStoreChanges);
1260
+ const stateStorage = this.storage.getStore("state");
1261
+ await stateStorage.applyChanges([
1262
+ {
1263
+ type: "set",
1264
+ data: { _id: "$arc", lastSyncDate: syncDate }
1265
+ }
1266
+ ]);
1267
+ }
1268
+ async connectWebSocket() {
1230
1269
  this._socket = new WebSocket(`wss://${window.location.host}/ws`);
1231
1270
  this.openSocket = new Promise((resolve) => {
1232
1271
  this._socket.addEventListener("open", () => {
@@ -1234,7 +1273,6 @@ class RTCClient {
1234
1273
  resolve(this._socket);
1235
1274
  });
1236
1275
  });
1237
- const arcState = await this.storage.getStore("state").findById("$arc", (a) => a);
1238
1276
  this._socket.addEventListener("message", (e) => {
1239
1277
  this.onMessage(JSON.parse(e.data));
1240
1278
  });
@@ -1243,17 +1281,13 @@ class RTCClient {
1243
1281
  console.error("WebSocket closed", e);
1244
1282
  this.reconnect();
1245
1283
  });
1246
- this.sendMessage({
1247
- type: "sync",
1248
- lastDate: arcState?.lastSyncDate || null
1249
- });
1250
1284
  }
1251
1285
  reconnect() {
1252
1286
  if (this.reconnectAttempts < this.maxReconnectAttempts) {
1253
1287
  const timeout = Math.pow(2, this.reconnectAttempts) * 1000;
1254
1288
  setTimeout(() => {
1255
1289
  this.reconnectAttempts++;
1256
- this.connect();
1290
+ this.connectWebSocket();
1257
1291
  }, timeout);
1258
1292
  } else {
1259
1293
  console.error("Max reconnect attempts reached. Giving up.");
@@ -1267,44 +1301,6 @@ class RTCClient {
1267
1301
  }
1268
1302
  onMessage(message) {
1269
1303
  switch (message.type) {
1270
- case "sync-result":
1271
- {
1272
- const { store, items } = message;
1273
- this.syncProgressCallback?.({ store, size: items.length });
1274
- const storeState = this.storage.getStore(store);
1275
- if (!storeState) {
1276
- console.error(`Store ${store} not found`);
1277
- return;
1278
- }
1279
- const changes = items.map((item) => {
1280
- if (item.deleted) {
1281
- return {
1282
- type: "delete",
1283
- id: item._id
1284
- };
1285
- }
1286
- return {
1287
- type: "set",
1288
- data: item
1289
- };
1290
- });
1291
- this.pendingStoreChanges.push(storeState.applyChanges(changes));
1292
- }
1293
- break;
1294
- case "sync-done":
1295
- Promise.all(this.pendingStoreChanges).then(() => {
1296
- const stateStorage = this.storage.getStore("state");
1297
- stateStorage.applyChanges([
1298
- {
1299
- type: "set",
1300
- data: { _id: "$arc", lastSyncDate: message.date }
1301
- }
1302
- ]).then(() => {
1303
- this.pendingStoreChanges = [];
1304
- this.syncResolve?.();
1305
- });
1306
- });
1307
- break;
1308
1304
  case "state-changes":
1309
1305
  this.storage.applyChanges(message.changes);
1310
1306
  break;
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.18",
7
+ "version": "0.0.19",
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.",