Skip to content
This repository was archived by the owner on Feb 12, 2024. It is now read-only.

Commit 734b4b9

Browse files
committed
fix: republish working for starting and stopping multiple times
1 parent b133d21 commit 734b4b9

File tree

1 file changed

+53
-18
lines changed

1 file changed

+53
-18
lines changed

src/core/ipns/republisher.js

+53-18
Original file line numberDiff line numberDiff line change
@@ -22,35 +22,68 @@ class IpnsRepublisher {
2222
this._publisher = publisher
2323
this._ipfs = ipfs
2424
this._repo = ipfs._repo
25-
this._timeoutId = null
26-
this._canceled = false
27-
this._onCancel = null
25+
this._republishHandle = null
2826
}
2927

3028
start () {
31-
const periodically = (cb) => {
32-
this._republishEntries(this._ipfs._peerInfo.id.privKey, this._ipfs._options.pass, () => {
33-
if (this._canceled) {
34-
return this._onCancel()
29+
if (this._republishHandle) {
30+
const errMsg = 'already running'
31+
32+
log.error(errMsg)
33+
throw errcode(new Error(errMsg), 'ERR_REPUBLISH_ALREADY_RUNNING')
34+
}
35+
36+
// TODO: this handler should be isolated in another module
37+
const republishHandle = {
38+
_onCancel: null,
39+
_timeoutId: null,
40+
runPeriodically: (fn, period) => {
41+
republishHandle._timeoutId = setTimeout(() => {
42+
republishHandle._timeoutId = null
43+
44+
fn((nextPeriod) => {
45+
// Was republish cancelled while fn was being called?
46+
if (republishHandle._onCancel) {
47+
return republishHandle._onCancel()
48+
}
49+
// Schedule next
50+
republishHandle.runPeriodically(fn, nextPeriod || period)
51+
})
52+
}, period)
53+
},
54+
cancel: (cb) => {
55+
// Not currently running a republish, can callback immediately
56+
if (republishHandle._timeoutId) {
57+
clearTimeout(republishHandle._timeoutId)
58+
return cb()
3559
}
36-
this._timeoutId = setTimeout(() => periodically(cb), defaultBroadcastInterval)
37-
})
60+
// Wait for republish to finish then call callback
61+
republishHandle._onCancel = cb
62+
}
3863
}
3964

40-
setTimeout(() => {
41-
periodically()
65+
const { privKey } = this._ipfs._peerInfo.id
66+
const { pass } = this._ipfs._options
67+
68+
republishHandle.runPeriodically((done) => {
69+
this._republishEntries(privKey, pass, () => done(defaultBroadcastInterval))
4270
}, minute)
71+
72+
this._republishHandle = republishHandle
4373
}
4474

45-
stop (cb) {
46-
this._canceled = true
47-
if (this._timeoutId || !this._onCancel) {
48-
// Not running
49-
clearTimeout(this._timeoutId)
50-
return cb()
75+
stop (callback) {
76+
const republishHandle = this._republishHandle
77+
78+
if (!republishHandle) {
79+
const errMsg = 'not running'
80+
81+
log.error(errMsg)
82+
return callback(errcode(new Error(errMsg), 'ERR_REPUBLISH_NOT_RUNNING'))
5183
}
5284

53-
this._onCancel = cb
85+
this._republishHandle = null
86+
republishHandle.cancel(callback)
5487
}
5588

5689
_republishEntries (privateKey, pass, callback) {
@@ -91,6 +124,8 @@ class IpnsRepublisher {
91124
callback(null)
92125
})
93126
})
127+
} else {
128+
callback(null)
94129
}
95130
})
96131
}

0 commit comments

Comments
 (0)