From a138daedc39cb60d5e8bd46cdbb42df24ee6e6ab Mon Sep 17 00:00:00 2001 From: Alan Shaw Date: Thu, 12 Dec 2019 22:52:38 +0000 Subject: [PATCH 1/2] refactor: convert pubsub API to async/await --- src/core/components/pubsub.js | 92 +++-------------------------------- src/core/components/start.js | 5 +- src/core/errors.js | 11 +++++ 3 files changed, 22 insertions(+), 86 deletions(-) diff --git a/src/core/components/pubsub.js b/src/core/components/pubsub.js index 8c5916b906..0954304f46 100644 --- a/src/core/components/pubsub.js +++ b/src/core/components/pubsub.js @@ -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) } } diff --git a/src/core/components/start.js b/src/core/components/start.js index f9f41c7458..f937e60631 100644 --- a/src/core/components/start.js +++ b/src/core/components/start.js @@ -5,7 +5,7 @@ const PeerBook = require('peer-book') const IPNS = require('../ipns') const routingConfig = require('../ipns/routing/config') const defer = require('p-defer') -const { AlreadyInitializedError } = require('../errors') +const { AlreadyInitializedError, DisabledError } = require('../errors') const Commands = require('./') module.exports = ({ @@ -133,6 +133,9 @@ function createApi ({ add, config: Commands.config({ repo }), init: () => { throw new AlreadyInitializedError() }, + pubsub: libp2p.pubsub + ? Commands.pubsub({ libp2p }) + : () => { throw new DisabledError('pubsub disabled') }, start: () => apiManager.api, stop } diff --git a/src/core/errors.js b/src/core/errors.js index a2ab2542a5..11ab4bb7b5 100644 --- a/src/core/errors.js +++ b/src/core/errors.js @@ -41,3 +41,14 @@ class NotStartedError extends Error { NotStartedError.code = 'ERR_NOT_STARTED' exports.NotStartedError = NotStartedError + +class DisabledError extends Error { + constructor (message = 'disabled') { + super(message) + this.name = 'DisabledError' + this.code = DisabledError.code + } +} + +DisabledError.code = 'ERR_DISABLED' +exports.DisabledError = DisabledError From fd0c49dd7c0ba27c40eb4661a5345f25375f53e0 Mon Sep 17 00:00:00 2001 From: Alan Shaw Date: Thu, 12 Dec 2019 22:56:39 +0000 Subject: [PATCH 2/2] refactor: switch wording to not enabled --- src/core/components/start.js | 4 ++-- src/core/errors.js | 12 ++++++------ 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/src/core/components/start.js b/src/core/components/start.js index f937e60631..13c27df439 100644 --- a/src/core/components/start.js +++ b/src/core/components/start.js @@ -5,7 +5,7 @@ const PeerBook = require('peer-book') const IPNS = require('../ipns') const routingConfig = require('../ipns/routing/config') const defer = require('p-defer') -const { AlreadyInitializedError, DisabledError } = require('../errors') +const { AlreadyInitializedError, NotEnabledError } = require('../errors') const Commands = require('./') module.exports = ({ @@ -135,7 +135,7 @@ function createApi ({ init: () => { throw new AlreadyInitializedError() }, pubsub: libp2p.pubsub ? Commands.pubsub({ libp2p }) - : () => { throw new DisabledError('pubsub disabled') }, + : () => { throw new NotEnabledError('pubsub not enabled') }, start: () => apiManager.api, stop } diff --git a/src/core/errors.js b/src/core/errors.js index 11ab4bb7b5..c7cf7ac938 100644 --- a/src/core/errors.js +++ b/src/core/errors.js @@ -42,13 +42,13 @@ class NotStartedError extends Error { NotStartedError.code = 'ERR_NOT_STARTED' exports.NotStartedError = NotStartedError -class DisabledError extends Error { - constructor (message = 'disabled') { +class NotEnabledError extends Error { + constructor (message = 'not enabled') { super(message) - this.name = 'DisabledError' - this.code = DisabledError.code + this.name = 'NotEnabledError' + this.code = NotEnabledError.code } } -DisabledError.code = 'ERR_DISABLED' -exports.DisabledError = DisabledError +NotEnabledError.code = 'ERR_NOT_ENABLED' +exports.NotEnabledError = NotEnabledError