remove codec eval from `connectionManager` listener
This commit is contained in:
parent
4ff9841ead
commit
89b51c4e25
|
@ -3,7 +3,7 @@
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import { hexToBytes } from 'ethereum-cryptography/utils'
|
import { hexToBytes } from 'ethereum-cryptography/utils'
|
||||||
import { Protocols, waku_filter, waku_light_push, waku_store } from 'js-waku'
|
import { Protocols } from 'js-waku'
|
||||||
import { createLightNode } from 'js-waku/lib/create_waku'
|
import { createLightNode } from 'js-waku/lib/create_waku'
|
||||||
import { PeerDiscoveryStaticPeers } from 'js-waku/lib/peer_discovery_static_list'
|
import { PeerDiscoveryStaticPeers } from 'js-waku/lib/peer_discovery_static_list'
|
||||||
import { waitForRemotePeer } from 'js-waku/lib/wait_for_remote_peer'
|
import { waitForRemotePeer } from 'js-waku/lib/wait_for_remote_peer'
|
||||||
|
@ -21,12 +21,6 @@ import type { Storage } from './storage'
|
||||||
import type { WakuLight } from 'js-waku/lib/interfaces'
|
import type { WakuLight } from 'js-waku/lib/interfaces'
|
||||||
import type { MessageV1 as WakuMessage } from 'js-waku/lib/waku_message/version_1'
|
import type { MessageV1 as WakuMessage } from 'js-waku/lib/waku_message/version_1'
|
||||||
|
|
||||||
const PROTOCOLS = [Protocols.Store, Protocols.Filter, Protocols.LightPush]
|
|
||||||
const CODECS = [
|
|
||||||
waku_store.StoreCodec,
|
|
||||||
waku_filter.FilterCodec,
|
|
||||||
waku_light_push.LightPushCodec,
|
|
||||||
]
|
|
||||||
const THROWAWAY_ACCOUNT_STORAGE_KEY = 'throwaway_account'
|
const THROWAWAY_ACCOUNT_STORAGE_KEY = 'throwaway_account'
|
||||||
|
|
||||||
export interface ClientOptions {
|
export interface ClientOptions {
|
||||||
|
@ -70,6 +64,10 @@ class Client {
|
||||||
|
|
||||||
constructor(waku: WakuLight, options: ClientOptions) {
|
constructor(waku: WakuLight, options: ClientOptions) {
|
||||||
// Waku
|
// Waku
|
||||||
|
/**
|
||||||
|
* Waku should be connected and protocols awaited at this point, thus connected.
|
||||||
|
*/
|
||||||
|
this.connected = true
|
||||||
this.waku = waku
|
this.waku = waku
|
||||||
this.wakuMessages = new Set()
|
this.wakuMessages = new Set()
|
||||||
this.#wakuDisconnectionTimer = setInterval(async () => {
|
this.#wakuDisconnectionTimer = setInterval(async () => {
|
||||||
|
@ -86,24 +84,12 @@ class Client {
|
||||||
await Promise.allSettled(connectionsToClose)
|
await Promise.allSettled(connectionsToClose)
|
||||||
}, 10 * 1000)
|
}, 10 * 1000)
|
||||||
/**
|
/**
|
||||||
* Note: Assumes 1 remote node.
|
* Note: Assumes 1 remote node and that the diconnection does not require calling
|
||||||
|
* `waitForRemotePeer` again to ensure protocols/codecs.
|
||||||
*/
|
*/
|
||||||
this.waku.libp2p.connectionManager.addEventListener(
|
this.waku.libp2p.connectionManager.addEventListener('peer:connect', () => {
|
||||||
'peer:connect',
|
this.connected = true // reconnect
|
||||||
async event => {
|
})
|
||||||
const connection = event.detail
|
|
||||||
const protocols = await this.waku.libp2p.peerStore.protoBook.get(
|
|
||||||
connection.remotePeer
|
|
||||||
)
|
|
||||||
const isMissingProtocol = CODECS.some(
|
|
||||||
codec => !protocols.includes(codec)
|
|
||||||
)
|
|
||||||
|
|
||||||
if (!isMissingProtocol) {
|
|
||||||
this.connected = true
|
|
||||||
}
|
|
||||||
}
|
|
||||||
)
|
|
||||||
/**
|
/**
|
||||||
* >This event will **only** be triggered when the last connection is closed.
|
* >This event will **only** be triggered when the last connection is closed.
|
||||||
* @see https://github.com/libp2p/js-libp2p/blob/bad9e8c0ff58d60a78314077720c82ae331cc55b/doc/API.md?plain=1#L2100
|
* @see https://github.com/libp2p/js-libp2p/blob/bad9e8c0ff58d60a78314077720c82ae331cc55b/doc/API.md?plain=1#L2100
|
||||||
|
@ -111,7 +97,6 @@ class Client {
|
||||||
waku.libp2p.connectionManager.addEventListener('peer:disconnect', () => {
|
waku.libp2p.connectionManager.addEventListener('peer:disconnect', () => {
|
||||||
this.connected = false
|
this.connected = false
|
||||||
})
|
})
|
||||||
this.connected = true
|
|
||||||
|
|
||||||
// Storage
|
// Storage
|
||||||
this.storage = options.storage ?? new LocalStorage()
|
this.storage = options.storage ?? new LocalStorage()
|
||||||
|
@ -163,7 +148,11 @@ class Client {
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
await waku.start()
|
await waku.start()
|
||||||
await waitForRemotePeer(waku, PROTOCOLS, 10 * 1000)
|
await waitForRemotePeer(
|
||||||
|
waku,
|
||||||
|
[Protocols.Store, Protocols.Filter, Protocols.LightPush],
|
||||||
|
10 * 1000
|
||||||
|
)
|
||||||
|
|
||||||
// Client
|
// Client
|
||||||
client = new Client(waku, options)
|
client = new Client(waku, options)
|
||||||
|
|
Loading…
Reference in New Issue