Skip to content

Commit 300bf95

Browse files
dignifiedquiredaviddias
authored andcommitted
feat: emit full messages, instead of just data (#13)
1 parent c5a8bfc commit 300bf95

File tree

3 files changed

+15
-10
lines changed

3 files changed

+15
-10
lines changed

src/index.js

+6-4
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ class FloodSub extends EventEmitter {
158158
this.cache.put(seqno)
159159

160160
// 2. emit to self
161-
this._emitMessages(msg.topicCIDs, [msg.data])
161+
this._emitMessages(msg.topicCIDs, [msg])
162162

163163
// 3. propagate msg to others
164164
this._forwardMessages(msg.topicCIDs, [msg])
@@ -213,9 +213,6 @@ class FloodSub extends EventEmitter {
213213
topics = ensureArray(topics)
214214
messages = ensureArray(messages)
215215

216-
// Emit to self if I'm interested
217-
this._emitMessages(topics, messages)
218-
219216
const from = this.libp2p.peerInfo.id.toB58String()
220217

221218
const buildMessage = (msg) => {
@@ -230,6 +227,11 @@ class FloodSub extends EventEmitter {
230227
}
231228
}
232229

230+
const msgObjects = messages.map(buildMessage)
231+
232+
// Emit to self if I'm interested
233+
this._emitMessages(topics, msgObjects)
234+
233235
// send to all the other peers
234236
this._forwardMessages(topics, messages.map(buildMessage))
235237
}

test/2-nodes.js

+6-3
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ describe('basics', () => {
8181
function shouldNotHappen (msg) { expect.fail() }
8282

8383
psA.once('Z', (msg) => {
84-
expect(msg.toString()).to.equal('hey')
84+
expect(msg.data.toString()).to.equal('hey')
8585
psB.removeListener('Z', shouldNotHappen)
8686
done()
8787
})
@@ -96,7 +96,7 @@ describe('basics', () => {
9696

9797
psA.once('Z', (msg) => {
9898
psA.once('Z', shouldNotHappen)
99-
expect(msg.toString()).to.equal('banana')
99+
expect(msg.data.toString()).to.equal('banana')
100100
setTimeout(() => {
101101
psA.removeListener('Z', shouldNotHappen)
102102
psB.removeListener('Z', shouldNotHappen)
@@ -117,7 +117,10 @@ describe('basics', () => {
117117
psA.on('Z', receivedMsg)
118118

119119
function receivedMsg (msg) {
120-
expect(msg.toString()).to.equal('banana')
120+
expect(msg.data.toString()).to.equal('banana')
121+
expect(msg.from).to.be.eql(psB.libp2p.peerInfo.id.toB58String())
122+
expect(Buffer.isBuffer(msg.seqno)).to.be.true
123+
expect(msg.topicCIDs).to.be.eql(['Z'])
121124

122125
if (++counter === 10) {
123126
psA.removeListener('Z', receivedMsg)

test/multiple-nodes.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ describe('multiple nodes', () => {
123123
a.ps.publish('Z', new Buffer('hey'))
124124

125125
function incMsg (msg) {
126-
expect(msg.toString()).to.equal('hey')
126+
expect(msg.data.toString()).to.equal('hey')
127127
check()
128128
}
129129

@@ -156,7 +156,7 @@ describe('multiple nodes', () => {
156156
b.ps.publish('Z', new Buffer('hey'))
157157

158158
function incMsg (msg) {
159-
expect(msg.toString()).to.equal('hey')
159+
expect(msg.data.toString()).to.equal('hey')
160160
check()
161161
}
162162

@@ -268,7 +268,7 @@ describe('multiple nodes', () => {
268268
c.ps.publish('Z', new Buffer('hey from c'))
269269

270270
function incMsg (msg) {
271-
expect(msg.toString()).to.equal('hey from c')
271+
expect(msg.data.toString()).to.equal('hey from c')
272272
check()
273273
}
274274

0 commit comments

Comments
 (0)