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