Tracker
Tracker records which relays have sent or accepted each event, as a two-way map between event ids and relay urls. request uses one to detect events that arrive from more than one relay. Every repository owns one as repository.tracker, which an App reaches as app.repository.tracker.
Tracker
Properties:
relaysById-Map<string, Set<string>>from event id to relay urlsidsByRelay-Map<string, Set<string>>from relay url to event ids
Events:
"add"- Emitted with(eventId, relay)when a new pair is recorded"remove"- Emitted with(eventId, relay)when a pair is removed"load"- Emitted byload()"clear"- Emitted byclear()
Methods:
track(eventId, relay)- Records the pair and returns whether the event was already recorded for any relayaddRelay(eventId, relay)/removeRelay(eventId, relay)- Records or removes one pair. Removing an event's last relay forgets the event, so a latertrackreturnsfalsehasRelay(eventId, relay)- Whether the pair is recordedgetRelays(eventId)- The relays recorded for an eventgetIds(relay)- The events recorded for a relaycopy(eventId1, eventId2)- Records every relay of the first event for the second, asWrapManagerdoes from a gift wrap to its rumorhydrate(eventId, relays)- Records relays an event was seen on before, as when a repository reads its provenance back from storageload(relaysById)- Adds aMap<string, Set<string>>of pairs to those already recorded. Callclear()first to replace themclear()- Removes every pair
load emits only "load", not an "add" per pair, and hydrate emits nothing.
Example
typescript
import {Tracker} from "@welshman/store"
import {load} from "@welshman/net"
const tracker = new Tracker()
await load({
relays: ["wss://relay1.com", "wss://relay2.com"],
filters: [{kinds: [1], limit: 10}],
tracker,
context,
})
for (const id of tracker.getIds("wss://relay1.com")) {
console.log(id, "also on", Array.from(tracker.getRelays(id)))
}