@automerge/automerge-repo-react-hooks 2.5.2-alpha.3 → 2.5.2-alpha.5
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 +32 -12
- package/dist/index.js.map +1 -1
- package/dist/usePresence.d.ts +1 -1
- package/dist/usePresence.d.ts.map +1 -1
- package/package.json +3 -3
- package/src/usePresence.ts +15 -8
package/dist/index.js
CHANGED
|
@@ -6758,7 +6758,7 @@ class PeerStateView {
|
|
|
6758
6758
|
getLastSeenPeer(peers) {
|
|
6759
6759
|
let freshestLastSeenAt;
|
|
6760
6760
|
return peers.reduce((freshest, curr) => {
|
|
6761
|
-
const lastSeenAt = this.value[curr]?.
|
|
6761
|
+
const lastSeenAt = this.value[curr]?.lastSeenAt;
|
|
6762
6762
|
if (!lastSeenAt) {
|
|
6763
6763
|
return freshest;
|
|
6764
6764
|
}
|
|
@@ -6848,11 +6848,15 @@ 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
|
-
|
|
6859
|
+
lastSeenAt: Date.now(),
|
|
6856
6860
|
},
|
|
6857
6861
|
});
|
|
6858
6862
|
}
|
|
@@ -6874,7 +6878,7 @@ class PeerPresenceInfo {
|
|
|
6874
6878
|
deviceId,
|
|
6875
6879
|
userId,
|
|
6876
6880
|
lastActiveAt: now,
|
|
6877
|
-
|
|
6881
|
+
lastSeenAt: now,
|
|
6878
6882
|
value: {
|
|
6879
6883
|
...existingState,
|
|
6880
6884
|
...value,
|
|
@@ -6898,9 +6902,15 @@ class PeerPresenceInfo {
|
|
|
6898
6902
|
*/
|
|
6899
6903
|
prune() {
|
|
6900
6904
|
const threshold = Date.now() - this.ttl;
|
|
6901
|
-
|
|
6902
|
-
|
|
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;
|
|
6903
6912
|
})));
|
|
6913
|
+
return pruned;
|
|
6904
6914
|
}
|
|
6905
6915
|
/**
|
|
6906
6916
|
* Get a snapshot of the current peer states
|
|
@@ -7141,7 +7151,10 @@ class Presence extends EventEmitter {
|
|
|
7141
7151
|
// to minimize variance between peer expiration, since the heartbeat frequency
|
|
7142
7152
|
// is expected to be several times higher.
|
|
7143
7153
|
this.#pruningInterval = setInterval(() => {
|
|
7144
|
-
this.#peers.prune();
|
|
7154
|
+
const pruned = this.#peers.prune();
|
|
7155
|
+
if (pruned.length > 0) {
|
|
7156
|
+
this.emit("pruning", { pruned });
|
|
7157
|
+
}
|
|
7145
7158
|
}, this.#heartbeatMs);
|
|
7146
7159
|
}
|
|
7147
7160
|
stopPruningPeers() {
|
|
@@ -7180,16 +7193,23 @@ function usePresence({
|
|
|
7180
7193
|
presence.on("snapshot", () => setPeerStates(presence.getPeerStates()));
|
|
7181
7194
|
presence.on("update", () => setPeerStates(presence.getPeerStates()));
|
|
7182
7195
|
presence.on("goodbye", () => setPeerStates(presence.getPeerStates()));
|
|
7196
|
+
presence.on("pruning", () => setPeerStates(presence.getPeerStates()));
|
|
7183
7197
|
return () => {
|
|
7184
7198
|
presence.stop();
|
|
7185
7199
|
};
|
|
7186
7200
|
}, [presence, userId, deviceId, firstInitialState, firstOpts]);
|
|
7187
|
-
const start = useCallback(
|
|
7188
|
-
|
|
7189
|
-
initialState
|
|
7190
|
-
|
|
7191
|
-
|
|
7192
|
-
|
|
7201
|
+
const start = useCallback(
|
|
7202
|
+
(config) => {
|
|
7203
|
+
const initialState2 = config?.initialState ?? presence.getLocalState();
|
|
7204
|
+
const opts = {
|
|
7205
|
+
...firstOpts.current,
|
|
7206
|
+
...config,
|
|
7207
|
+
initialState: initialState2
|
|
7208
|
+
};
|
|
7209
|
+
presence.start(opts);
|
|
7210
|
+
},
|
|
7211
|
+
[presence, firstOpts]
|
|
7212
|
+
);
|
|
7193
7213
|
const stop = useCallback(() => {
|
|
7194
7214
|
presence.stop();
|
|
7195
7215
|
}, [presence]);
|