|
1 | 1 | 'use strict'
|
2 | 2 |
|
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 }) => { |
18 | 4 | 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) |
89 | 11 | }
|
90 | 12 | }
|
0 commit comments