Split up tracker event types
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@welshman/app",
|
||||
"version": "0.2.0",
|
||||
"version": "0.2.1",
|
||||
"author": "hodlbod",
|
||||
"license": "MIT",
|
||||
"description": "A collection of svelte stores for use in building nostr client applications.",
|
||||
|
||||
+11
-22
@@ -9,26 +9,7 @@ export const relay = new LocalRelay(repository)
|
||||
|
||||
export const tracker = new Tracker()
|
||||
|
||||
// Adapt above objects to stores
|
||||
|
||||
export const makeRepositoryStore = ({throttle: t = 300}: {throttle?: number} = {}) =>
|
||||
custom(
|
||||
setter => {
|
||||
let onUpdate = () => setter(repository)
|
||||
|
||||
if (t) {
|
||||
onUpdate = throttle(t, onUpdate)
|
||||
}
|
||||
|
||||
onUpdate()
|
||||
repository.on("update", onUpdate)
|
||||
|
||||
return () => repository.off("update", onUpdate)
|
||||
},
|
||||
{
|
||||
set: (other: Repository) => repository.load(other.dump()),
|
||||
},
|
||||
)
|
||||
// Adapt objects to stores
|
||||
|
||||
export const makeTrackerStore = ({throttle: t = 300}: {throttle?: number} = {}) =>
|
||||
custom(
|
||||
@@ -40,9 +21,17 @@ export const makeTrackerStore = ({throttle: t = 300}: {throttle?: number} = {})
|
||||
}
|
||||
|
||||
onUpdate()
|
||||
tracker.on("update", onUpdate)
|
||||
tracker.on("add", onUpdate)
|
||||
tracker.on("remove", onUpdate)
|
||||
tracker.on("load", onUpdate)
|
||||
tracker.on("clear", onUpdate)
|
||||
|
||||
return () => tracker.off("update", onUpdate)
|
||||
return () => {
|
||||
tracker.off("add", onUpdate)
|
||||
tracker.off("remove", onUpdate)
|
||||
tracker.off("load", onUpdate)
|
||||
tracker.off("clear", onUpdate)
|
||||
}
|
||||
},
|
||||
{
|
||||
set: (other: Tracker) => tracker.load(other.relaysById),
|
||||
|
||||
@@ -37,7 +37,7 @@ export class RelaysStorageAdapter {
|
||||
}
|
||||
|
||||
sync() {
|
||||
return throttled(3000, relays).subscribe($relays => bulkPut(this.options.name, $relays))
|
||||
return throttled(3000, relays).subscribe($relays => Boolean(console.log('relays', $relays))||bulkPut(this.options.name, $relays))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -152,19 +152,29 @@ export class TrackerStorageAdapter {
|
||||
}
|
||||
|
||||
sync() {
|
||||
const onUpdate = throttle(3000, async () => {
|
||||
await bulkPut(
|
||||
const updateOne = (id: string, relay: string) =>
|
||||
bulkPut(this.options.name, [{id, relays: Array.from(this.options.tracker.getRelays(id))}])
|
||||
|
||||
const updateAll = () =>
|
||||
bulkPut(
|
||||
this.options.name,
|
||||
Array.from(this.options.tracker.relaysById.entries()).map(([id, relays]) => ({
|
||||
id,
|
||||
relays: Array.from(relays),
|
||||
})),
|
||||
)
|
||||
})
|
||||
|
||||
this.options.tracker.on("update", onUpdate)
|
||||
this.options.tracker.on("add", updateOne)
|
||||
this.options.tracker.on("remove", updateOne)
|
||||
this.options.tracker.on("load", updateAll)
|
||||
this.options.tracker.on("clear", updateAll)
|
||||
|
||||
return () => this.options.tracker.off("update", onUpdate)
|
||||
return () => {
|
||||
this.options.tracker.off("add", updateOne)
|
||||
this.options.tracker.off("remove", updateOne)
|
||||
this.options.tracker.off("load", updateAll)
|
||||
this.options.tracker.off("clear", updateAll)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -34,7 +34,7 @@ describe("Tracker", () => {
|
||||
|
||||
it("should not duplicate existing pairs", () => {
|
||||
const updateSpy = vi.fn()
|
||||
tracker.on("update", updateSpy)
|
||||
tracker.on("add", updateSpy)
|
||||
|
||||
tracker.addRelay("event1", "relay1")
|
||||
tracker.addRelay("event1", "relay1")
|
||||
@@ -42,15 +42,6 @@ describe("Tracker", () => {
|
||||
// expect(updateSpy).toHaveBeenCalledTimes(1)
|
||||
expect(tracker.getRelays("event1").size).toBe(1)
|
||||
})
|
||||
|
||||
it("should emit update event", () => {
|
||||
const updateSpy = vi.fn()
|
||||
tracker.on("update", updateSpy)
|
||||
|
||||
tracker.addRelay("event1", "relay1")
|
||||
|
||||
expect(updateSpy).toHaveBeenCalled()
|
||||
})
|
||||
})
|
||||
|
||||
describe("removeRelay", () => {
|
||||
@@ -68,7 +59,7 @@ describe("Tracker", () => {
|
||||
|
||||
it("should emit update event on successful removal", () => {
|
||||
const updateSpy = vi.fn()
|
||||
tracker.on("update", updateSpy)
|
||||
tracker.on("remove", updateSpy)
|
||||
|
||||
tracker.removeRelay("event1", "relay1")
|
||||
|
||||
@@ -77,7 +68,7 @@ describe("Tracker", () => {
|
||||
|
||||
it("should not emit update event if nothing was removed", () => {
|
||||
const updateSpy = vi.fn()
|
||||
tracker.on("update", updateSpy)
|
||||
tracker.on("remove", updateSpy)
|
||||
|
||||
tracker.removeRelay("nonexistent", "relay1")
|
||||
|
||||
@@ -142,7 +133,7 @@ describe("Tracker", () => {
|
||||
|
||||
it("should emit update event", () => {
|
||||
const updateSpy = vi.fn()
|
||||
tracker.on("update", updateSpy)
|
||||
tracker.on("load", updateSpy)
|
||||
|
||||
tracker.load(new Map())
|
||||
|
||||
@@ -165,7 +156,7 @@ describe("Tracker", () => {
|
||||
|
||||
it("should emit update event", () => {
|
||||
const updateSpy = vi.fn()
|
||||
tracker.on("update", updateSpy)
|
||||
tracker.on("clear", updateSpy)
|
||||
|
||||
tracker.clear()
|
||||
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@welshman/net",
|
||||
"version": "0.2.0",
|
||||
"version": "0.2.1",
|
||||
"author": "hodlbod",
|
||||
"license": "MIT",
|
||||
"description": "Utilities for connecting with nostr relays.",
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
import {Emitter, addToMapKey} from "@welshman/lib"
|
||||
|
||||
|
||||
export class Tracker extends Emitter {
|
||||
relaysById = new Map<string, Set<string>>()
|
||||
idsByRelay = new Map<string, Set<string>>()
|
||||
@@ -36,7 +37,7 @@ export class Tracker extends Emitter {
|
||||
this.relaysById.set(eventId, relays)
|
||||
this.idsByRelay.set(relay, ids)
|
||||
|
||||
this.emit("update")
|
||||
this.emit("add", eventId, relay)
|
||||
}
|
||||
|
||||
removeRelay = (eventId: string, relay: string) => {
|
||||
@@ -45,7 +46,7 @@ export class Tracker extends Emitter {
|
||||
|
||||
if (!didDeleteRelay && !didDeleteId) return
|
||||
|
||||
this.emit("update")
|
||||
this.emit("remove", eventId, relay)
|
||||
}
|
||||
|
||||
track = (eventId: string, relay: string) => {
|
||||
@@ -73,13 +74,13 @@ export class Tracker extends Emitter {
|
||||
}
|
||||
}
|
||||
|
||||
this.emit("update")
|
||||
this.emit("load")
|
||||
}
|
||||
|
||||
clear = () => {
|
||||
this.relaysById.clear()
|
||||
this.idsByRelay.clear()
|
||||
|
||||
this.emit("update")
|
||||
this.emit("clear")
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user