@automerge/automerge-repo-react-hooks 2.5.2-alpha.4 → 2.5.2-alpha.6

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
@@ -6848,11 +6848,14 @@ class PeerPresenceInfo {
6848
6848
  * @param peerId
6849
6849
  */
6850
6850
  markSeen(peerId) {
6851
+ if (!(peerId in this.#peerStates.value)) {
6852
+ // Ignore heartbeats from peers we have not seen before: they will send a snapshot
6853
+ return;
6854
+ }
6851
6855
  this.#peerStates = new PeerStateView({
6852
6856
  ...this.#peerStates.value,
6853
6857
  [peerId]: {
6854
6858
  ...this.#peerStates.value[peerId],
6855
- peerId,
6856
6859
  lastSeenAt: Date.now(),
6857
6860
  },
6858
6861
  });
@@ -6899,9 +6902,15 @@ class PeerPresenceInfo {
6899
6902
  */
6900
6903
  prune() {
6901
6904
  const threshold = Date.now() - this.ttl;
6902
- this.#peerStates = new PeerStateView(Object.fromEntries(Object.entries(this.#peerStates.value).filter(([, state]) => {
6903
- return state.lastSeenAt >= threshold;
6905
+ const pruned = [];
6906
+ this.#peerStates = new PeerStateView(Object.fromEntries(Object.entries(this.#peerStates.value).filter(([id, state]) => {
6907
+ const keep = state.lastSeenAt >= threshold;
6908
+ if (!keep) {
6909
+ pruned.push(id);
6910
+ }
6911
+ return keep;
6904
6912
  })));
6913
+ return pruned;
6905
6914
  }
6906
6915
  /**
6907
6916
  * Get a snapshot of the current peer states
@@ -7142,7 +7151,10 @@ class Presence extends EventEmitter {
7142
7151
  // to minimize variance between peer expiration, since the heartbeat frequency
7143
7152
  // is expected to be several times higher.
7144
7153
  this.#pruningInterval = setInterval(() => {
7145
- this.#peers.prune();
7154
+ const pruned = this.#peers.prune();
7155
+ if (pruned.length > 0) {
7156
+ this.emit("pruning", { pruned });
7157
+ }
7146
7158
  }, this.#heartbeatMs);
7147
7159
  }
7148
7160
  stopPruningPeers() {
@@ -7181,6 +7193,7 @@ function usePresence({
7181
7193
  presence.on("snapshot", () => setPeerStates(presence.getPeerStates()));
7182
7194
  presence.on("update", () => setPeerStates(presence.getPeerStates()));
7183
7195
  presence.on("goodbye", () => setPeerStates(presence.getPeerStates()));
7196
+ presence.on("pruning", () => setPeerStates(presence.getPeerStates()));
7184
7197
  return () => {
7185
7198
  presence.stop();
7186
7199
  };