@@ -4,6 +4,11 @@ const debug = require('debug')
4
4
const log = debug ( 'libp2p:relay' )
5
5
log . error = debug ( 'libp2p:relay:error' )
6
6
7
+ const {
8
+ setDelayedInterval,
9
+ clearDelayedInterval
10
+ } = require ( 'set-delayed-interval' )
11
+
7
12
const AutoRelay = require ( './auto-relay' )
8
13
const { namespaceToCid } = require ( './utils' )
9
14
const {
@@ -33,6 +38,8 @@ class Relay {
33
38
34
39
// Create autoRelay if enabled
35
40
this . _autoRelay = this . _options . autoRelay . enabled && new AutoRelay ( { libp2p, ...this . _options . autoRelay } )
41
+
42
+ this . _advertiseService = this . _advertiseService . bind ( this )
36
43
}
37
44
38
45
/**
@@ -45,9 +52,9 @@ class Relay {
45
52
const canHop = this . _options . hop . enabled
46
53
47
54
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
+ )
51
58
}
52
59
}
53
60
@@ -57,7 +64,7 @@ class Relay {
57
64
* @returns {void }
58
65
*/
59
66
stop ( ) {
60
- clearTimeout ( this . _timeout )
67
+ clearDelayedInterval ( this . _timeout )
61
68
}
62
69
63
70
/**
@@ -77,14 +84,7 @@ class Relay {
77
84
} else {
78
85
log . error ( err )
79
86
}
80
-
81
- return
82
87
}
83
-
84
- // Restart timeout
85
- this . _timeout = setTimeout ( ( ) => {
86
- this . _advertiseService ( )
87
- } , this . _options . advertise . ttl )
88
88
}
89
89
}
90
90
0 commit comments