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

Commit

Permalink
feat: safe dispatch event (#319)
Browse files Browse the repository at this point in the history
Adds a type-safe `safeDispatchEvent` method to the `EventEmitter` interface that allows type checking of dispatched events.

resolves #317 

Co-authored-by: Alex Potsides <alex@achingbrain.net>
  • Loading branch information
mpetrunic and achingbrain authored Jan 17, 2023
1 parent 55cddb7 commit 8caeee8
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 8 deletions.
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 @@ -99,19 +99,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 {
return this.dispatchEvent(new CustomEvent<Detail>(type as string, detail))
}
}

/**
Expand Down

0 comments on commit 8caeee8

Please sign in to comment.