Skip to content
This repository has been archived by the owner on Jun 27, 2023. It is now read-only.

feat: emit full messages, instead of just data #13

Merged
merged 1 commit into from
Dec 21, 2016
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
10 changes: 6 additions & 4 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ class FloodSub extends EventEmitter {
this.cache.put(seqno)

// 2. emit to self
this._emitMessages(msg.topicCIDs, [msg.data])
this._emitMessages(msg.topicCIDs, [msg])

// 3. propagate msg to others
this._forwardMessages(msg.topicCIDs, [msg])
Expand Down Expand Up @@ -213,9 +213,6 @@ class FloodSub extends EventEmitter {
topics = ensureArray(topics)
messages = ensureArray(messages)

// Emit to self if I'm interested
this._emitMessages(topics, messages)

const from = this.libp2p.peerInfo.id.toB58String()

const buildMessage = (msg) => {
Expand All @@ -230,6 +227,11 @@ class FloodSub extends EventEmitter {
}
}

const msgObjects = messages.map(buildMessage)

// Emit to self if I'm interested
this._emitMessages(topics, msgObjects)

// send to all the other peers
this._forwardMessages(topics, messages.map(buildMessage))
}
Expand Down
9 changes: 6 additions & 3 deletions test/2-nodes.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ describe('basics', () => {
function shouldNotHappen (msg) { expect.fail() }

psA.once('Z', (msg) => {
expect(msg.toString()).to.equal('hey')
expect(msg.data.toString()).to.equal('hey')
psB.removeListener('Z', shouldNotHappen)
done()
})
Expand All @@ -96,7 +96,7 @@ describe('basics', () => {

psA.once('Z', (msg) => {
psA.once('Z', shouldNotHappen)
expect(msg.toString()).to.equal('banana')
expect(msg.data.toString()).to.equal('banana')
setTimeout(() => {
psA.removeListener('Z', shouldNotHappen)
psB.removeListener('Z', shouldNotHappen)
Expand All @@ -117,7 +117,10 @@ describe('basics', () => {
psA.on('Z', receivedMsg)

function receivedMsg (msg) {
expect(msg.toString()).to.equal('banana')
expect(msg.data.toString()).to.equal('banana')
expect(msg.from).to.be.eql(psB.libp2p.peerInfo.id.toB58String())
expect(Buffer.isBuffer(msg.seqno)).to.be.true
expect(msg.topicCIDs).to.be.eql(['Z'])

if (++counter === 10) {
psA.removeListener('Z', receivedMsg)
Expand Down
6 changes: 3 additions & 3 deletions test/multiple-nodes.js
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ describe('multiple nodes', () => {
a.ps.publish('Z', new Buffer('hey'))

function incMsg (msg) {
expect(msg.toString()).to.equal('hey')
expect(msg.data.toString()).to.equal('hey')
check()
}

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

function incMsg (msg) {
expect(msg.toString()).to.equal('hey')
expect(msg.data.toString()).to.equal('hey')
check()
}

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

function incMsg (msg) {
expect(msg.toString()).to.equal('hey from c')
expect(msg.data.toString()).to.equal('hey from c')
check()
}

Expand Down