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 type {TrustedEvent, Filter} from "@welshman/util"
|
||||||
import {Tracker, subscribe as baseSubscribe} from "@welshman/net"
|
import {Tracker, subscribe as baseSubscribe} from "@welshman/net"
|
||||||
import type {SubscribeRequestWithHandlers} 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>()
|
export const repository = new Repository<TrustedEvent>()
|
||||||
|
|
||||||
@@ -13,6 +13,17 @@ export const relay = new Relay(repository)
|
|||||||
|
|
||||||
export const tracker = new Tracker()
|
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 type PartialSubscribeRequest = Partial<SubscribeRequestWithHandlers> & {filters: Filter[]}
|
||||||
|
|
||||||
export const subscribe = (request: PartialSubscribeRequest) => {
|
export const subscribe = (request: PartialSubscribeRequest) => {
|
||||||
|
|||||||
@@ -4,7 +4,8 @@ import {throttle} from "throttle-debounce"
|
|||||||
import {writable} from "svelte/store"
|
import {writable} from "svelte/store"
|
||||||
import type {Unsubscriber, Writable} from "svelte/store"
|
import type {Unsubscriber, Writable} from "svelte/store"
|
||||||
import {randomInt, fromPairs} from "@welshman/lib"
|
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>
|
export type Item = Record<string, any>
|
||||||
|
|
||||||
@@ -141,4 +142,22 @@ export const storageAdapters = {
|
|||||||
new Map(data.map(({key, value}) => [key, value])),
|
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 {stamp, own, hash} from "@welshman/signer"
|
||||||
import type {HashedEvent, EventTemplate, SignedEvent} from '@welshman/util'
|
import type {HashedEvent, EventTemplate, SignedEvent} from '@welshman/util'
|
||||||
import {publish, PublishStatus} from "@welshman/net"
|
import {publish, PublishStatus} from "@welshman/net"
|
||||||
import {repository} from './core'
|
import {repository, tracker} from './core'
|
||||||
import {pubkey, getSession, getSigner} from './session'
|
import {pubkey, getSession, getSigner} from './session'
|
||||||
|
|
||||||
export type PublishStatusData = {
|
export type PublishStatusData = {
|
||||||
@@ -52,6 +52,10 @@ thunkWorker.addGlobalHandler(async ({event, relays, resolve}: ThunkWithResolve)
|
|||||||
assoc(id, Object.assign(statusByUrl, {[url]: {id, url, status, message}})),
|
assoc(id, Object.assign(statusByUrl, {[url]: {id, url, status, message}})),
|
||||||
)
|
)
|
||||||
|
|
||||||
|
if (status === PublishStatus.Success) {
|
||||||
|
tracker.track(id, url)
|
||||||
|
}
|
||||||
|
|
||||||
if (
|
if (
|
||||||
Object.values(statusByUrl).filter(s => s.status !== PublishStatus.Pending).length ===
|
Object.values(statusByUrl).filter(s => s.status !== PublishStatus.Pending).length ===
|
||||||
relays.length
|
relays.length
|
||||||
|
|||||||
@@ -46,6 +46,11 @@ export class Tracker extends Emitter {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
load = (data: Tracker['data']) => {
|
||||||
|
this.data = data
|
||||||
|
this.emit('update')
|
||||||
|
}
|
||||||
|
|
||||||
clear = () => {
|
clear = () => {
|
||||||
this.data.clear()
|
this.data.clear()
|
||||||
this.emit('update')
|
this.emit('update')
|
||||||
|
|||||||
@@ -28,15 +28,23 @@ export const getter = <T>(store: Readable<T>) => {
|
|||||||
return () => value
|
return () => value
|
||||||
}
|
}
|
||||||
|
|
||||||
export function withGetter<T>(store: Writable<T>): Writable<T> & {get: () => T}
|
export type WritableWithGetter<T> = Writable<T> & {get: () => T}
|
||||||
export function withGetter<T>(store: Readable<T>): Readable<T> & {get: () => T}
|
export type ReadableWithGetter<T> = Readable<T> & {get: () => T}
|
||||||
|
|
||||||
|
export function withGetter<T>(store: Writable<T>): WritableWithGetter<T>
|
||||||
|
export function withGetter<T>(store: Readable<T>): ReadableWithGetter<T>
|
||||||
export function withGetter<T>(store: Readable<T> | Writable<T>) {
|
export function withGetter<T>(store: Readable<T> | Writable<T>) {
|
||||||
return {...store, get: getter<T>(store)}
|
return {...store, get: getter<T>(store)}
|
||||||
}
|
}
|
||||||
|
|
||||||
type Start<T> = (set: Subscriber<T>) => Unsubscriber
|
type Start<T> = (set: Subscriber<T>) => Unsubscriber
|
||||||
|
|
||||||
export const custom = <T>(start: Start<T>, opts: {throttle?: number} = {}) => {
|
export type CustomStoreOpts<T> = {
|
||||||
|
throttle?: number
|
||||||
|
set?: (x: T) => void
|
||||||
|
}
|
||||||
|
|
||||||
|
export const custom = <T>(start: Start<T>, opts: CustomStoreOpts<T> = {}): WritableWithGetter<T> => {
|
||||||
const subs: Subscriber<T>[] = []
|
const subs: Subscriber<T>[] = []
|
||||||
|
|
||||||
let value: T
|
let value: T
|
||||||
@@ -51,7 +59,17 @@ export const custom = <T>(start: Start<T>, opts: {throttle?: number} = {}) => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
return {
|
return {
|
||||||
set,
|
get: () => value,
|
||||||
|
set: (newValue: T) => {
|
||||||
|
set(newValue)
|
||||||
|
opts.set?.(newValue)
|
||||||
|
},
|
||||||
|
update: (f: (value: T) => T) => {
|
||||||
|
const newValue = f(value)
|
||||||
|
|
||||||
|
set(newValue)
|
||||||
|
opts.set?.(newValue)
|
||||||
|
},
|
||||||
subscribe: (sub: Subscriber<T>) => {
|
subscribe: (sub: Subscriber<T>) => {
|
||||||
if (opts.throttle) {
|
if (opts.throttle) {
|
||||||
sub = throttle(opts.throttle, sub)
|
sub = throttle(opts.throttle, sub)
|
||||||
|
|||||||
@@ -12,6 +12,7 @@ export const BOGUS_RELAY_URL = "bogus://welshman.relay"
|
|||||||
|
|
||||||
export type RelayProfile = {
|
export type RelayProfile = {
|
||||||
url: string
|
url: string
|
||||||
|
icon?: string
|
||||||
name?: string
|
name?: string
|
||||||
pubkey?: string
|
pubkey?: string
|
||||||
contact?: string
|
contact?: string
|
||||||
|
|||||||
Reference in New Issue
Block a user