Put tracker in a store
This commit is contained in:
@@ -3,7 +3,7 @@ import {Repository, Relay, LOCAL_RELAY_URL, getFilterResultCardinality} from "@w
|
||||
import type {TrustedEvent, Filter} from "@welshman/util"
|
||||
import {Tracker, subscribe as baseSubscribe} from "@welshman/net"
|
||||
import type {SubscribeRequestWithHandlers} from "@welshman/net"
|
||||
import {createEventStore} from "@welshman/store"
|
||||
import {createEventStore, custom} from "@welshman/store"
|
||||
|
||||
export const repository = new Repository<TrustedEvent>()
|
||||
|
||||
@@ -13,6 +13,17 @@ export const relay = new Relay(repository)
|
||||
|
||||
export const tracker = new Tracker()
|
||||
|
||||
export const trackerStore = custom(setter => {
|
||||
const onUpdate = () => setter(tracker)
|
||||
|
||||
onUpdate()
|
||||
tracker.on('update', onUpdate)
|
||||
|
||||
return () => tracker.off('update', onUpdate)
|
||||
}, {
|
||||
set: (other: Tracker) => tracker.load(other.data),
|
||||
})
|
||||
|
||||
export type PartialSubscribeRequest = Partial<SubscribeRequestWithHandlers> & {filters: Filter[]}
|
||||
|
||||
export const subscribe = (request: PartialSubscribeRequest) => {
|
||||
|
||||
@@ -4,7 +4,8 @@ import {throttle} from "throttle-debounce"
|
||||
import {writable} from "svelte/store"
|
||||
import type {Unsubscriber, Writable} from "svelte/store"
|
||||
import {randomInt, fromPairs} from "@welshman/lib"
|
||||
import {withGetter, adapter} from "@welshman/store"
|
||||
import type {Tracker} from "@welshman/net"
|
||||
import {withGetter, adapter, custom} from "@welshman/store"
|
||||
|
||||
export type Item = Record<string, any>
|
||||
|
||||
@@ -141,4 +142,22 @@ export const storageAdapters = {
|
||||
new Map(data.map(({key, value}) => [key, value])),
|
||||
}),
|
||||
}),
|
||||
fromTracker: (tracker: Tracker) => ({
|
||||
keyPath: 'key',
|
||||
store: custom(setter => {
|
||||
const onUpdate = () =>
|
||||
setter(
|
||||
Array.from(tracker.data.entries())
|
||||
.map(([key, urls]) => ({key, value: Array.from(urls)}))
|
||||
)
|
||||
|
||||
onUpdate()
|
||||
tracker.on('update', onUpdate)
|
||||
|
||||
return () => tracker.off('update', onUpdate)
|
||||
}, {
|
||||
set: (data: {key: string, value: string[]}[]) =>
|
||||
tracker.load(new Map(data.map(({key, value}) => [key, new Set(value)]))),
|
||||
}),
|
||||
}),
|
||||
}
|
||||
|
||||
@@ -3,7 +3,7 @@ import {Worker, assoc} from '@welshman/lib'
|
||||
import {stamp, own, hash} from "@welshman/signer"
|
||||
import type {HashedEvent, EventTemplate, SignedEvent} from '@welshman/util'
|
||||
import {publish, PublishStatus} from "@welshman/net"
|
||||
import {repository} from './core'
|
||||
import {repository, tracker} from './core'
|
||||
import {pubkey, getSession, getSigner} from './session'
|
||||
|
||||
export type PublishStatusData = {
|
||||
@@ -52,6 +52,10 @@ thunkWorker.addGlobalHandler(async ({event, relays, resolve}: ThunkWithResolve)
|
||||
assoc(id, Object.assign(statusByUrl, {[url]: {id, url, status, message}})),
|
||||
)
|
||||
|
||||
if (status === PublishStatus.Success) {
|
||||
tracker.track(id, url)
|
||||
}
|
||||
|
||||
if (
|
||||
Object.values(statusByUrl).filter(s => s.status !== PublishStatus.Pending).length ===
|
||||
relays.length
|
||||
|
||||
Reference in New Issue
Block a user