Skip to content

Commit e0a37cc

Browse files
authoredFeb 14, 2020
fix: remove use of assert module (#65)
The polyfill is big, we can simulate it by throwing an Error and it doesn't work under React Native.
1 parent 978444d commit e0a37cc

File tree

2 files changed

+18
-8
lines changed

2 files changed

+18
-8
lines changed
 

‎src/index.js

+6-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
'use strict'
22

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

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

3132
super({
3233
debugName: 'libp2p:gossipsub',
@@ -325,7 +326,9 @@ class GossipSub extends BasicPubsub {
325326
* @returns {void}
326327
*/
327328
join (topics) {
328-
assert(this.started, 'GossipSub has not started')
329+
if (!this.started) {
330+
throw new Error('GossipSub has not started')
331+
}
329332
topics = utils.ensureArray(topics)
330333

331334
this.log('JOIN %s', topics)

‎src/pubsub.js

+12-5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
'use strict'
22

3-
const assert = require('assert')
43
const errcode = require('err-code')
54

65
const TimeCache = require('time-cache')
@@ -261,7 +260,9 @@ class BasicPubSub extends Pubsub {
261260
* @returns {void}
262261
*/
263262
subscribe (topics) {
264-
assert(this.started, 'Pubsub has not started')
263+
if (!this.started) {
264+
throw new Error('Pubsub has not started')
265+
}
265266

266267
topics = utils.ensureArray(topics)
267268

@@ -305,7 +306,9 @@ class BasicPubSub extends Pubsub {
305306
* @returns {void}
306307
*/
307308
unsubscribe (topics) {
308-
assert(this.started, 'Pubsub has not started')
309+
if (!this.started) {
310+
throw new Error('Pubsub has not started')
311+
}
309312

310313
topics = utils.ensureArray(topics)
311314

@@ -350,7 +353,9 @@ class BasicPubSub extends Pubsub {
350353
* @returns {void}
351354
*/
352355
async publish (topics, messages) {
353-
assert(this.started, 'Pubsub has not started')
356+
if (!this.started) {
357+
throw new Error('Pubsub has not started')
358+
}
354359

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

@@ -387,7 +392,9 @@ class BasicPubSub extends Pubsub {
387392
* @returns {Array<String>}
388393
*/
389394
getTopics () {
390-
assert(this.started, 'Pubsub is not started')
395+
if (!this.started) {
396+
throw new Error('Pubsub is not started')
397+
}
391398

392399
return Array.from(this.subscriptions)
393400
}

0 commit comments

Comments
 (0)
Please sign in to comment.