Skip to content
This repository has been archived by the owner on Jun 26, 2023. It is now read-only.

feat: safe dispatch event #319

Merged
merged 2 commits into from
Jan 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions packages/interface-mocks/src/connection-manager.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { CustomEvent, EventEmitter } from '@libp2p/interfaces/events'
import { EventEmitter } from '@libp2p/interfaces/events'
import type { Startable } from '@libp2p/interfaces/startable'
import type { Connection } from '@libp2p/interface-connection'
import type { PeerId } from '@libp2p/interface-peer-id'
Expand Down Expand Up @@ -94,19 +94,19 @@ class MockConnectionManager extends EventEmitter<ConnectionManagerEvents> implem
this.connections.push(aToB)
;(componentsB.connectionManager as MockConnectionManager).connections.push(bToA)

this.components.connectionManager?.dispatchEvent(new CustomEvent<Connection>('peer:connect', {
this.components.connectionManager?.safeDispatchEvent<Connection>('peer:connect', {
detail: aToB
}))
})

for (const protocol of this.components.registrar.getProtocols()) {
for (const topology of this.components.registrar.getTopologies(protocol)) {
topology.onConnect(componentsB.peerId, aToB)
}
}

componentsB.connectionManager?.dispatchEvent(new CustomEvent<Connection>('peer:connect', {
componentsB.connectionManager?.safeDispatchEvent<Connection>('peer:connect', {
detail: bToA
}))
})

for (const protocol of componentsB.registrar.getProtocols()) {
for (const topology of componentsB.registrar.getTopologies(protocol)) {
Expand Down
6 changes: 3 additions & 3 deletions packages/interface-mocks/src/peer-discovery.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { multiaddr } from '@multiformats/multiaddr'
import * as PeerIdFactory from '@libp2p/peer-id-factory'
import { EventEmitter, CustomEvent } from '@libp2p/interfaces/events'
import { EventEmitter } from '@libp2p/interfaces/events'
import type { PeerDiscovery, PeerDiscoveryEvents } from '@libp2p/interface-peer-discovery'
import type { PeerInfo } from '@libp2p/interface-peer-info'
import { symbol } from '@libp2p/interface-peer-discovery'
Expand Down Expand Up @@ -52,13 +52,13 @@ export class MockDiscovery extends EventEmitter<PeerDiscoveryEvents> implements
PeerIdFactory.createEd25519PeerId()
.then(peerId => {
this._timer = setTimeout(() => {
this.dispatchEvent(new CustomEvent<PeerInfo>('peer', {
this.safeDispatchEvent<PeerInfo>('peer', {
detail: {
id: peerId,
multiaddrs: [multiaddr('/ip4/127.0.0.1/tcp/8000')],
protocols: []
}
}))
})
}, this.options.discoveryDelay ?? 1000)
})
.catch(() => {})
Expand Down
4 changes: 4 additions & 0 deletions packages/interfaces/src/events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,10 @@ export class EventEmitter<EventMap extends { [s: string]: any }> extends EventTa

return result
}

safeDispatchEvent<Detail>(type: keyof EventMap, detail: CustomEventInit<Detail>): boolean {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is detail the best name for this param?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No idea, its already ref like that in multiple places

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

detail is used in other places as it's the option key from the CustomEvent constructor

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

right it seems confusing since this is actually the CustomEvent options object which contains detail

safeDispatchEvent<Detail>(type: keyof EventMap, detail: CustomEventInit<Detail>): boolean {
return this.dispatchEvent(new CustomEvent<Detail>(type as string, detail))

this.components.connectionManager?.safeDispatchEvent<Connection>('peer:connect', {
detail: aToB
})

return this.dispatchEvent(new CustomEvent<Detail>(type as string, detail))
}
}

/**
Expand Down