Skip to content

Commit

Permalink
Code consolidation
Browse files Browse the repository at this point in the history
  • Loading branch information
islathehut committed May 17, 2024
1 parent 7f1bce6 commit 75818c8
Showing 1 changed file with 26 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -267,19 +267,11 @@ export class ConnectionsManagerService extends EventEmitter implements OnModuleI
this.logger('Resuming!')
await this.openSocket()
const peersToDial = await this.getPeersOnResume()
if (!this.tor.isTorServiceUsed) {
this.logger(`We aren't using the tor service in this client, checking bootstrap status in connection manager`)
this.initializationInterval = setInterval(async () => {
console.log('Checking bootstrap interval')
const bootstrapDone = await this.tor.isBootstrappingFinished()
if (bootstrapDone) {
clearInterval(this.initializationInterval)
this.logger('Bootstrapping is finished')
this.libp2pService?.resume(peersToDial)
}
}, 2500)
return
const callback = async () => {
this.logger('Bootstrapping is finished')
this.libp2pService?.resume(peersToDial)
}
if (await this.runOnTorBootstrap(callback)) return
this.libp2pService?.resume(peersToDial)
}

Expand Down Expand Up @@ -652,20 +644,13 @@ export class ConnectionsManagerService extends EventEmitter implements OnModuleI
ConnectionProcessInfo.CONNECTING_TO_COMMUNITY
)

if (!this.tor.isTorServiceUsed) {
this.logger(`We aren't using the tor service in this client, checking bootstrap status in connection manager`)
this.initializationInterval = setInterval(async () => {
console.log('Checking bootstrap interval')
const bootstrapDone = await this.tor.isBootstrappingFinished()
if (bootstrapDone) {
console.log(`Sending ${SocketActionTypes.TOR_INITIALIZED}`)
this.serverIoProvider.io.emit(SocketActionTypes.TOR_INITIALIZED)
console.log(`Sending ${SocketActionTypes.INITIAL_DIAL}`)
this.libp2pService?.emit(Libp2pEvents.INITIAL_DIAL)
clearInterval(this.initializationInterval)
}
}, 2500)
const callback = async () => {
console.log(`Sending ${SocketActionTypes.TOR_INITIALIZED}`)
this.serverIoProvider.io.emit(SocketActionTypes.TOR_INITIALIZED)
console.log(`Sending ${SocketActionTypes.INITIAL_DIAL}`)
this.libp2pService?.emit(Libp2pEvents.INITIAL_DIAL)
}
await this.runOnTorBootstrap(callback)
}

private attachTorEventsListeners() {
Expand Down Expand Up @@ -871,4 +856,20 @@ export class ConnectionsManagerService extends EventEmitter implements OnModuleI
this.serverIoProvider.io.emit(SocketActionTypes.USER_PROFILES_STORED, payload)
})
}

private async runOnTorBootstrap(callback: () => Promise<any>, intervalTimerMs: number = 2500): Promise<boolean> {
if (!this.tor.isTorServiceUsed) {
this.logger(`We aren't using the tor service in this client, checking bootstrap status in connection manager`)
this.initializationInterval = setInterval(async () => {
console.log('Checking bootstrap interval')
const bootstrapDone = await this.tor.isBootstrappingFinished()
if (bootstrapDone) {
clearInterval(this.initializationInterval)
await callback()
}
}, intervalTimerMs)
return true
}
return false
}
}

0 comments on commit 75818c8

Please sign in to comment.