@@ -5,16 +5,15 @@ const WorkerQueue = require('./queue')
55
66const { blockKeyToCid } = require ( '../utils' )
77
8- // const initialDelay = 15000
9- const initialDelay = 3000
10-
118class Reprovider {
129 /**
1310 * Reprovider goal is to reannounce blocks to the network.
1411 * @param {object } contentRouting
1512 * @param {Blockstore } blockstore
1613 * @param {object } options
17- * @memberof Reprovider
14+ * @param {string } options.delay reprovider initial delay in human friendly time
15+ * @param {string } options.interval reprovider interval in human friendly time
16+ * @param {string } options.strategy reprovider strategy
1817 */
1918 constructor ( contentRouting , blockstore , options ) {
2019 this . _contentRouting = contentRouting
@@ -33,7 +32,7 @@ class Reprovider {
3332 // Start doing reprovides after the initial delay
3433 this . _timeoutId = setTimeout ( ( ) => {
3534 this . _runPeriodically ( )
36- } , initialDelay )
35+ } , this . _options . delay )
3736 }
3837
3938 /**
@@ -49,27 +48,34 @@ class Reprovider {
4948 }
5049
5150 /**
52- * Run reprovide on every `options.interval` ms
51+ * Run reprovide on every `options.interval` ms recursively
5352 * @returns {void }
5453 */
5554 async _runPeriodically ( ) {
56- while ( this . _timeoutId ) {
57- const blocks = await promisify ( ( callback ) => this . _blockstore . query ( { } , callback ) ) ( )
58-
59- // TODO strategy logic here
60- if ( this . _options . strategy === 'pinned' ) {
55+ // Verify if stopped
56+ if ( ! this . _timeoutId ) return
6157
62- } else if ( this . _options . strategy === 'pinned' ) {
58+ // TODO strategy logic here
59+ const blocks = await promisify ( ( callback ) => this . _blockstore . query ( { } , callback ) ) ( )
6360
64- }
61+ if ( this . _options . strategy === 'pinned' ) {
6562
66- await this . _worker . execute ( blocks )
63+ } else if ( this . _options . strategy === 'pinned' ) {
6764
68- // Each subsequent walk should run on a `this._options.interval` interval
69- await new Promise ( resolve => {
70- this . _timeoutId = setTimeout ( resolve , this . _options . interval )
71- } )
7265 }
66+
67+ // Verify if stopped
68+ if ( ! this . _timeoutId ) return
69+
70+ await this . _worker . execute ( blocks )
71+
72+ // Verify if stopped
73+ if ( ! this . _timeoutId ) return
74+
75+ // Each subsequent walk should run on a `this._options.interval` interval
76+ this . _timeoutId = setTimeout ( ( ) => {
77+ this . _runPeriodically ( )
78+ } , this . _options . interval )
7379 }
7480
7581 /**
0 commit comments