rework context
This commit is contained in:
@@ -1,3 +1,4 @@
|
||||
export * from './context'
|
||||
export * from './core'
|
||||
export * from './collection'
|
||||
export * from './freshness'
|
||||
@@ -15,34 +16,3 @@ export * from './thunk'
|
||||
export * from './topics'
|
||||
export * from './util'
|
||||
export * from './zappers'
|
||||
|
||||
import {partition} from "@welshman/lib"
|
||||
import {type Subscription, NetworkContext, defaultOptimizeSubscriptions} from "@welshman/net"
|
||||
import {type TrustedEvent, unionFilters, isSignedEvent, hasValidSignature} from "@welshman/util"
|
||||
import {tracker, repository, AppContext} from './core'
|
||||
import {makeRouter, getFilterSelections} from './router'
|
||||
import {onAuth, getSession} from './session'
|
||||
|
||||
export function* optimizeSubscriptions(subs: Subscription[]) {
|
||||
const [withRelays, withoutRelays] = partition(sub => sub.request.relays.length > 0, subs)
|
||||
const filters = unionFilters(withoutRelays.flatMap(sub => sub.request.filters))
|
||||
|
||||
yield* defaultOptimizeSubscriptions(withRelays)
|
||||
|
||||
if (filters.length > 0) {
|
||||
yield* getFilterSelections(filters)
|
||||
}
|
||||
}
|
||||
|
||||
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,
|
||||
})
|
||||
|
||||
Object.assign(AppContext, {
|
||||
router: makeRouter(),
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user