Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: should subscribe a topic only once #6

Merged
merged 1 commit into from
Nov 15, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@
"detect-node": "^2.0.4",
"dirty-chai": "^2.0.1",
"ipfs": "~0.31.5",
"ipfsd-ctl": "~0.39.1"
"ipfsd-ctl": "~0.39.1",
"sinon": "^7.0.0"
},
"contributors": [
"Vasco Santos <vasco.santos@moxy.studio>",
Expand Down
25 changes: 18 additions & 7 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,17 +86,28 @@ class DatastorePubsub {

const stringifiedTopic = key.toString()

// Subscribe
this._pubsub.subscribe(stringifiedTopic, this._handleSubscription, (err) => {
this._pubsub.ls((err, res) => {
if (err) {
const errMsg = `cannot subscribe topic ${stringifiedTopic}`
return callback(err)
}

log.error(errMsg)
return callback(errcode(new Error(errMsg), 'ERR_SUBSCRIBING_TOPIC'))
// If already subscribed, just try to get it
if (res && Array.isArray(res) && res.indexOf(stringifiedTopic) > -1) {
return this._getLocal(key, callback)
}
log(`subscribed values for key ${stringifiedTopic}`)

this._getLocal(key, callback)
// Subscribe
this._pubsub.subscribe(stringifiedTopic, this._handleSubscription, (err) => {
if (err) {
const errMsg = `cannot subscribe topic ${stringifiedTopic}`

log.error(errMsg)
return callback(errcode(new Error(errMsg), 'ERR_SUBSCRIBING_TOPIC'))
}
log(`subscribed values for key ${stringifiedTopic}`)

this._getLocal(key, callback)
})
})
}

Expand Down
17 changes: 17 additions & 0 deletions test/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const chai = require('chai')
const dirtyChai = require('dirty-chai')
const expect = chai.expect
chai.use(dirtyChai)
const sinon = require('sinon')

const isNode = require('detect-node')
const parallel = require('async/parallel')
Expand Down Expand Up @@ -495,4 +496,20 @@ describe('datastore-pubsub', function () {
})
})
})

it('should subscribe a topic only once', function (done) {
const dsPubsubA = new DatastorePubsub(pubsubA, datastoreA, peerIdA, smoothValidator)

sinon.spy(pubsubA, 'subscribe')

dsPubsubA.get(key, (err) => {
expect(err).to.exist() // not locally stored record
dsPubsubA.get(key, (err) => {
expect(err).to.exist() // not locally stored record
expect(pubsubA.subscribe.calledOnce).to.equal(true)

done()
})
})
})
})