cry-synced-db-client 0.1.56 → 0.1.57

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.
package/dist/index.js CHANGED
@@ -2682,6 +2682,7 @@ class SyncedDb {
2682
2682
  syncedDbInstanceId;
2683
2683
  syncMetaCache = new Map;
2684
2684
  unsubscribeServerUpdates;
2685
+ cleanupNotifierCallbacks;
2685
2686
  beforeUnloadHandler;
2686
2687
  onSync;
2687
2688
  onConflictResolved;
@@ -2882,11 +2883,13 @@ class SyncedDb {
2882
2883
  this.crossTabSync.init();
2883
2884
  if (this.serverUpdateNotifier) {
2884
2885
  if (this.serverUpdateNotifier.setCallbacks) {
2885
- this.serverUpdateNotifier.setCallbacks({
2886
+ const cleanup = this.serverUpdateNotifier.setCallbacks({
2886
2887
  onConnect: () => this.connectionManager.callOnWsConnect(),
2887
2888
  onDisconnect: (reason) => this.connectionManager.callOnWsDisconnect(reason),
2888
2889
  onReconnect: (attempt) => this.connectionManager.callOnWsReconnect(attempt)
2889
2890
  });
2891
+ if (cleanup)
2892
+ this.cleanupNotifierCallbacks = cleanup;
2890
2893
  }
2891
2894
  this.unsubscribeServerUpdates = this.serverUpdateNotifier.subscribe((payload) => this.serverUpdateHandler.handleServerUpdate(payload));
2892
2895
  try {
@@ -2917,13 +2920,25 @@ class SyncedDb {
2917
2920
  this.unsubscribeServerUpdates();
2918
2921
  this.unsubscribeServerUpdates = undefined;
2919
2922
  }
2923
+ if (this.cleanupNotifierCallbacks) {
2924
+ this.cleanupNotifierCallbacks();
2925
+ this.cleanupNotifierCallbacks = undefined;
2926
+ }
2920
2927
  if (this.serverUpdateNotifier) {
2921
- await this.serverUpdateNotifier.disconnect();
2928
+ if (this.serverUpdateNotifier.dispose) {
2929
+ this.serverUpdateNotifier.dispose();
2930
+ } else {
2931
+ await this.serverUpdateNotifier.disconnect();
2932
+ }
2922
2933
  }
2923
2934
  if (typeof window !== "undefined" && this.beforeUnloadHandler) {
2924
2935
  window.removeEventListener("beforeunload", this.beforeUnloadHandler);
2925
2936
  this.beforeUnloadHandler = undefined;
2926
2937
  }
2938
+ this.syncMetaCache.clear();
2939
+ for (const collectionName of this.collections.keys()) {
2940
+ this.inMemManager.clearCollection(collectionName);
2941
+ }
2927
2942
  this.initialized = false;
2928
2943
  }
2929
2944
  isOnline() {
@@ -5773,13 +5788,17 @@ class RestProxy {
5773
5788
  }
5774
5789
  combineSignals(signal1, signal2) {
5775
5790
  const controller = new AbortController;
5776
- const abort = () => controller.abort();
5777
5791
  if (signal1.aborted || signal2.aborted) {
5778
5792
  controller.abort();
5779
5793
  return controller.signal;
5780
5794
  }
5781
- signal1.addEventListener("abort", abort, { once: true });
5782
- signal2.addEventListener("abort", abort, { once: true });
5795
+ const abort = () => {
5796
+ controller.abort();
5797
+ signal1.removeEventListener("abort", abort);
5798
+ signal2.removeEventListener("abort", abort);
5799
+ };
5800
+ signal1.addEventListener("abort", abort);
5801
+ signal2.addEventListener("abort", abort);
5783
5802
  return controller.signal;
5784
5803
  }
5785
5804
  async ping() {
@@ -5864,9 +5883,9 @@ class Ebus2ProxyServerUpdateNotifier {
5864
5883
  maxReconnectDelayMs;
5865
5884
  pingIntervalMs;
5866
5885
  pongTimeoutMs;
5867
- onWsConnect;
5868
- onWsDisconnect;
5869
- onWsReconnect;
5886
+ onWsConnectCallbacks = [];
5887
+ onWsDisconnectCallbacks = [];
5888
+ onWsReconnectCallbacks = [];
5870
5889
  onWsNotification;
5871
5890
  ws = null;
5872
5891
  callbacks = new Set;
@@ -5889,9 +5908,12 @@ class Ebus2ProxyServerUpdateNotifier {
5889
5908
  this.pingIntervalMs = config.pingIntervalMs ?? DEFAULT_PING_INTERVAL_MS;
5890
5909
  this.pongTimeoutMs = config.pongTimeoutMs ?? DEFAULT_PONG_TIMEOUT_MS;
5891
5910
  this.currentReconnectDelay = this.reconnectDelayMs;
5892
- this.onWsConnect = config.onWsConnect;
5893
- this.onWsDisconnect = config.onWsDisconnect;
5894
- this.onWsReconnect = config.onWsReconnect;
5911
+ if (config.onWsConnect)
5912
+ this.onWsConnectCallbacks.push(config.onWsConnect);
5913
+ if (config.onWsDisconnect)
5914
+ this.onWsDisconnectCallbacks.push(config.onWsDisconnect);
5915
+ if (config.onWsReconnect)
5916
+ this.onWsReconnectCallbacks.push(config.onWsReconnect);
5895
5917
  this.onWsNotification = config.onWsNotification;
5896
5918
  }
5897
5919
  subscribe(callback) {
@@ -5920,31 +5942,47 @@ class Ebus2ProxyServerUpdateNotifier {
5920
5942
  }
5921
5943
  this.connected = false;
5922
5944
  }
5945
+ dispose() {
5946
+ this.disconnect();
5947
+ this.callbacks.clear();
5948
+ this.onWsConnectCallbacks.length = 0;
5949
+ this.onWsDisconnectCallbacks.length = 0;
5950
+ this.onWsReconnectCallbacks.length = 0;
5951
+ }
5923
5952
  isConnected() {
5924
5953
  return this.connected && !this.forcedOffline;
5925
5954
  }
5926
5955
  setCallbacks(callbacks) {
5956
+ const addedCallbacks = {};
5927
5957
  if (callbacks.onConnect) {
5928
- const existingOnConnect = this.onWsConnect;
5929
- this.onWsConnect = () => {
5930
- existingOnConnect?.();
5931
- callbacks.onConnect();
5932
- };
5958
+ addedCallbacks.onConnect = callbacks.onConnect;
5959
+ this.onWsConnectCallbacks.push(callbacks.onConnect);
5933
5960
  }
5934
5961
  if (callbacks.onDisconnect) {
5935
- const existingOnDisconnect = this.onWsDisconnect;
5936
- this.onWsDisconnect = (reason) => {
5937
- existingOnDisconnect?.(reason);
5938
- callbacks.onDisconnect(reason);
5939
- };
5962
+ addedCallbacks.onDisconnect = callbacks.onDisconnect;
5963
+ this.onWsDisconnectCallbacks.push(callbacks.onDisconnect);
5940
5964
  }
5941
5965
  if (callbacks.onReconnect) {
5942
- const existingOnReconnect = this.onWsReconnect;
5943
- this.onWsReconnect = (attempt) => {
5944
- existingOnReconnect?.(attempt);
5945
- callbacks.onReconnect(attempt);
5946
- };
5966
+ addedCallbacks.onReconnect = callbacks.onReconnect;
5967
+ this.onWsReconnectCallbacks.push(callbacks.onReconnect);
5947
5968
  }
5969
+ return () => {
5970
+ if (addedCallbacks.onConnect) {
5971
+ const idx = this.onWsConnectCallbacks.indexOf(addedCallbacks.onConnect);
5972
+ if (idx !== -1)
5973
+ this.onWsConnectCallbacks.splice(idx, 1);
5974
+ }
5975
+ if (addedCallbacks.onDisconnect) {
5976
+ const idx = this.onWsDisconnectCallbacks.indexOf(addedCallbacks.onDisconnect);
5977
+ if (idx !== -1)
5978
+ this.onWsDisconnectCallbacks.splice(idx, 1);
5979
+ }
5980
+ if (addedCallbacks.onReconnect) {
5981
+ const idx = this.onWsReconnectCallbacks.indexOf(addedCallbacks.onReconnect);
5982
+ if (idx !== -1)
5983
+ this.onWsReconnectCallbacks.splice(idx, 1);
5984
+ }
5985
+ };
5948
5986
  }
5949
5987
  forceOffline(forced) {
5950
5988
  this.forcedOffline = forced;
@@ -6001,9 +6039,9 @@ class Ebus2ProxyServerUpdateNotifier {
6001
6039
  this.sendSubscribe(channel);
6002
6040
  }
6003
6041
  this.startPingInterval();
6004
- if (this.onWsConnect) {
6042
+ for (const callback of this.onWsConnectCallbacks) {
6005
6043
  try {
6006
- this.onWsConnect();
6044
+ callback();
6007
6045
  } catch (err) {
6008
6046
  console.error("onWsConnect callback failed:", err);
6009
6047
  }
@@ -6015,11 +6053,13 @@ class Ebus2ProxyServerUpdateNotifier {
6015
6053
  this.subscribedChannels.clear();
6016
6054
  this.clearTimers();
6017
6055
  const reason = event.reason || `Code: ${event.code}`;
6018
- if (wasConnected && this.onWsDisconnect) {
6019
- try {
6020
- this.onWsDisconnect(reason);
6021
- } catch (err) {
6022
- console.error("onWsDisconnect callback failed:", err);
6056
+ if (wasConnected) {
6057
+ for (const callback of this.onWsDisconnectCallbacks) {
6058
+ try {
6059
+ callback(reason);
6060
+ } catch (err) {
6061
+ console.error("onWsDisconnect callback failed:", err);
6062
+ }
6023
6063
  }
6024
6064
  }
6025
6065
  if (this.shouldReconnect && !this.forcedOffline) {
@@ -6087,9 +6127,9 @@ class Ebus2ProxyServerUpdateNotifier {
6087
6127
  return;
6088
6128
  }
6089
6129
  this.reconnectAttempt++;
6090
- if (this.onWsReconnect) {
6130
+ for (const callback of this.onWsReconnectCallbacks) {
6091
6131
  try {
6092
- this.onWsReconnect(this.reconnectAttempt);
6132
+ callback(this.reconnectAttempt);
6093
6133
  } catch (err) {
6094
6134
  console.error("onWsReconnect callback failed:", err);
6095
6135
  }
@@ -35,9 +35,9 @@ export declare class Ebus2ProxyServerUpdateNotifier implements I_ServerUpdateNot
35
35
  private maxReconnectDelayMs;
36
36
  private pingIntervalMs;
37
37
  private pongTimeoutMs;
38
- private onWsConnect?;
39
- private onWsDisconnect?;
40
- private onWsReconnect?;
38
+ private onWsConnectCallbacks;
39
+ private onWsDisconnectCallbacks;
40
+ private onWsReconnectCallbacks;
41
41
  private onWsNotification?;
42
42
  private ws;
43
43
  private callbacks;
@@ -54,12 +54,19 @@ export declare class Ebus2ProxyServerUpdateNotifier implements I_ServerUpdateNot
54
54
  subscribe(callback: ServerUpdateCallback): () => void;
55
55
  connect(): Promise<void>;
56
56
  disconnect(): Promise<void>;
57
+ /**
58
+ * Fully dispose of the notifier, clearing all callbacks and state.
59
+ * Call this when completely done with the notifier (e.g., on app shutdown).
60
+ * Unlike disconnect(), this also clears all registered callbacks.
61
+ */
62
+ dispose(): void;
57
63
  isConnected(): boolean;
58
64
  /**
59
65
  * Set connection lifecycle callbacks.
60
66
  * These are merged with any callbacks provided in the constructor config.
67
+ * Returns a cleanup function to remove the added callbacks.
61
68
  */
62
- setCallbacks(callbacks: ServerUpdateNotifierCallbacks): void;
69
+ setCallbacks(callbacks: ServerUpdateNotifierCallbacks): () => void;
63
70
  /**
64
71
  * Force offline mode - disconnects WebSocket and prevents reconnection
65
72
  */
@@ -28,6 +28,7 @@ export declare class SyncedDb implements I_SyncedDb {
28
28
  private readonly syncedDbInstanceId;
29
29
  private syncMetaCache;
30
30
  private unsubscribeServerUpdates?;
31
+ private cleanupNotifierCallbacks?;
31
32
  private beforeUnloadHandler?;
32
33
  private readonly onSync?;
33
34
  private readonly onConflictResolved?;
@@ -42,6 +42,14 @@ export interface I_ServerUpdateNotifier {
42
42
  * Set connection lifecycle callbacks.
43
43
  * These are called by the notifier implementation when connection state changes.
44
44
  * Optional method - implementations that don't support lifecycle events may not implement this.
45
+ * @returns Cleanup function to remove the added callbacks (optional)
45
46
  */
46
- setCallbacks?(callbacks: ServerUpdateNotifierCallbacks): void;
47
+ setCallbacks?(callbacks: ServerUpdateNotifierCallbacks): void | (() => void);
48
+ /**
49
+ * Fully dispose of the notifier, clearing all callbacks and state.
50
+ * Call this when completely done with the notifier (e.g., on app shutdown).
51
+ * Unlike disconnect(), this also clears all registered callbacks.
52
+ * Optional method.
53
+ */
54
+ dispose?(): void;
47
55
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "cry-synced-db-client",
3
- "version": "0.1.56",
3
+ "version": "0.1.57",
4
4
  "type": "module",
5
5
  "main": "./dist/index.js",
6
6
  "module": "./dist/index.js",