Add check for deleted events in request
This commit is contained in:
@@ -1,5 +1,5 @@
|
|||||||
import {EventEmitter} from "events"
|
import {EventEmitter} from "events"
|
||||||
import {on, call, randomId, yieldThread, pushToMapKey, batcher} from "@welshman/lib"
|
import {on, always, call, randomId, yieldThread, pushToMapKey, batcher} from "@welshman/lib"
|
||||||
import {
|
import {
|
||||||
Filter,
|
Filter,
|
||||||
unionFilters,
|
unionFilters,
|
||||||
@@ -21,6 +21,7 @@ export enum RequestEvent {
|
|||||||
Eose = "request:event:eose",
|
Eose = "request:event:eose",
|
||||||
Event = "request:event:event",
|
Event = "request:event:event",
|
||||||
Filtered = "request:event:filtered",
|
Filtered = "request:event:filtered",
|
||||||
|
Deleted = "request:event:deleted",
|
||||||
Invalid = "request:event:invalid",
|
Invalid = "request:event:invalid",
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -28,6 +29,7 @@ export enum RequestEvent {
|
|||||||
|
|
||||||
export type SingleRequestEvents = {
|
export type SingleRequestEvents = {
|
||||||
[RequestEvent.Event]: (event: TrustedEvent) => void
|
[RequestEvent.Event]: (event: TrustedEvent) => void
|
||||||
|
[RequestEvent.Deleted]: (event: any) => void
|
||||||
[RequestEvent.Invalid]: (event: any) => void
|
[RequestEvent.Invalid]: (event: any) => void
|
||||||
[RequestEvent.Filtered]: (event: TrustedEvent) => void
|
[RequestEvent.Filtered]: (event: TrustedEvent) => void
|
||||||
[RequestEvent.Duplicate]: (event: TrustedEvent) => void
|
[RequestEvent.Duplicate]: (event: TrustedEvent) => void
|
||||||
@@ -43,7 +45,8 @@ export type SingleRequestOptions = {
|
|||||||
timeout?: number
|
timeout?: number
|
||||||
tracker?: Tracker
|
tracker?: Tracker
|
||||||
autoClose?: boolean
|
autoClose?: boolean
|
||||||
verifyEvent?: (event: any) => boolean
|
isEventValid?: (event: any, url: string) => boolean
|
||||||
|
isEventDeleted?: (event: any, url: string) => boolean
|
||||||
}
|
}
|
||||||
|
|
||||||
export class SingleRequest extends (EventEmitter as new () => TypedEmitter<SingleRequestEvents>) {
|
export class SingleRequest extends (EventEmitter as new () => TypedEmitter<SingleRequestEvents>) {
|
||||||
@@ -56,8 +59,8 @@ export class SingleRequest extends (EventEmitter as new () => TypedEmitter<Singl
|
|||||||
super()
|
super()
|
||||||
|
|
||||||
const tracker = options.tracker || new Tracker()
|
const tracker = options.tracker || new Tracker()
|
||||||
|
const isEventValid = options.isEventValid || defaultVerifyEvent
|
||||||
const verifyEvent = options.verifyEvent || defaultVerifyEvent
|
const isEventDeleted = options.isEventDeleted || always(false)
|
||||||
|
|
||||||
// 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)
|
||||||
@@ -72,7 +75,9 @@ export class SingleRequest extends (EventEmitter as new () => TypedEmitter<Singl
|
|||||||
|
|
||||||
if (tracker.track(event.id, url)) {
|
if (tracker.track(event.id, url)) {
|
||||||
this.emit(RequestEvent.Duplicate, event)
|
this.emit(RequestEvent.Duplicate, event)
|
||||||
} else if (verifyEvent?.(event) === false) {
|
} else if (isEventDeleted(event, url)) {
|
||||||
|
this.emit(RequestEvent.Deleted, event)
|
||||||
|
} else if (!isEventValid(event, url)) {
|
||||||
this.emit(RequestEvent.Invalid, event)
|
this.emit(RequestEvent.Invalid, event)
|
||||||
} else if (!matchFilter(this.options.filter, event)) {
|
} else if (!matchFilter(this.options.filter, event)) {
|
||||||
this.emit(RequestEvent.Filtered, event)
|
this.emit(RequestEvent.Filtered, event)
|
||||||
@@ -137,6 +142,7 @@ export class SingleRequest extends (EventEmitter as new () => TypedEmitter<Singl
|
|||||||
|
|
||||||
export type MultiRequestEvents = {
|
export type MultiRequestEvents = {
|
||||||
[RequestEvent.Event]: (event: TrustedEvent, url: string) => void
|
[RequestEvent.Event]: (event: TrustedEvent, url: string) => void
|
||||||
|
[RequestEvent.Deleted]: (event: TrustedEvent, url: string) => void
|
||||||
[RequestEvent.Invalid]: (event: TrustedEvent, url: string) => void
|
[RequestEvent.Invalid]: (event: TrustedEvent, url: string) => void
|
||||||
[RequestEvent.Filtered]: (event: TrustedEvent, url: string) => void
|
[RequestEvent.Filtered]: (event: TrustedEvent, url: string) => void
|
||||||
[RequestEvent.Duplicate]: (event: TrustedEvent, url: string) => void
|
[RequestEvent.Duplicate]: (event: TrustedEvent, url: string) => void
|
||||||
@@ -165,6 +171,10 @@ export class MultiRequest extends (EventEmitter as new () => TypedEmitter<MultiR
|
|||||||
this.emit(RequestEvent.Event, event, relay)
|
this.emit(RequestEvent.Event, event, relay)
|
||||||
})
|
})
|
||||||
|
|
||||||
|
req.on(RequestEvent.Deleted, (event: TrustedEvent) => {
|
||||||
|
this.emit(RequestEvent.Deleted, event, relay)
|
||||||
|
})
|
||||||
|
|
||||||
req.on(RequestEvent.Invalid, (event: TrustedEvent) => {
|
req.on(RequestEvent.Invalid, (event: TrustedEvent) => {
|
||||||
this.emit(RequestEvent.Invalid, event, relay)
|
this.emit(RequestEvent.Invalid, event, relay)
|
||||||
})
|
})
|
||||||
|
|||||||
Reference in New Issue
Block a user