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

Commit 6efd705

Browse files
committed
Try get tests working
1 parent 699bd25 commit 6efd705

File tree

3 files changed

+39
-16
lines changed

3 files changed

+39
-16
lines changed

src/core/components/floodsub.js

+34-14
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,29 @@ const Readable = require('stream').Readable
77
const OFFLINE_ERROR = require('../utils').OFFLINE_ERROR
88
const FSUB_ERROR = new Error(`FloodSub is not started.`)
99

10-
module.exports = function floodsub (self) {
11-
return {
12-
start: promisify((callback) => {
13-
if (!self.isOnline()) {
14-
throw OFFLINE_ERROR
15-
}
10+
/* Internal subscriptions state and functions */
11+
let subscriptions = {}
1612

17-
self._floodsub = new FloodSub(self._libp2pNode)
18-
return callback(null, self._floodsub)
19-
}),
13+
const addSubscription = (topic, request, stream) => {
14+
subscriptions[topic] = { request: request, stream: stream }
15+
}
16+
17+
const removeSubscription = promisify((topic, callback) => {
18+
if (!subscriptions[topic]) {
19+
return callback(new Error(`Not subscribed to ${topic}`))
20+
}
2021

22+
// subscriptions[topic].request.abort()
23+
// subscriptions[topic].stream.end()
24+
delete subscriptions[topic]
25+
26+
if (callback) {
27+
callback(null)
28+
}
29+
})
30+
31+
module.exports = function floodsub (self) {
32+
return {
2133
subscribe: promisify((topic, options, callback) => {
2234
// TODO: Clarify with @diasdavid what to do with the `options.discover` param
2335
// Ref: https://github.com/ipfs/js-ipfs-api/pull/377/files#diff-f0c61c06fd5dc36b6f760b7ea97b1862R50
@@ -34,11 +46,12 @@ module.exports = function floodsub (self) {
3446
throw FSUB_ERROR
3547
}
3648

37-
let rs = new Readable()
38-
rs.cancel = () => self._floodsub.unsubscribe(topic)
39-
49+
let stream = new Readable({ objectMode: true })
50+
stream._read = () => {}
51+
4052
self._floodsub.on(topic, (data) => {
41-
rs.emit('data', {
53+
console.log("DATA", data.toString())
54+
stream.emit('data', {
4255
data: data.toString(),
4356
topicIDs: [topic]
4457
})
@@ -50,7 +63,14 @@ module.exports = function floodsub (self) {
5063
return callback(err)
5164
}
5265

53-
callback(null, rs)
66+
stream.cancel = promisify((cb) => {
67+
self._floodsub.unsubscribe(topic)
68+
removeSubscription(topic, cb)
69+
})
70+
71+
// Add the request to the active subscriptions and return the stream
72+
addSubscription(topic, null, stream)
73+
callback(null, stream)
5474
}),
5575

5676
publish: promisify((topic, data, callback) => {

src/core/components/go-online.js

+5
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
const series = require('async/series')
44
const Bitswap = require('ipfs-bitswap')
5+
const FloodSub = require('libp2p-floodsub')
56

67
module.exports = function goOnline (self) {
78
return (cb) => {
@@ -21,6 +22,10 @@ module.exports = function goOnline (self) {
2122
)
2223
self._bitswap.start()
2324
self._blockService.goOnline(self._bitswap)
25+
26+
self._floodsub = new FloodSub(self._libp2pNode)
27+
// self._floodsub.start()
28+
2429
cb()
2530
})
2631
}

test/core/both/test-pubsub.js

-2
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@ const IPFSFactory = require('../../utils/factory-core')
66

77
let factory
88

9-
console.log("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!")
10-
119
const common = {
1210
setup: function (cb) {
1311
factory = new IPFSFactory()

0 commit comments

Comments
 (0)