Rename request/publish functions
This commit is contained in:
@@ -2,7 +2,7 @@ import {hexToBytes} from "@noble/hashes/utils"
|
|||||||
import {getPublicKey, finalizeEvent} from "nostr-tools/pure"
|
import {getPublicKey, finalizeEvent} from "nostr-tools/pure"
|
||||||
import {now} from "@welshman/lib"
|
import {now} from "@welshman/lib"
|
||||||
import {TrustedEvent, StampedEvent, Filter} from "@welshman/util"
|
import {TrustedEvent, StampedEvent, Filter} from "@welshman/util"
|
||||||
import {multireq, multicast, PublishEvent, RequestEvent, AdapterContext} from "@welshman/net"
|
import {MultiRequest, MultiPublish, PublishEvent, RequestEvent, AdapterContext} from "@welshman/net"
|
||||||
|
|
||||||
export type DVMHandler = {
|
export type DVMHandler = {
|
||||||
stop?: () => void
|
stop?: () => void
|
||||||
@@ -47,10 +47,10 @@ export class DVM {
|
|||||||
filter["#p"] = [getPublicKey(hexToBytes(sk))]
|
filter["#p"] = [getPublicKey(hexToBytes(sk))]
|
||||||
}
|
}
|
||||||
|
|
||||||
const sub = multireq({relays, filter, context})
|
const req = new MultiRequest({relays, filter, context})
|
||||||
|
|
||||||
sub.on(RequestEvent.Event, (e: TrustedEvent, url: string) => this.onEvent(e))
|
req.on(RequestEvent.Event, (e: TrustedEvent, url: string) => this.onEvent(e))
|
||||||
sub.on(RequestEvent.Close, () => resolve())
|
req.on(RequestEvent.Close, () => resolve())
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -113,7 +113,7 @@ export class DVM {
|
|||||||
const event = finalizeEvent(template, hexToBytes(sk))
|
const event = finalizeEvent(template, hexToBytes(sk))
|
||||||
|
|
||||||
await new Promise<void>(resolve => {
|
await new Promise<void>(resolve => {
|
||||||
multicast({event, relays, context}).on(PublishEvent.Complete, resolve)
|
new MultiPublish({event, relays, context}).on(PublishEvent.Complete, resolve)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
import {Emitter, now} from "@welshman/lib"
|
import {Emitter, now} from "@welshman/lib"
|
||||||
import {TrustedEvent, SignedEvent, Filter} from "@welshman/util"
|
import {TrustedEvent, SignedEvent, Filter} from "@welshman/util"
|
||||||
import {multireq, multicast, Multireq, Multicast, RequestEvent, AdapterContext} from "@welshman/net"
|
import {MultiRequest, MultiPublish, RequestEvent, AdapterContext} from "@welshman/net"
|
||||||
|
|
||||||
export enum DVMEvent {
|
export enum DVMEvent {
|
||||||
Progress = "progress",
|
Progress = "progress",
|
||||||
@@ -19,8 +19,8 @@ export type DVMRequestOptions = {
|
|||||||
export type DVMRequest = {
|
export type DVMRequest = {
|
||||||
request: DVMRequestOptions
|
request: DVMRequestOptions
|
||||||
emitter: Emitter
|
emitter: Emitter
|
||||||
sub: Multireq
|
sub: MultiRequest
|
||||||
pub: Multicast
|
pub: MultiPublish
|
||||||
}
|
}
|
||||||
|
|
||||||
export const makeDvmRequest = (request: DVMRequestOptions) => {
|
export const makeDvmRequest = (request: DVMRequestOptions) => {
|
||||||
@@ -37,8 +37,8 @@ export const makeDvmRequest = (request: DVMRequestOptions) => {
|
|||||||
const kinds = reportProgress ? [kind, 7000] : [kind]
|
const kinds = reportProgress ? [kind, 7000] : [kind]
|
||||||
const filter: Filter = {kinds, since: now() - 60, "#e": [event.id]}
|
const filter: Filter = {kinds, since: now() - 60, "#e": [event.id]}
|
||||||
|
|
||||||
const sub = multireq({relays, filter, timeout, context})
|
const sub = new MultiRequest({relays, filter, timeout, context})
|
||||||
const pub = multicast({relays, event, timeout, context})
|
const pub = new MultiPublish({relays, event, timeout, context})
|
||||||
|
|
||||||
sub.on(RequestEvent.Event, (event: TrustedEvent, url: string) => {
|
sub.on(RequestEvent.Event, (event: TrustedEvent, url: string) => {
|
||||||
if (event.kind === 7000) {
|
if (event.kind === 7000) {
|
||||||
|
|||||||
+13
-19
@@ -21,9 +21,9 @@ export enum PublishEvent {
|
|||||||
Complete = "publish:event:complete",
|
Complete = "publish:event:complete",
|
||||||
}
|
}
|
||||||
|
|
||||||
// Unicast
|
// SinglePublish
|
||||||
|
|
||||||
export type UnicastEvents = {
|
export type SinglePublishEvents = {
|
||||||
[PublishEvent.Success]: (id: string, detail: string) => void
|
[PublishEvent.Success]: (id: string, detail: string) => void
|
||||||
[PublishEvent.Failure]: (id: string, detail: string) => void
|
[PublishEvent.Failure]: (id: string, detail: string) => void
|
||||||
[PublishEvent.Timeout]: () => void
|
[PublishEvent.Timeout]: () => void
|
||||||
@@ -31,26 +31,26 @@ export type UnicastEvents = {
|
|||||||
[PublishEvent.Complete]: () => void
|
[PublishEvent.Complete]: () => void
|
||||||
}
|
}
|
||||||
|
|
||||||
export type UnicastOptions = {
|
export type SinglePublishOptions = {
|
||||||
event: SignedEvent
|
event: SignedEvent
|
||||||
relay: string
|
relay: string
|
||||||
context?: AdapterContext
|
context?: AdapterContext
|
||||||
timeout?: number
|
timeout?: number
|
||||||
}
|
}
|
||||||
|
|
||||||
export class Unicast extends (EventEmitter as new () => TypedEmitter<UnicastEvents>) {
|
export class SinglePublish extends (EventEmitter as new () => TypedEmitter<SinglePublishEvents>) {
|
||||||
status = PublishStatus.Pending
|
status = PublishStatus.Pending
|
||||||
|
|
||||||
_unsubscriber: () => void
|
_unsubscriber: () => void
|
||||||
_adapter: AbstractAdapter
|
_adapter: AbstractAdapter
|
||||||
|
|
||||||
constructor(readonly options: UnicastOptions) {
|
constructor(readonly options: SinglePublishOptions) {
|
||||||
super()
|
super()
|
||||||
|
|
||||||
// Set up our adapter
|
// Set up our adapter
|
||||||
this._adapter = getAdapter(this.options.relay, this.options.context)
|
this._adapter = getAdapter(this.options.relay, this.options.context)
|
||||||
|
|
||||||
// Listen for Unicast result
|
// Listen for SinglePublish result
|
||||||
this._unsubscriber = on(
|
this._unsubscriber = on(
|
||||||
this._adapter,
|
this._adapter,
|
||||||
AdapterEvent.Receive,
|
AdapterEvent.Receive,
|
||||||
@@ -105,9 +105,9 @@ export class Unicast extends (EventEmitter as new () => TypedEmitter<UnicastEven
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Multicast
|
// MultiPublish
|
||||||
|
|
||||||
export type MulticastEvents = {
|
export type MultiPublishEvents = {
|
||||||
[PublishEvent.Success]: (id: string, detail: string, url: string) => void
|
[PublishEvent.Success]: (id: string, detail: string, url: string) => void
|
||||||
[PublishEvent.Failure]: (id: string, detail: string, url: string) => void
|
[PublishEvent.Failure]: (id: string, detail: string, url: string) => void
|
||||||
[PublishEvent.Timeout]: (url: string) => void
|
[PublishEvent.Timeout]: (url: string) => void
|
||||||
@@ -115,23 +115,23 @@ export type MulticastEvents = {
|
|||||||
[PublishEvent.Complete]: () => void
|
[PublishEvent.Complete]: () => void
|
||||||
}
|
}
|
||||||
|
|
||||||
export type MulticastOptions = Omit<UnicastOptions, "relay"> & {
|
export type MultiPublishOptions = Omit<SinglePublishOptions, "relay"> & {
|
||||||
relays: string[]
|
relays: string[]
|
||||||
}
|
}
|
||||||
|
|
||||||
export class Multicast extends (EventEmitter as new () => TypedEmitter<MulticastEvents>) {
|
export class MultiPublish extends (EventEmitter as new () => TypedEmitter<MultiPublishEvents>) {
|
||||||
status: Record<string, PublishStatus>
|
status: Record<string, PublishStatus>
|
||||||
|
|
||||||
_children: Unicast[] = []
|
_children: SinglePublish[] = []
|
||||||
_completed = new Set<string>()
|
_completed = new Set<string>()
|
||||||
|
|
||||||
constructor({relays, ...options}: MulticastOptions) {
|
constructor({relays, ...options}: MultiPublishOptions) {
|
||||||
super()
|
super()
|
||||||
|
|
||||||
this.status = fromPairs(relays.map(relay => [relay, PublishStatus.Pending]))
|
this.status = fromPairs(relays.map(relay => [relay, PublishStatus.Pending]))
|
||||||
|
|
||||||
for (const relay of relays) {
|
for (const relay of relays) {
|
||||||
const unicast = new Unicast({relay, ...options})
|
const unicast = new SinglePublish({relay, ...options})
|
||||||
|
|
||||||
unicast.on(PublishEvent.Success, (id: string, detail: string) => {
|
unicast.on(PublishEvent.Success, (id: string, detail: string) => {
|
||||||
this.status[relay] = unicast.status
|
this.status[relay] = unicast.status
|
||||||
@@ -177,9 +177,3 @@ export class Multicast extends (EventEmitter as new () => TypedEmitter<Multicast
|
|||||||
this.removeAllListeners()
|
this.removeAllListeners()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Convenience functions
|
|
||||||
|
|
||||||
export const unicast = (options: UnicastOptions) => new Unicast(options)
|
|
||||||
|
|
||||||
export const multicast = (options: MulticastOptions) => new Multicast(options)
|
|
||||||
|
|||||||
@@ -11,8 +11,8 @@ import {
|
|||||||
} from "./message.js"
|
} from "./message.js"
|
||||||
import {getAdapter, AdapterContext, AbstractAdapter, AdapterEvent} from "./adapter.js"
|
import {getAdapter, AdapterContext, AbstractAdapter, AdapterEvent} from "./adapter.js"
|
||||||
import {Negentropy, NegentropyStorageVector} from "./negentropy.js"
|
import {Negentropy, NegentropyStorageVector} from "./negentropy.js"
|
||||||
import {unireq, RequestEvent} from "./request.js"
|
import {SingleRequest, RequestEvent} from "./request.js"
|
||||||
import {multicast, PublishEvent} from "./publish.js"
|
import {MultiPublish, PublishEvent} from "./publish.js"
|
||||||
|
|
||||||
export enum DifferenceEvent {
|
export enum DifferenceEvent {
|
||||||
Message = "difference:event:message",
|
Message = "difference:event:message",
|
||||||
@@ -204,7 +204,7 @@ export const pull = async ({context, ...options}: PullOptions) => {
|
|||||||
return Promise.all(
|
return Promise.all(
|
||||||
chunk(500, allIds).map(ids => {
|
chunk(500, allIds).map(ids => {
|
||||||
return new Promise<void>(resolve => {
|
return new Promise<void>(resolve => {
|
||||||
const req = unireq({relay, context, filter: {ids}, autoClose: true})
|
const req = new SingleRequest({relay, context, filter: {ids}, autoClose: true})
|
||||||
|
|
||||||
req.on(RequestEvent.Close, resolve)
|
req.on(RequestEvent.Close, resolve)
|
||||||
req.on(RequestEvent.Event, event => result.push(event))
|
req.on(RequestEvent.Event, event => result.push(event))
|
||||||
@@ -236,7 +236,7 @@ export const push = async ({context, events, ...options}: PushOptions) => {
|
|||||||
|
|
||||||
if (relays) {
|
if (relays) {
|
||||||
new Promise<void>(resolve => {
|
new Promise<void>(resolve => {
|
||||||
multicast({event, relays, context}).on(PublishEvent.Complete, resolve)
|
new MultiPublish({event, relays, context}).on(PublishEvent.Complete, resolve)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}),
|
}),
|
||||||
|
|||||||
+12
-18
@@ -26,9 +26,9 @@ export enum RequestEvent {
|
|||||||
Invalid = "request:event:invalid",
|
Invalid = "request:event:invalid",
|
||||||
}
|
}
|
||||||
|
|
||||||
// Unireq
|
// SingleRequest
|
||||||
|
|
||||||
export type UnireqEvents = {
|
export type SingleRequestEvents = {
|
||||||
[RequestEvent.Event]: (event: SignedEvent) => void
|
[RequestEvent.Event]: (event: SignedEvent) => void
|
||||||
[RequestEvent.Invalid]: (event: SignedEvent) => void
|
[RequestEvent.Invalid]: (event: SignedEvent) => void
|
||||||
[RequestEvent.Filtered]: (event: SignedEvent) => void
|
[RequestEvent.Filtered]: (event: SignedEvent) => void
|
||||||
@@ -38,7 +38,7 @@ export type UnireqEvents = {
|
|||||||
[RequestEvent.Eose]: () => void
|
[RequestEvent.Eose]: () => void
|
||||||
}
|
}
|
||||||
|
|
||||||
export type UnireqOptions = {
|
export type SingleRequestOptions = {
|
||||||
relay: string
|
relay: string
|
||||||
filter: Filter
|
filter: Filter
|
||||||
context?: AdapterContext
|
context?: AdapterContext
|
||||||
@@ -48,13 +48,13 @@ export type UnireqOptions = {
|
|||||||
verifyEvent?: (event: SignedEvent) => boolean
|
verifyEvent?: (event: SignedEvent) => boolean
|
||||||
}
|
}
|
||||||
|
|
||||||
export class Unireq extends (EventEmitter as new () => TypedEmitter<UnireqEvents>) {
|
export class SingleRequest extends (EventEmitter as new () => TypedEmitter<SingleRequestEvents>) {
|
||||||
_id = `REQ-${randomId().slice(0, 8)}`
|
_id = `REQ-${randomId().slice(0, 8)}`
|
||||||
_unsubscribers: Unsubscriber[] = []
|
_unsubscribers: Unsubscriber[] = []
|
||||||
_adapter: AbstractAdapter
|
_adapter: AbstractAdapter
|
||||||
_closed = false
|
_closed = false
|
||||||
|
|
||||||
constructor(readonly options: UnireqOptions) {
|
constructor(readonly options: SingleRequestOptions) {
|
||||||
super()
|
super()
|
||||||
|
|
||||||
const tracker = options.tracker || new Tracker()
|
const tracker = options.tracker || new Tracker()
|
||||||
@@ -135,9 +135,9 @@ export class Unireq extends (EventEmitter as new () => TypedEmitter<UnireqEvents
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Multireq
|
// MultiRequest
|
||||||
|
|
||||||
export type MultireqEvents = {
|
export type MultiRequestEvents = {
|
||||||
[RequestEvent.Event]: (event: SignedEvent, url: string) => void
|
[RequestEvent.Event]: (event: SignedEvent, url: string) => void
|
||||||
[RequestEvent.Invalid]: (event: SignedEvent, url: string) => void
|
[RequestEvent.Invalid]: (event: SignedEvent, url: string) => void
|
||||||
[RequestEvent.Filtered]: (event: SignedEvent, url: string) => void
|
[RequestEvent.Filtered]: (event: SignedEvent, url: string) => void
|
||||||
@@ -147,21 +147,21 @@ export type MultireqEvents = {
|
|||||||
[RequestEvent.Close]: () => void
|
[RequestEvent.Close]: () => void
|
||||||
}
|
}
|
||||||
|
|
||||||
export type MultireqOptions = Omit<UnireqOptions, "relay"> & {
|
export type MultiRequestOptions = Omit<SingleRequestOptions, "relay"> & {
|
||||||
relays: string[]
|
relays: string[]
|
||||||
}
|
}
|
||||||
|
|
||||||
export class Multireq extends (EventEmitter as new () => TypedEmitter<MultireqEvents>) {
|
export class MultiRequest extends (EventEmitter as new () => TypedEmitter<MultiRequestEvents>) {
|
||||||
_children: Unireq[] = []
|
_children: SingleRequest[] = []
|
||||||
_closed = new Set<string>()
|
_closed = new Set<string>()
|
||||||
|
|
||||||
constructor({relays, ...options}: MultireqOptions) {
|
constructor({relays, ...options}: MultiRequestOptions) {
|
||||||
super()
|
super()
|
||||||
|
|
||||||
const tracker = new Tracker()
|
const tracker = new Tracker()
|
||||||
|
|
||||||
for (const relay of relays) {
|
for (const relay of relays) {
|
||||||
const req = new Unireq({relay, tracker, ...options})
|
const req = new SingleRequest({relay, tracker, ...options})
|
||||||
|
|
||||||
req.on(RequestEvent.Event, (event: SignedEvent) => {
|
req.on(RequestEvent.Event, (event: SignedEvent) => {
|
||||||
this.emit(RequestEvent.Event, event, relay)
|
this.emit(RequestEvent.Event, event, relay)
|
||||||
@@ -205,9 +205,3 @@ export class Multireq extends (EventEmitter as new () => TypedEmitter<MultireqEv
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Convenience functions
|
|
||||||
|
|
||||||
export const unireq = (options: UnireqOptions) => new Unireq(options)
|
|
||||||
|
|
||||||
export const multireq = (options: MultireqOptions) => new Multireq(options)
|
|
||||||
|
|||||||
Reference in New Issue
Block a user