@@ -19,7 +19,7 @@ import type { EventEmitter } from '@libp2p/interfaces/events'
19
19
import type { Startable } from '@libp2p/interfaces/startable'
20
20
import type { Multiaddr } from '@multiformats/multiaddr'
21
21
import type { DualDHT } from '@libp2p/interface-dht'
22
- import type { PeerStore } from '@libp2p/interface-peer-store'
22
+ import type { Address , Peer , PeerStore } from '@libp2p/interface-peer-store'
23
23
import type { PeerId } from '@libp2p/interface-peer-id'
24
24
import type { Connection , Stream } from '@libp2p/interface-connection'
25
25
import type { PeerRouting } from '@libp2p/interface-peer-routing'
@@ -29,12 +29,34 @@ import type { StreamHandler, StreamHandlerOptions, Topology } from '@libp2p/inte
29
29
import type { Metrics } from '@libp2p/interface-metrics'
30
30
import type { PeerInfo } from '@libp2p/interface-peer-info'
31
31
import type { KeyChain } from '@libp2p/interface-keychain'
32
+ import type { Listener } from '@libp2p/interface-transport'
32
33
33
34
/**
34
- * Once you have a libp2p instance, you can listen to several events it emits, so that you can be notified of relevant network events.
35
+ * Used by the connection manager to sort addresses into order before dialling
36
+ */
37
+ export interface AddressSorter {
38
+ ( a : Address , b : Address ) : - 1 | 0 | 1
39
+ }
40
+
41
+ /**
42
+ * Event detail emitted when peer data changes
43
+ */
44
+ export interface PeerUpdate {
45
+ peer : Peer
46
+ previous ?: Peer
47
+ }
48
+
49
+ /**
50
+ * Once you have a libp2p instance, you can listen to several events it emits,
51
+ * so that you can be notified of relevant network events.
52
+ *
53
+ * Event names are `noun:adjective` so the first part is the name of the object
54
+ * being acted on and the second is the action.
35
55
*/
36
56
export interface Libp2pEvents {
37
57
/**
58
+ * This event is dispatched when a new network peer is discovered.
59
+ *
38
60
* @example
39
61
*
40
62
* ```js
@@ -47,18 +69,18 @@ export interface Libp2pEvents {
47
69
'peer:discovery' : CustomEvent < PeerInfo >
48
70
49
71
/**
50
- * This event will be triggered anytime a new Connection is established to another peer.
72
+ * This event will be triggered anytime a new peer connects .
51
73
*
52
74
* @example
53
75
*
54
76
* ```js
55
77
* libp2p.connectionManager.addEventListener('peer:connect', (event) => {
56
- * const connection = event.detail
78
+ * const peerId = event.detail
57
79
* // ...
58
80
* })
59
81
* ```
60
82
*/
61
- 'peer:connect' : CustomEvent < Connection >
83
+ 'peer:connect' : CustomEvent < PeerId >
62
84
63
85
/**
64
86
* This event will be triggered anytime we are disconnected from another peer, regardless of
@@ -74,7 +96,64 @@ export interface Libp2pEvents {
74
96
* })
75
97
* ```
76
98
*/
77
- 'peer:disconnect' : CustomEvent < Connection >
99
+ 'peer:disconnect' : CustomEvent < PeerId >
100
+
101
+ /**
102
+ * This event is dispatched when the peer store data for a peer has been
103
+ * updated - e.g. their multiaddrs, protocols etc have changed.
104
+ *
105
+ * If they were previously known to this node, the old peer data will be
106
+ * set in the `previous` field.
107
+ *
108
+ * This may be in response to the identify protocol running, a manual
109
+ * update or some other event.
110
+ */
111
+ 'peer:update' : CustomEvent < PeerUpdate >
112
+
113
+ /**
114
+ * This event is dispatched when the current node's peer record changes -
115
+ * for example a transport started listening on a new address or a new
116
+ * protocol handler was registered.
117
+ *
118
+ * @example
119
+ *
120
+ * ```js
121
+ * libp2p.addEventListener('self:peer:update', (event) => {
122
+ * const { peer } = event.detail
123
+ * // ...
124
+ * })
125
+ * ```
126
+ */
127
+ 'self:peer:update' : CustomEvent < PeerUpdate >
128
+
129
+ /**
130
+ * This event is dispatched when a transport begins listening on a new address
131
+ */
132
+ 'transport:listening' : CustomEvent < Listener >
133
+
134
+ /**
135
+ * This event is dispatched when a transport stops listening on an address
136
+ */
137
+ 'transport:close' : CustomEvent < Listener >
138
+
139
+ /**
140
+ * This event is dispatched when the connection manager has more than the
141
+ * configured allowable max connections and has closed some connections to
142
+ * bring the node back under the limit.
143
+ */
144
+ 'connection:prune' : CustomEvent < Connection [ ] >
145
+
146
+ /**
147
+ * This event notifies listeners when new incoming or outgoing connections
148
+ * are opened.
149
+ */
150
+ 'connection:open' : CustomEvent < Connection >
151
+
152
+ /**
153
+ * This event notifies listeners when incoming or outgoing connections are
154
+ * closed.
155
+ */
156
+ 'connection:close' : CustomEvent < Connection >
78
157
}
79
158
80
159
/**
0 commit comments