@arcote.tech/arc 0.0.20 → 0.0.22

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.
@@ -5,7 +5,7 @@ import { ArcCollectionQuery } from "./abstract-collection-query";
5
5
  export declare class QueryCollectionResult<C extends ArcCollectionAny> {
6
6
  private result;
7
7
  constructor(result: CollectionItem<C>[]);
8
- get(id: util.GetType<C["id"]>): ({
8
+ get(id: util.GetType<C["id"]> | null): ({
9
9
  _id: string;
10
10
  } & ReturnType<C["deserialize"]> extends infer T ? { [KeyType in keyof T]: ({
11
11
  _id: string;
@@ -9,7 +9,7 @@ export declare class ForkedDataStorage extends DataStorage {
9
9
  getReadWriteTransaction(): Promise<ReadWriteTransaction>;
10
10
  getStore<Item extends {
11
11
  _id: string;
12
- }>(storeName: string, deserialize?: (data: any) => Item): StoreState<Item>;
12
+ }>(storeName: string): StoreState<Item>;
13
13
  fork(): ForkedDataStorage;
14
14
  merge(): Promise<void>;
15
15
  }
@@ -5,7 +5,7 @@ import type { StoreState } from "./store-state.abstract";
5
5
  export declare abstract class DataStorage {
6
6
  abstract getStore<Item extends {
7
7
  _id: string;
8
- }>(storeName: string, deserialize?: (data: any) => Item): StoreState<Item>;
8
+ }>(storeName: string): StoreState<Item>;
9
9
  abstract fork(): ForkedDataStorage;
10
10
  abstract getReadTransaction(): Promise<ReadTransaction>;
11
11
  abstract getReadWriteTransaction(): Promise<ReadWriteTransaction>;
@@ -6,7 +6,7 @@ export declare class ForkedStoreState<Item extends {
6
6
  master: StoreState<Item>;
7
7
  protected changedItems: Map<string, Item | null>;
8
8
  changes: StoreStateChange<Item>[];
9
- constructor(storeName: string, dataStorage: DataStorage, master: StoreState<Item>, deserialize?: (data: any) => Item);
9
+ constructor(storeName: string, dataStorage: DataStorage, master: StoreState<Item>);
10
10
  applyChangeAndReturnEvent(change: StoreStateChange<Item>): Promise<{
11
11
  from: Item | null;
12
12
  to: Item | null;
@@ -5,7 +5,7 @@ export declare abstract class StoreState<Item extends {
5
5
  }> {
6
6
  storeName: string;
7
7
  protected dataStorage: DataStorage;
8
- protected deserialize?: ((data: any) => Item) | undefined;
8
+ deserialize?: ((data: any) => Item) | undefined;
9
9
  protected listeners: Map<QueryListenerCallback<Item>, QueryListener<Item>>;
10
10
  constructor(storeName: string, dataStorage: DataStorage, deserialize?: ((data: any) => Item) | undefined);
11
11
  abstract applyChange(change: StoreStateChange<Item>): Promise<{
package/dist/index.js CHANGED
@@ -55,7 +55,7 @@ class QueryCollectionResult {
55
55
  this.result = result;
56
56
  }
57
57
  get(id) {
58
- return this.result.find((r) => r._id === id);
58
+ return id ? this.result.find((r) => r._id === id) : undefined;
59
59
  }
60
60
  map(callbackfn) {
61
61
  return this.result.map(callbackfn);
@@ -358,7 +358,7 @@ class ArcIndexedCollection extends ArcCollection {
358
358
  if (!(name in this.indexes)) {
359
359
  throw new Error(`Index "${name}" not found in collection "${this.name}"`);
360
360
  }
361
- return (data) => dataStorage.getStore(this.name, this.deserialize.bind(this)).findByIndex(name, data);
361
+ return (data) => dataStorage.getStore(this.name).findByIndex(name, data);
362
362
  }
363
363
  });
364
364
  }
@@ -490,7 +490,7 @@ class StoreState {
490
490
  this.deserialize = deserialize;
491
491
  }
492
492
  fork() {
493
- return new ForkedStoreState(this.storeName, this.dataStorage, this, this.deserialize);
493
+ return new ForkedStoreState(this.storeName, this.dataStorage, this);
494
494
  }
495
495
  async set(item) {
496
496
  const change = {
@@ -543,8 +543,8 @@ class ForkedStoreState extends StoreState {
543
543
  master;
544
544
  changedItems = new Map;
545
545
  changes = [];
546
- constructor(storeName, dataStorage, master, deserialize) {
547
- super(storeName, dataStorage, deserialize);
546
+ constructor(storeName, dataStorage, master) {
547
+ super(storeName, dataStorage, master.deserialize);
548
548
  this.master = master;
549
549
  }
550
550
  async applyChangeAndReturnEvent(change) {
@@ -677,11 +677,11 @@ class ForkedDataStorage extends DataStorage {
677
677
  getReadWriteTransaction() {
678
678
  return this.master.getReadWriteTransaction();
679
679
  }
680
- getStore(storeName, deserialize) {
680
+ getStore(storeName) {
681
681
  if (this.stores.has(storeName))
682
682
  return this.stores.get(storeName);
683
- const masterStorage = this.master.getStore(storeName, deserialize);
684
- const storage = new ForkedStoreState(storeName, this, masterStorage, deserialize);
683
+ const masterStorage = this.master.getStore(storeName);
684
+ const storage = new ForkedStoreState(storeName, this, masterStorage);
685
685
  this.stores.set(storeName, storage);
686
686
  return storage;
687
687
  }
@@ -693,7 +693,6 @@ class ForkedDataStorage extends DataStorage {
693
693
  store: store.storeName,
694
694
  changes: store.changes
695
695
  }));
696
- console.log(changes);
697
696
  await this.master.commitChanges(changes);
698
697
  }
699
698
  }
@@ -736,7 +735,6 @@ class MasterStoreState extends StoreState {
736
735
  if (change.type === "modify") {
737
736
  const existing = await transaction.findById(this.storeName, change.id);
738
737
  const updated = existing ? deepMerge(existing, change.data) : { _id: change.id, ...change.data };
739
- console.log("updated", updated);
740
738
  await transaction.set(this.storeName, updated);
741
739
  const item = this.deserialize ? this.deserialize(updated) : updated;
742
740
  this.items.set(change.id, item);
@@ -1209,14 +1207,16 @@ class ArcStringEnum extends ArcAbstract {
1209
1207
  // rtc/client.ts
1210
1208
  class RTCClient {
1211
1209
  storage;
1210
+ token;
1212
1211
  _socket;
1213
1212
  openSocket;
1214
1213
  reconnectAttempts = 0;
1215
1214
  maxReconnectAttempts = 5;
1216
1215
  syncProgressCallback;
1217
1216
  syncPromise = null;
1218
- constructor(storage) {
1217
+ constructor(storage, token) {
1219
1218
  this.storage = storage;
1219
+ this.token = token;
1220
1220
  }
1221
1221
  async sync(progressCallback) {
1222
1222
  if (this.syncPromise)
@@ -1231,7 +1231,8 @@ class RTCClient {
1231
1231
  const response = await fetch(`/ws/sync?lastSync=${arcState?.lastSyncDate || ""}`, {
1232
1232
  method: "GET",
1233
1233
  headers: {
1234
- "Content-Type": "application/json"
1234
+ "Content-Type": "application/json",
1235
+ Authorization: `Bearer ${this.token}`
1235
1236
  }
1236
1237
  });
1237
1238
  if (!response.ok) {
@@ -1239,28 +1240,18 @@ class RTCClient {
1239
1240
  }
1240
1241
  const { results, syncDate } = await response.json();
1241
1242
  const pendingStoreChanges = [];
1243
+ const transaction = await this.storage.getReadWriteTransaction();
1242
1244
  for (const { store, items } of results) {
1243
1245
  this.syncProgressCallback?.({ store, size: items.length });
1244
- const storeState = this.storage.getStore(store);
1245
- if (!storeState) {
1246
- console.error(`Store ${store} not found`);
1247
- continue;
1248
- }
1249
- const changes = items.map((item) => {
1246
+ for (const item of items) {
1250
1247
  if (item.deleted) {
1251
- return {
1252
- type: "delete",
1253
- id: item._id
1254
- };
1248
+ await transaction.remove(store, item._id);
1249
+ } else {
1250
+ await transaction.set(store, item);
1255
1251
  }
1256
- return {
1257
- type: "set",
1258
- data: item
1259
- };
1260
- });
1261
- pendingStoreChanges.push(storeState.applyChanges(changes));
1252
+ }
1262
1253
  }
1263
- await Promise.all(pendingStoreChanges);
1254
+ await transaction.commit();
1264
1255
  const stateStorage = this.storage.getStore("state");
1265
1256
  await stateStorage.applyChanges([
1266
1257
  {
@@ -1270,7 +1261,7 @@ class RTCClient {
1270
1261
  ]);
1271
1262
  }
1272
1263
  async connectWebSocket() {
1273
- this._socket = new WebSocket(`wss://${window.location.host}/ws`);
1264
+ this._socket = new WebSocket(`wss://${window.location.host}/ws?token=${this.token}`);
1274
1265
  this.openSocket = new Promise((resolve) => {
1275
1266
  this._socket.addEventListener("open", () => {
1276
1267
  this.reconnectAttempts = 0;
@@ -1317,8 +1308,8 @@ class RTCClient {
1317
1308
  socket.send(JSON.stringify(message));
1318
1309
  }
1319
1310
  }
1320
- var rtcClientFactory = (storage) => {
1321
- return new RTCClient(storage);
1311
+ var rtcClientFactory = (token) => (storage) => {
1312
+ return new RTCClient(storage, token);
1322
1313
  };
1323
1314
  // state/query.ts
1324
1315
  class ArcStateQuery extends ArcQuery {
@@ -1,3 +1,3 @@
1
1
  import type { RealTimeCommunicationAdapterFactory } from "./rtc";
2
- export declare const rtcClientFactory: RealTimeCommunicationAdapterFactory;
2
+ export declare const rtcClientFactory: (token: string) => RealTimeCommunicationAdapterFactory;
3
3
  //# sourceMappingURL=client.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.20",
7
+ "version": "0.0.22",
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.",