Remove shortcut handlers
This commit is contained in:
@@ -12,7 +12,7 @@ import {
|
|||||||
import {getAdapter, AdapterContext, AbstractAdapter, AdapterEventType} from "./adapter.js"
|
import {getAdapter, AdapterContext, AbstractAdapter, AdapterEventType} from "./adapter.js"
|
||||||
import {Negentropy, NegentropyStorageVector} from "./negentropy.js"
|
import {Negentropy, NegentropyStorageVector} from "./negentropy.js"
|
||||||
import {subscribe, SubscriptionEventType} from "./subscribe.js"
|
import {subscribe, SubscriptionEventType} from "./subscribe.js"
|
||||||
import {publish, PublishEventType} from "./publish.js"
|
import {publish, PublicationEventType} from "./publish.js"
|
||||||
|
|
||||||
export enum DifferenceEventType {
|
export enum DifferenceEventType {
|
||||||
Message = "difference:event:message",
|
Message = "difference:event:message",
|
||||||
@@ -31,7 +31,6 @@ export type DifferenceOptions = {
|
|||||||
filter: Filter
|
filter: Filter
|
||||||
events: SignedEvent[]
|
events: SignedEvent[]
|
||||||
context: AdapterContext
|
context: AdapterContext
|
||||||
on?: Partial<DifferenceEvents>
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export class Difference extends (EventEmitter as new () => TypedEmitter<DifferenceEvents>) {
|
export class Difference extends (EventEmitter as new () => TypedEmitter<DifferenceEvents>) {
|
||||||
@@ -96,13 +95,6 @@ export class Difference extends (EventEmitter as new () => TypedEmitter<Differen
|
|||||||
},
|
},
|
||||||
)
|
)
|
||||||
|
|
||||||
// Register listeners
|
|
||||||
if (this.options.on) {
|
|
||||||
for (const [k, listener] of Object.entries(this.options.on)) {
|
|
||||||
this.on(k as keyof DifferenceEvents, listener)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
neg.initiate().then((msg: string) => {
|
neg.initiate().then((msg: string) => {
|
||||||
this._adapter.send([ClientMessageType.NegOpen, this._id, this.options.filter, msg])
|
this._adapter.send([ClientMessageType.NegOpen, this._id, this.options.filter, msg])
|
||||||
})
|
})
|
||||||
@@ -212,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 sub = subscribe({relay, filter: {ids}, context, autoClose: true})
|
const sub = subscribe({relay, context, filter: {ids}, autoClose: true})
|
||||||
|
|
||||||
sub.on(SubscriptionEventType.Close, resolve)
|
sub.on(SubscriptionEventType.Close, resolve)
|
||||||
sub.on(SubscriptionEventType.Event, event => result.push(event))
|
sub.on(SubscriptionEventType.Event, event => result.push(event))
|
||||||
@@ -247,7 +239,9 @@ export const push = async ({context, events, ...options}: PushOptions) => {
|
|||||||
relays.map(
|
relays.map(
|
||||||
relay =>
|
relay =>
|
||||||
new Promise<void>(resolve => {
|
new Promise<void>(resolve => {
|
||||||
publish({event, relay, context}).on(PublishEventType.Complete, resolve)
|
const pub = publish({event, relay, context})
|
||||||
|
|
||||||
|
pub.on(PublicationEventType.Complete, resolve)
|
||||||
}),
|
}),
|
||||||
),
|
),
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -5,48 +5,51 @@ import {RelayMessage, ClientMessageType, isRelayOk} from "./message.js"
|
|||||||
import {AbstractAdapter, AdapterEventType, AdapterContext, getAdapter} from "./adapter.js"
|
import {AbstractAdapter, AdapterEventType, AdapterContext, getAdapter} from "./adapter.js"
|
||||||
import {TypedEmitter} from "./util.js"
|
import {TypedEmitter} from "./util.js"
|
||||||
|
|
||||||
export enum PublishStatus {
|
export enum PublicationStatus {
|
||||||
Pending = "publish:status:pending",
|
Pending = "publication:status:pending",
|
||||||
Success = "publish:status:success",
|
Success = "publication:status:success",
|
||||||
Failure = "publish:status:failure",
|
Failure = "publication:status:failure",
|
||||||
Timeout = "publish:status:timeout",
|
Timeout = "publication:status:timeout",
|
||||||
Aborted = "publish:status:aborted",
|
Aborted = "publication:status:aborted",
|
||||||
}
|
}
|
||||||
|
|
||||||
export enum PublishEventType {
|
export enum PublicationEventType {
|
||||||
Complete = "publish:status:complete",
|
Success = "publication:event:success",
|
||||||
|
Failure = "publication:event:failure",
|
||||||
|
Timeout = "publication:event:timeout",
|
||||||
|
Aborted = "publication:event:aborted",
|
||||||
|
Complete = "publication:event:complete",
|
||||||
}
|
}
|
||||||
|
|
||||||
export type PublishEvents = {
|
export type PublicationEvents = {
|
||||||
[PublishStatus.Success]: (id: string, detail: string, url: string) => void
|
[PublicationEventType.Success]: (id: string, detail: string, url: string) => void
|
||||||
[PublishStatus.Failure]: (id: string, detail: string, url: string) => void
|
[PublicationEventType.Failure]: (id: string, detail: string, url: string) => void
|
||||||
[PublishStatus.Timeout]: () => void
|
[PublicationEventType.Timeout]: () => void
|
||||||
[PublishStatus.Aborted]: () => void
|
[PublicationEventType.Aborted]: () => void
|
||||||
[PublishEventType.Complete]: () => void
|
[PublicationEventType.Complete]: () => void
|
||||||
}
|
}
|
||||||
|
|
||||||
export type PublishOptions = {
|
export type PublicationOptions = {
|
||||||
relay: string
|
relay: string
|
||||||
event: SignedEvent
|
event: SignedEvent
|
||||||
context: AdapterContext
|
context: AdapterContext
|
||||||
timeout?: number
|
timeout?: number
|
||||||
on?: Partial<PublishEvents>
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export class Publish extends (EventEmitter as new () => TypedEmitter<PublishEvents>) {
|
export class Publication extends (EventEmitter as new () => TypedEmitter<PublicationEvents>) {
|
||||||
status = PublishStatus.Pending
|
status = PublicationStatus.Pending
|
||||||
|
|
||||||
_done = new Set<string>()
|
_done = new Set<string>()
|
||||||
_unsubscriber: () => void
|
_unsubscriber: () => void
|
||||||
_adapter: AbstractAdapter
|
_adapter: AbstractAdapter
|
||||||
|
|
||||||
constructor(readonly options: PublishOptions) {
|
constructor(readonly options: PublicationOptions) {
|
||||||
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 publish result
|
// Listen for Publication result
|
||||||
this._unsubscriber = on(
|
this._unsubscriber = on(
|
||||||
this._adapter,
|
this._adapter,
|
||||||
AdapterEventType.Receive,
|
AdapterEventType.Receive,
|
||||||
@@ -57,11 +60,11 @@ export class Publish extends (EventEmitter as new () => TypedEmitter<PublishEven
|
|||||||
if (id !== this.options.event.id) return
|
if (id !== this.options.event.id) return
|
||||||
|
|
||||||
if (ok) {
|
if (ok) {
|
||||||
this.status = PublishStatus.Success
|
this.status = PublicationStatus.Success
|
||||||
this.emit(PublishStatus.Success, id, detail, url)
|
this.emit(PublicationEventType.Success, id, detail, url)
|
||||||
} else {
|
} else {
|
||||||
this.status = PublishStatus.Failure
|
this.status = PublicationStatus.Failure
|
||||||
this.emit(PublishStatus.Failure, id, detail, url)
|
this.emit(PublicationEventType.Failure, id, detail, url)
|
||||||
}
|
}
|
||||||
|
|
||||||
this.cleanup()
|
this.cleanup()
|
||||||
@@ -69,13 +72,6 @@ export class Publish extends (EventEmitter as new () => TypedEmitter<PublishEven
|
|||||||
},
|
},
|
||||||
)
|
)
|
||||||
|
|
||||||
// Register handlers
|
|
||||||
if (this.options.on) {
|
|
||||||
for (const [k, listener] of Object.entries(this.options.on)) {
|
|
||||||
this.on(k as keyof PublishEvents, listener)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Autostart asynchronously so the caller can set up listeners
|
// Autostart asynchronously so the caller can set up listeners
|
||||||
yieldThread().then(this.start)
|
yieldThread().then(this.start)
|
||||||
}
|
}
|
||||||
@@ -83,32 +79,32 @@ export class Publish extends (EventEmitter as new () => TypedEmitter<PublishEven
|
|||||||
start = () => {
|
start = () => {
|
||||||
// Set timeout
|
// Set timeout
|
||||||
sleep(this.options.timeout || 10_000).then(() => {
|
sleep(this.options.timeout || 10_000).then(() => {
|
||||||
if (this.status === PublishStatus.Pending) {
|
if (this.status === PublicationStatus.Pending) {
|
||||||
this.status = PublishStatus.Timeout
|
this.status = PublicationStatus.Timeout
|
||||||
this.emit(PublishStatus.Timeout)
|
this.emit(PublicationEventType.Timeout)
|
||||||
}
|
}
|
||||||
|
|
||||||
this.cleanup()
|
this.cleanup()
|
||||||
})
|
})
|
||||||
|
|
||||||
// Send the publish message
|
// Send the Publication message
|
||||||
this._adapter.send([ClientMessageType.Event, event])
|
this._adapter.send([ClientMessageType.Event, event])
|
||||||
}
|
}
|
||||||
|
|
||||||
abort = () => {
|
abort = () => {
|
||||||
if (this.status === PublishStatus.Pending) {
|
if (this.status === PublicationStatus.Pending) {
|
||||||
this.status = PublishStatus.Aborted
|
this.status = PublicationStatus.Aborted
|
||||||
this.emit(PublishStatus.Aborted)
|
this.emit(PublicationEventType.Aborted)
|
||||||
this.cleanup()
|
this.cleanup()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
cleanup = () => {
|
cleanup = () => {
|
||||||
this.emit(PublishEventType.Complete)
|
this.emit(PublicationEventType.Complete)
|
||||||
this.removeAllListeners()
|
this.removeAllListeners()
|
||||||
this._adapter.cleanup()
|
this._adapter.cleanup()
|
||||||
this._unsubscriber()
|
this._unsubscriber()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export const publish = (options: PublishOptions) => new Publish(options)
|
export const publish = (options: PublicationOptions) => new Publication(options)
|
||||||
|
|||||||
@@ -35,7 +35,6 @@ export type SubscriptionOptions = {
|
|||||||
tracker?: Tracker
|
tracker?: Tracker
|
||||||
autoClose?: boolean
|
autoClose?: boolean
|
||||||
verifyEvent?: (event: SignedEvent) => boolean
|
verifyEvent?: (event: SignedEvent) => boolean
|
||||||
on?: Partial<SubscriptionEvents>
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export class Subscription extends (EventEmitter as new () => TypedEmitter<SubscriptionEvents>) {
|
export class Subscription extends (EventEmitter as new () => TypedEmitter<SubscriptionEvents>) {
|
||||||
@@ -98,13 +97,6 @@ export class Subscription extends (EventEmitter as new () => TypedEmitter<Subscr
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Register listeners
|
|
||||||
if (this.options.on) {
|
|
||||||
for (const [k, listener] of Object.entries(this.options.on)) {
|
|
||||||
this.on(k as keyof SubscriptionEvents, listener)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Autostart asynchronously so the caller can set up listeners
|
// Autostart asynchronously so the caller can set up listeners
|
||||||
yieldThread().then(this.open)
|
yieldThread().then(this.open)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user