dispatch `CONNECT`
This commit is contained in:
parent
482874a462
commit
5b48b614e8
|
@ -53,6 +53,7 @@ class Client {
|
||||||
*/
|
*/
|
||||||
#wakuDisconnectionTimer: ReturnType<typeof setInterval>
|
#wakuDisconnectionTimer: ReturnType<typeof setInterval>
|
||||||
connected: boolean
|
connected: boolean
|
||||||
|
#connectionCallbacks: Set<(connected: boolean) => void>
|
||||||
|
|
||||||
public activityCenter: ActivityCenter
|
public activityCenter: ActivityCenter
|
||||||
public community: Community
|
public community: Community
|
||||||
|
@ -68,6 +69,7 @@ class Client {
|
||||||
* Waku should be connected and protocols awaited at this point, thus connected.
|
* Waku should be connected and protocols awaited at this point, thus connected.
|
||||||
*/
|
*/
|
||||||
this.connected = true
|
this.connected = true
|
||||||
|
this.#connectionCallbacks = new Set()
|
||||||
this.waku = waku
|
this.waku = waku
|
||||||
this.wakuMessages = new Set()
|
this.wakuMessages = new Set()
|
||||||
this.#wakuDisconnectionTimer = setInterval(async () => {
|
this.#wakuDisconnectionTimer = setInterval(async () => {
|
||||||
|
@ -76,6 +78,12 @@ class Client {
|
||||||
for (const connection of this.waku.libp2p.connectionManager.getConnections()) {
|
for (const connection of this.waku.libp2p.connectionManager.getConnections()) {
|
||||||
try {
|
try {
|
||||||
await this.waku.libp2p.ping(connection.remoteAddr)
|
await this.waku.libp2p.ping(connection.remoteAddr)
|
||||||
|
|
||||||
|
if (!this.connected) {
|
||||||
|
this.connected = true
|
||||||
|
|
||||||
|
this.emitConnection(this.connected)
|
||||||
|
}
|
||||||
} catch {
|
} catch {
|
||||||
connectionsToClose.push(connection.close())
|
connectionsToClose.push(connection.close())
|
||||||
}
|
}
|
||||||
|
@ -89,6 +97,8 @@ class Client {
|
||||||
*/
|
*/
|
||||||
this.waku.libp2p.connectionManager.addEventListener('peer:connect', () => {
|
this.waku.libp2p.connectionManager.addEventListener('peer:connect', () => {
|
||||||
this.connected = true // reconnect
|
this.connected = true // reconnect
|
||||||
|
|
||||||
|
this.emitConnection(this.connected)
|
||||||
})
|
})
|
||||||
/**
|
/**
|
||||||
* >This event will **only** be triggered when the last connection is closed.
|
* >This event will **only** be triggered when the last connection is closed.
|
||||||
|
@ -96,6 +106,13 @@ class Client {
|
||||||
*/
|
*/
|
||||||
waku.libp2p.connectionManager.addEventListener('peer:disconnect', () => {
|
waku.libp2p.connectionManager.addEventListener('peer:disconnect', () => {
|
||||||
this.connected = false
|
this.connected = false
|
||||||
|
|
||||||
|
this.emitConnection(this.connected)
|
||||||
|
})
|
||||||
|
window.addEventListener('offline', () => {
|
||||||
|
this.connected = false
|
||||||
|
|
||||||
|
this.emitConnection(this.connected)
|
||||||
})
|
})
|
||||||
|
|
||||||
// Storage
|
// Storage
|
||||||
|
@ -177,6 +194,18 @@ class Client {
|
||||||
await this.waku.stop()
|
await this.waku.stop()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public onConnection = (callback: (connected: boolean) => void) => {
|
||||||
|
this.#connectionCallbacks.add(callback)
|
||||||
|
|
||||||
|
return () => {
|
||||||
|
this.#connectionCallbacks.delete(callback)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private emitConnection = (connected: boolean) => {
|
||||||
|
this.#connectionCallbacks.forEach(callback => callback(connected))
|
||||||
|
}
|
||||||
|
|
||||||
get account() {
|
get account() {
|
||||||
return this.#account
|
return this.#account
|
||||||
}
|
}
|
||||||
|
@ -201,10 +230,10 @@ class Client {
|
||||||
this.account = undefined
|
this.account = undefined
|
||||||
}
|
}
|
||||||
|
|
||||||
public onAccountChange(listener: (account?: Account) => void) {
|
public onAccountChange(callback: (account?: Account) => void) {
|
||||||
this.#accountCallbacks.add(listener)
|
this.#accountCallbacks.add(callback)
|
||||||
return () => {
|
return () => {
|
||||||
this.#accountCallbacks.delete(listener)
|
this.#accountCallbacks.delete(callback)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -23,6 +23,7 @@ export type Action =
|
||||||
| { type: 'UPDATE_COMMUNITY'; community: Community['description'] }
|
| { type: 'UPDATE_COMMUNITY'; community: Community['description'] }
|
||||||
| { type: 'SET_ACCOUNT'; account: Account | undefined }
|
| { type: 'SET_ACCOUNT'; account: Account | undefined }
|
||||||
| { type: 'FAIL' }
|
| { type: 'FAIL' }
|
||||||
|
| { type: 'CONNECT'; connected: boolean }
|
||||||
|
|
||||||
interface Props {
|
interface Props {
|
||||||
options: ClientOptions
|
options: ClientOptions
|
||||||
|
@ -50,6 +51,9 @@ const reducer = (state: State, action: Action): State => {
|
||||||
case 'FAIL': {
|
case 'FAIL': {
|
||||||
return { ...state, failed: true, loading: false }
|
return { ...state, failed: true, loading: false }
|
||||||
}
|
}
|
||||||
|
case 'CONNECT': {
|
||||||
|
return { ...state, loading: !action.connected }
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -88,6 +92,9 @@ export const ProtocolProvider = (props: Props) => {
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (client) {
|
if (client) {
|
||||||
const unsubscribe = [
|
const unsubscribe = [
|
||||||
|
client.onConnection(connected => {
|
||||||
|
dispatch({ type: 'CONNECT', connected })
|
||||||
|
}),
|
||||||
client.onAccountChange(account => {
|
client.onAccountChange(account => {
|
||||||
dispatch({ type: 'SET_ACCOUNT', account })
|
dispatch({ type: 'SET_ACCOUNT', account })
|
||||||
}),
|
}),
|
||||||
|
|
Loading…
Reference in New Issue