This repository has been archived by the owner on Feb 12, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: convert pubsub API to async/await (#2672)
* refactor: convert pubsub API to async/await * refactor: switch wording to not enabled
- Loading branch information
Alan Shaw
authored
Dec 13, 2019
1 parent
0921a82
commit 78f361e
Showing
3 changed files
with
22 additions
and
86 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,90 +1,12 @@ | ||
'use strict' | ||
|
||
const callbackify = require('callbackify') | ||
const OFFLINE_ERROR = require('../utils').OFFLINE_ERROR | ||
const errcode = require('err-code') | ||
|
||
module.exports = function pubsub (self) { | ||
function checkOnlineAndEnabled () { | ||
if (!self.isOnline()) { | ||
throw errcode(new Error(OFFLINE_ERROR), 'ERR_OFFLINE') | ||
} | ||
|
||
if (!self.libp2p.pubsub) { | ||
throw errcode(new Error('pubsub is not enabled'), 'ERR_PUBSUB_DISABLED') | ||
} | ||
} | ||
|
||
module.exports = ({ libp2p }) => { | ||
return { | ||
subscribe: (topic, handler, options, callback) => { | ||
if (typeof options === 'function') { | ||
callback = options | ||
options = {} | ||
} | ||
|
||
if (typeof callback === 'function') { | ||
try { | ||
checkOnlineAndEnabled() | ||
} catch (err) { | ||
return callback(err) | ||
} | ||
|
||
self.libp2p.pubsub.subscribe(topic, handler, options, callback) | ||
return | ||
} | ||
|
||
try { | ||
checkOnlineAndEnabled() | ||
} catch (err) { | ||
return Promise.reject(err) | ||
} | ||
|
||
return self.libp2p.pubsub.subscribe(topic, handler, options) | ||
}, | ||
|
||
unsubscribe: (topic, handler, callback) => { | ||
if (typeof callback === 'function') { | ||
try { | ||
checkOnlineAndEnabled() | ||
} catch (err) { | ||
return callback(err) | ||
} | ||
|
||
self.libp2p.pubsub.unsubscribe(topic, handler, callback) | ||
return | ||
} | ||
|
||
try { | ||
checkOnlineAndEnabled() | ||
} catch (err) { | ||
return Promise.reject(err) | ||
} | ||
|
||
return self.libp2p.pubsub.unsubscribe(topic, handler) | ||
}, | ||
|
||
publish: callbackify(async (topic, data) => { // eslint-disable-line require-await | ||
checkOnlineAndEnabled() | ||
|
||
await self.libp2p.pubsub.publish(topic, data) | ||
}), | ||
|
||
ls: callbackify(async () => { // eslint-disable-line require-await | ||
checkOnlineAndEnabled() | ||
|
||
return self.libp2p.pubsub.ls() | ||
}), | ||
|
||
peers: callbackify(async (topic) => { // eslint-disable-line require-await | ||
checkOnlineAndEnabled() | ||
|
||
return self.libp2p.pubsub.peers(topic) | ||
}), | ||
|
||
setMaxListeners (n) { | ||
checkOnlineAndEnabled() | ||
|
||
self.libp2p.pubsub.setMaxListeners(n) | ||
} | ||
subscribe: (...args) => libp2p.pubsub.subscribe(...args), | ||
unsubscribe: (...args) => libp2p.pubsub.unsubscribe(...args), | ||
publish: (...args) => libp2p.pubsub.publish(...args), | ||
ls: (...args) => libp2p.pubsub.getTopics(...args), | ||
peers: (...args) => libp2p.pubsub.getSubscribers(...args), | ||
setMaxListeners: (n) => libp2p.pubsub.setMaxListeners(n) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters