Add shortcut handlers to subscribe
This commit is contained in:
@@ -1,10 +1,6 @@
|
||||
import {readable, derived, type Readable} from 'svelte/store'
|
||||
import {type SubscribeRequest} from "@welshman/net"
|
||||
import {indexBy, type Maybe, now} from '@welshman/lib'
|
||||
import {getIdFilters} from '@welshman/util'
|
||||
import type {TrustedEvent} from '@welshman/util'
|
||||
import {withGetter, deriveEvents} from '@welshman/store'
|
||||
import {repository, load} from './core'
|
||||
import {withGetter} from '@welshman/store'
|
||||
import {getFreshness, setFreshness} from './freshness'
|
||||
|
||||
export const collection = <T, LoadArgs extends any[]>({
|
||||
@@ -61,21 +57,3 @@ export const collection = <T, LoadArgs extends any[]>({
|
||||
|
||||
return {indexStore, deriveItem, loadItem, getItem}
|
||||
}
|
||||
|
||||
export const deriveEvent = (idOrAddress: string, request: Partial<SubscribeRequest> = {}) => {
|
||||
let attempted = false
|
||||
|
||||
const filters = getIdFilters([idOrAddress])
|
||||
|
||||
return derived(
|
||||
deriveEvents(repository, {filters, includeDeleted: true}),
|
||||
(events: TrustedEvent[]) => {
|
||||
if (!attempted && events.length === 0) {
|
||||
load({...request, filters})
|
||||
attempted = true
|
||||
}
|
||||
|
||||
return events[0]
|
||||
},
|
||||
)
|
||||
}
|
||||
|
||||
@@ -2,7 +2,7 @@ import {isNil} from "@welshman/lib"
|
||||
import {Repository, Relay, LOCAL_RELAY_URL, getFilterResultCardinality} from "@welshman/util"
|
||||
import type {TrustedEvent, Filter} from "@welshman/util"
|
||||
import {Tracker, subscribe as baseSubscribe} from "@welshman/net"
|
||||
import type {SubscribeRequest} from "@welshman/net"
|
||||
import type {SubscribeRequestWithHandlers} from "@welshman/net"
|
||||
import {createEventStore} from "@welshman/store"
|
||||
import type {Router} from './router'
|
||||
|
||||
@@ -11,7 +11,6 @@ export const AppContext: {
|
||||
requestDelay: number
|
||||
requestTimeout: number
|
||||
dufflepudUrl?: string
|
||||
splitRequest?: (req: PartialSubscribeRequest) => SubscribeRequest[]
|
||||
} = {
|
||||
router: undefined as unknown as Router,
|
||||
requestDelay: 50,
|
||||
@@ -26,7 +25,7 @@ export const relay = new Relay(repository)
|
||||
|
||||
export const tracker = new Tracker()
|
||||
|
||||
export type PartialSubscribeRequest = Partial<SubscribeRequest> & {filters: Filter[]}
|
||||
export type PartialSubscribeRequest = Partial<SubscribeRequestWithHandlers> & {filters: Filter[]}
|
||||
|
||||
export const subscribe = (request: PartialSubscribeRequest) => {
|
||||
const events: TrustedEvent[] = []
|
||||
|
||||
@@ -18,10 +18,10 @@ export * from './zappers'
|
||||
|
||||
import {partition} from "@welshman/lib"
|
||||
import {type Subscription, NetworkContext, defaultOptimizeSubscriptions} from "@welshman/net"
|
||||
import {type TrustedEvent, unionFilters} from "@welshman/util"
|
||||
import {type TrustedEvent, unionFilters, isSignedEvent, hasValidSignature} from "@welshman/util"
|
||||
import {tracker, repository, AppContext} from './core'
|
||||
import {makeRouter, getFilterSelections} from './router'
|
||||
import {onAuth} from './session'
|
||||
import {onAuth, getSession} from './session'
|
||||
|
||||
export function* optimizeSubscriptions(subs: Subscription[]) {
|
||||
const [withRelays, withoutRelays] = partition(sub => sub.request.relays.length > 0, subs)
|
||||
@@ -38,6 +38,8 @@ Object.assign(NetworkContext, {
|
||||
onAuth,
|
||||
onEvent: (url: string, event: TrustedEvent) => tracker.track(event.id, url),
|
||||
isDeleted: (url: string, event: TrustedEvent) => repository.isDeleted(event),
|
||||
hasValidSignature: (event: TrustedEvent) =>
|
||||
getSession(event.pubkey) || (isSignedEvent(event) && hasValidSignature(event)),
|
||||
optimizeSubscriptions,
|
||||
})
|
||||
|
||||
|
||||
Reference in New Issue
Block a user