Skip to content

Commit

Permalink
fix: remove use of assert module (#65)
Browse files Browse the repository at this point in the history
The polyfill is big, we can simulate it by throwing an Error and it doesn't work under React Native.
  • Loading branch information
achingbrain authored Feb 14, 2020
1 parent 978444d commit e0a37cc
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 8 deletions.
9 changes: 6 additions & 3 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
'use strict'

const assert = require('assert')
const { utils } = require('libp2p-pubsub')

const PeerInfo = require('peer-info')
Expand All @@ -26,7 +25,9 @@ class GossipSub extends BasicPubsub {
* @constructor
*/
constructor (peerInfo, registrar, options = {}) {
assert(PeerInfo.isPeerInfo(peerInfo), 'peer info must be an instance of `peer-info`')
if (!PeerInfo.isPeerInfo(peerInfo)) {
throw new Error('peer info must be an instance of `peer-info`')
}

super({
debugName: 'libp2p:gossipsub',
Expand Down Expand Up @@ -325,7 +326,9 @@ class GossipSub extends BasicPubsub {
* @returns {void}
*/
join (topics) {
assert(this.started, 'GossipSub has not started')
if (!this.started) {
throw new Error('GossipSub has not started')
}
topics = utils.ensureArray(topics)

this.log('JOIN %s', topics)
Expand Down
17 changes: 12 additions & 5 deletions src/pubsub.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
'use strict'

const assert = require('assert')
const errcode = require('err-code')

const TimeCache = require('time-cache')
Expand Down Expand Up @@ -261,7 +260,9 @@ class BasicPubSub extends Pubsub {
* @returns {void}
*/
subscribe (topics) {
assert(this.started, 'Pubsub has not started')
if (!this.started) {
throw new Error('Pubsub has not started')
}

topics = utils.ensureArray(topics)

Expand Down Expand Up @@ -305,7 +306,9 @@ class BasicPubSub extends Pubsub {
* @returns {void}
*/
unsubscribe (topics) {
assert(this.started, 'Pubsub has not started')
if (!this.started) {
throw new Error('Pubsub has not started')
}

topics = utils.ensureArray(topics)

Expand Down Expand Up @@ -350,7 +353,9 @@ class BasicPubSub extends Pubsub {
* @returns {void}
*/
async publish (topics, messages) {
assert(this.started, 'Pubsub has not started')
if (!this.started) {
throw new Error('Pubsub has not started')
}

this.log('publish', topics, messages)

Expand Down Expand Up @@ -387,7 +392,9 @@ class BasicPubSub extends Pubsub {
* @returns {Array<String>}
*/
getTopics () {
assert(this.started, 'Pubsub is not started')
if (!this.started) {
throw new Error('Pubsub is not started')
}

return Array.from(this.subscriptions)
}
Expand Down

0 comments on commit e0a37cc

Please sign in to comment.