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

Commit 78f361e

Browse files
author
Alan Shaw
authored
refactor: convert pubsub API to async/await (#2672)
* refactor: convert pubsub API to async/await * refactor: switch wording to not enabled
1 parent 0921a82 commit 78f361e

File tree

3 files changed

+22
-86
lines changed

3 files changed

+22
-86
lines changed

src/core/components/pubsub.js

+7-85
Original file line numberDiff line numberDiff line change
@@ -1,90 +1,12 @@
11
'use strict'
22

3-
const callbackify = require('callbackify')
4-
const OFFLINE_ERROR = require('../utils').OFFLINE_ERROR
5-
const errcode = require('err-code')
6-
7-
module.exports = function pubsub (self) {
8-
function checkOnlineAndEnabled () {
9-
if (!self.isOnline()) {
10-
throw errcode(new Error(OFFLINE_ERROR), 'ERR_OFFLINE')
11-
}
12-
13-
if (!self.libp2p.pubsub) {
14-
throw errcode(new Error('pubsub is not enabled'), 'ERR_PUBSUB_DISABLED')
15-
}
16-
}
17-
3+
module.exports = ({ libp2p }) => {
184
return {
19-
subscribe: (topic, handler, options, callback) => {
20-
if (typeof options === 'function') {
21-
callback = options
22-
options = {}
23-
}
24-
25-
if (typeof callback === 'function') {
26-
try {
27-
checkOnlineAndEnabled()
28-
} catch (err) {
29-
return callback(err)
30-
}
31-
32-
self.libp2p.pubsub.subscribe(topic, handler, options, callback)
33-
return
34-
}
35-
36-
try {
37-
checkOnlineAndEnabled()
38-
} catch (err) {
39-
return Promise.reject(err)
40-
}
41-
42-
return self.libp2p.pubsub.subscribe(topic, handler, options)
43-
},
44-
45-
unsubscribe: (topic, handler, callback) => {
46-
if (typeof callback === 'function') {
47-
try {
48-
checkOnlineAndEnabled()
49-
} catch (err) {
50-
return callback(err)
51-
}
52-
53-
self.libp2p.pubsub.unsubscribe(topic, handler, callback)
54-
return
55-
}
56-
57-
try {
58-
checkOnlineAndEnabled()
59-
} catch (err) {
60-
return Promise.reject(err)
61-
}
62-
63-
return self.libp2p.pubsub.unsubscribe(topic, handler)
64-
},
65-
66-
publish: callbackify(async (topic, data) => { // eslint-disable-line require-await
67-
checkOnlineAndEnabled()
68-
69-
await self.libp2p.pubsub.publish(topic, data)
70-
}),
71-
72-
ls: callbackify(async () => { // eslint-disable-line require-await
73-
checkOnlineAndEnabled()
74-
75-
return self.libp2p.pubsub.ls()
76-
}),
77-
78-
peers: callbackify(async (topic) => { // eslint-disable-line require-await
79-
checkOnlineAndEnabled()
80-
81-
return self.libp2p.pubsub.peers(topic)
82-
}),
83-
84-
setMaxListeners (n) {
85-
checkOnlineAndEnabled()
86-
87-
self.libp2p.pubsub.setMaxListeners(n)
88-
}
5+
subscribe: (...args) => libp2p.pubsub.subscribe(...args),
6+
unsubscribe: (...args) => libp2p.pubsub.unsubscribe(...args),
7+
publish: (...args) => libp2p.pubsub.publish(...args),
8+
ls: (...args) => libp2p.pubsub.getTopics(...args),
9+
peers: (...args) => libp2p.pubsub.getSubscribers(...args),
10+
setMaxListeners: (n) => libp2p.pubsub.setMaxListeners(n)
8911
}
9012
}

src/core/components/start.js

+4-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ const PeerBook = require('peer-book')
55
const IPNS = require('../ipns')
66
const routingConfig = require('../ipns/routing/config')
77
const defer = require('p-defer')
8-
const { AlreadyInitializedError } = require('../errors')
8+
const { AlreadyInitializedError, NotEnabledError } = require('../errors')
99
const Commands = require('./')
1010

1111
module.exports = ({
@@ -134,6 +134,9 @@ function createApi ({
134134
config: Commands.config({ repo }),
135135
init: () => { throw new AlreadyInitializedError() },
136136
ping: Commands.ping({ libp2p }),
137+
pubsub: libp2p.pubsub
138+
? Commands.pubsub({ libp2p })
139+
: () => { throw new NotEnabledError('pubsub not enabled') },
137140
start: () => apiManager.api,
138141
stop
139142
}

src/core/errors.js

+11
Original file line numberDiff line numberDiff line change
@@ -41,3 +41,14 @@ class NotStartedError extends Error {
4141

4242
NotStartedError.code = 'ERR_NOT_STARTED'
4343
exports.NotStartedError = NotStartedError
44+
45+
class NotEnabledError extends Error {
46+
constructor (message = 'not enabled') {
47+
super(message)
48+
this.name = 'NotEnabledError'
49+
this.code = NotEnabledError.code
50+
}
51+
}
52+
53+
NotEnabledError.code = 'ERR_NOT_ENABLED'
54+
exports.NotEnabledError = NotEnabledError

0 commit comments

Comments
 (0)