Skip to content

Commit 9e96c6e

Browse files
committed
feat(#8): at topic creation create a new immutable meta subTopic
1 parent bff876f commit 9e96c6e

File tree

3 files changed

+38
-19
lines changed

3 files changed

+38
-19
lines changed

src/index.js

+17-5
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ const assert = require('assert')
55
const lp = require('pull-length-prefixed')
66
const pull = require('pull-stream')
77
const CID = require('cids')
8-
const { eachLimit, series } = require('async')
8+
const { eachLimit, series, waterfall } = require('async')
99

1010
const log = require('./utils/logger')
1111
const { getTopic } = require('./utils/dht-helpers')
@@ -340,8 +340,8 @@ class Pulsarcast extends EventEmitter {
340340
topicsToLink.push(...Object.values(topicOptions.subTopics))
341341
}
342342

343-
series([
344-
// Check the existence of parent ans subTopics
343+
waterfall([
344+
// Check the existence of parent and subTopics
345345
(cb) => {
346346
if (topicsToLink.length === 0) return setImmediate(cb)
347347
eachLimit(
@@ -351,17 +351,29 @@ class Pulsarcast extends EventEmitter {
351351
cb
352352
)
353353
},
354+
// Setup the meta topic node
354355
(cb) => {
356+
const topicNode = new TopicNode(`meta-${topicName}`, myId, topicOptions)
357+
358+
this._addTopic(topicNode, (err, linkedTopic, topicCID) => {
359+
if (err) return cb(err)
360+
361+
this.subscriptions.add(topicCID.toBaseEncodedString())
362+
cb(null, topicCID)
363+
})
364+
},
365+
(metaCid, cb) => {
366+
topicOptions.subTopics.meta = metaCid.toBaseEncodedString()
355367
const topicNode = new TopicNode(topicName, myId, topicOptions)
356368

357369
this._addTopic(topicNode, (err, linkedTopic, topicCID) => {
358-
if (err) return callback(err)
370+
if (err) return cb(err)
359371

360372
this.subscriptions.add(topicCID.toBaseEncodedString())
361373
this.rpc.send.topic.new(linkedTopic, cb)
362374
})
363375
}
364-
], (err, [_, [cid, topicNode]]) => callback(err, cid, topicNode))
376+
], (err, cid, topicNode) => callback(err, cid, topicNode))
365377
}
366378

367379
updateTopic (topicB58Str, options, callback) {

test/integration/2-nodes.js

+10-5
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
const chai = require('chai')
66
const expect = chai.expect
77
const { parallel } = require('async')
8+
const CID = require('cids')
89

910
const Pulsarcast = require('../../src')
1011
const TopicNode = require('../../src/dag/topic-node')
@@ -40,11 +41,13 @@ describe('2 nodes', () => {
4041
nodes[0].createTopic('test', (err, savedCID, topicNode) => {
4142
expect(err).to.not.exist
4243
expect(topicNode).to.be.an.instanceof(TopicNode)
44+
expect(topicNode.subTopics.meta).to.be.an.instanceof(CID)
4345
topicNode.getCID((err, cid) => {
4446
expect(err).to.not.exist
4547
const topicB58Str = cid.toBaseEncodedString()
4648
expect(cid.equals(savedCID)).to.be.true
47-
expect(nodes[0].subscriptions.size).to.equal(1)
49+
// Topic and meta topic
50+
expect(nodes[0].subscriptions.size).to.equal(2)
4851
expect(nodes[0].subscriptions.has(topicB58Str)).to.be.true
4952
topic = topicNode
5053
topicCID = cid
@@ -57,12 +60,13 @@ describe('2 nodes', () => {
5760
nodes[0].createTopic('test-2.0', {parent: topicCID.toBaseEncodedString()}, (err, savedCID, childTopicNode) => {
5861
expect(err).to.not.exist
5962
expect(childTopicNode).to.be.an.instanceof(TopicNode)
60-
childTopicNode.parent.equals(topic)
63+
expect(childTopicNode.subTopics.meta).to.be.an.instanceof(CID)
64+
expect(childTopicNode.parent.equals(topicCID)).to.be.true
6165
childTopicNode.getCID((err, cid) => {
6266
expect(err).to.not.exist
6367
const topicB58Str = cid.toBaseEncodedString()
6468
expect(cid.equals(savedCID)).to.be.true
65-
expect(nodes[0].subscriptions.size).to.equal(2)
69+
expect(nodes[0].subscriptions.size).to.equal(4)
6670
expect(nodes[0].subscriptions.has(topicB58Str)).to.be.true
6771
done()
6872
})
@@ -74,13 +78,14 @@ describe('2 nodes', () => {
7478
{subTopics: {'some-topic': topicCID.toBaseEncodedString()}},
7579
(err, savedCID, newTopicNode) => {
7680
expect(err).to.not.exist
77-
newTopicNode.subTopics['some-topic'].equals(topic)
81+
expect(newTopicNode.subTopics['some-topic'].equals(topicCID)).to.be.true
82+
expect(newTopicNode.subTopics.meta).to.be.an.instanceof(CID)
7883
expect(newTopicNode).to.be.an.instanceof(TopicNode)
7984
newTopicNode.getCID((err, cid) => {
8085
expect(err).to.not.exist
8186
const topicB58Str = cid.toBaseEncodedString()
8287
expect(cid.equals(savedCID)).to.be.true
83-
expect(nodes[0].subscriptions.size).to.equal(3)
88+
expect(nodes[0].subscriptions.size).to.equal(6)
8489
expect(nodes[0].subscriptions.has(topicB58Str)).to.be.true
8590
done()
8691
})

test/integration/multiple-nodes.js

+11-9
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
const chai = require('chai')
66
const expect = chai.expect
77
const { each } = require('async')
8+
const CID = require('cids')
9+
810
const Pulsarcast = require('../../src')
911
const TopicNode = require('../../src/dag/topic-node')
1012
const EventNode = require('../../src/dag/event-node')
@@ -16,9 +18,7 @@ describe('multiple nodes', function () {
1618
// Topics
1719
let topic
1820
let topicCID
19-
let parentTopic
2021
let parentTopicCID
21-
let subTopic
2222
let subTopicCID
2323

2424
// Parameters
@@ -49,13 +49,14 @@ describe('multiple nodes', function () {
4949
nodes[publisher + 1].createTopic('test-parent', (err, savedCID, topicNode) => {
5050
expect(err).to.not.exist
5151
expect(topicNode).to.be.an.instanceof(TopicNode)
52+
expect(topicNode.subTopics.meta).to.be.an.instanceof(CID)
5253
topicNode.getCID((err, cid) => {
5354
expect(err).to.not.exist
5455
const topicB58Str = cid.toBaseEncodedString()
5556
expect(cid.equals(savedCID)).to.be.true
56-
expect(nodes[publisher + 1].subscriptions.size).to.equal(1)
57+
// Topic and meta topic
58+
expect(nodes[publisher + 1].subscriptions.size).to.equal(2)
5759
expect(nodes[publisher + 1].subscriptions.has(topicB58Str)).to.be.true
58-
parentTopic = topicNode
5960
parentTopicCID = cid
6061
done()
6162
})
@@ -66,13 +67,13 @@ describe('multiple nodes', function () {
6667
nodes[publisher + 1].createTopic('test-subtopic', (err, savedCID, topicNode) => {
6768
expect(err).to.not.exist
6869
expect(topicNode).to.be.an.instanceof(TopicNode)
70+
expect(topicNode.subTopics.meta).to.be.an.instanceof(CID)
6971
topicNode.getCID((err, cid) => {
7072
expect(err).to.not.exist
7173
const topicB58Str = cid.toBaseEncodedString()
7274
expect(cid.equals(savedCID)).to.be.true
73-
expect(nodes[publisher + 1].subscriptions.size).to.equal(2)
75+
expect(nodes[publisher + 1].subscriptions.size).to.equal(4)
7476
expect(nodes[publisher + 1].subscriptions.has(topicB58Str)).to.be.true
75-
subTopic = topicNode
7677
subTopicCID = cid
7778
done()
7879
})
@@ -89,13 +90,14 @@ describe('multiple nodes', function () {
8990
}, (err, savedCID, topicNode) => {
9091
expect(err).to.not.exist
9192
expect(topicNode).to.be.an.instanceof(TopicNode)
92-
topicNode.parent.equals(parentTopic)
93-
topicNode.subTopics['test-subtopic'].equals(subTopic)
93+
expect(topicNode.subTopics.meta).to.be.an.instanceof(CID)
94+
expect(topicNode.parent.equals(parentTopicCID)).to.be.true
95+
expect(topicNode.subTopics['test-subtopic'].equals(subTopicCID)).to.be.true
9496
topicNode.getCID((err, cid) => {
9597
expect(err).to.not.exist
9698
const topicB58Str = cid.toBaseEncodedString()
9799
expect(cid.equals(savedCID)).to.be.true
98-
expect(nodes[publisher].subscriptions.size).to.equal(1)
100+
expect(nodes[publisher].subscriptions.size).to.equal(2)
99101
expect(nodes[publisher].subscriptions.has(topicB58Str)).to.be.true
100102
topic = topicNode
101103
topicCID = cid

0 commit comments

Comments
 (0)