Skip to content

Commit dbc2653

Browse files
authored
chore: use set-delayed-interval module on circuit (#809)
1 parent 7c3f7e0 commit dbc2653

File tree

1 file changed

+11
-11
lines changed

1 file changed

+11
-11
lines changed

src/circuit/index.js

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@ const debug = require('debug')
44
const log = debug('libp2p:relay')
55
log.error = debug('libp2p:relay:error')
66

7+
const {
8+
setDelayedInterval,
9+
clearDelayedInterval
10+
} = require('set-delayed-interval')
11+
712
const AutoRelay = require('./auto-relay')
813
const { namespaceToCid } = require('./utils')
914
const {
@@ -33,6 +38,8 @@ class Relay {
3338

3439
// Create autoRelay if enabled
3540
this._autoRelay = this._options.autoRelay.enabled && new AutoRelay({ libp2p, ...this._options.autoRelay })
41+
42+
this._advertiseService = this._advertiseService.bind(this)
3643
}
3744

3845
/**
@@ -45,9 +52,9 @@ class Relay {
4552
const canHop = this._options.hop.enabled
4653

4754
if (canHop && this._options.advertise.enabled) {
48-
this._timeout = setTimeout(() => {
49-
this._advertiseService()
50-
}, this._options.advertise.bootDelay)
55+
this._timeout = setDelayedInterval(
56+
this._advertiseService, this._options.advertise.ttl, this._options.advertise.bootDelay
57+
)
5158
}
5259
}
5360

@@ -57,7 +64,7 @@ class Relay {
5764
* @returns {void}
5865
*/
5966
stop () {
60-
clearTimeout(this._timeout)
67+
clearDelayedInterval(this._timeout)
6168
}
6269

6370
/**
@@ -77,14 +84,7 @@ class Relay {
7784
} else {
7885
log.error(err)
7986
}
80-
81-
return
8287
}
83-
84-
// Restart timeout
85-
this._timeout = setTimeout(() => {
86-
this._advertiseService()
87-
}, this._options.advertise.ttl)
8888
}
8989
}
9090

0 commit comments

Comments
 (0)