Skip to content
This repository was archived by the owner on Mar 10, 2020. It is now read-only.

Commit 49f05d3

Browse files
author
Alan Shaw
committed
fix: reduce the number of concurrent requests in browser
In the browser we can only make 6 concurrent requests to the same origin. License: MIT Signed-off-by: Alan Shaw <alan.shaw@protocol.ai>
1 parent 4fdb6f6 commit 49f05d3

File tree

1 file changed

+17
-25
lines changed

1 file changed

+17
-25
lines changed

src/pubsub/unsubscribe.js

Lines changed: 17 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
/* eslint-env mocha */
22
'use strict'
33

4-
const eachSeries = require('async/eachSeries')
5-
const timesSeries = require('async/timesSeries')
4+
const { isBrowser, isWebWorker } = require('ipfs-utils/src/env')
65
const { getTopic } = require('./utils')
76
const { getDescribe, getIt, expect } = require('../utils/mocha')
87

@@ -33,33 +32,26 @@ module.exports = (createCommon, options) => {
3332

3433
after((done) => common.teardown(done))
3534

36-
it('should subscribe and unsubscribe 10 times', (done) => {
37-
const count = 10
35+
// Browser/worker has max ~6 open HTTP requests to the same origin
36+
const count = isBrowser || isWebWorker ? 6 : 10
37+
38+
it(`should subscribe and unsubscribe ${count} times`, async () => {
3839
const someTopic = getTopic()
40+
const handlers = Array.from(Array(count), () => msg => {})
3941

40-
timesSeries(count, (_, cb) => {
41-
const handler = (msg) => {}
42-
ipfs.pubsub.subscribe(someTopic, handler, (err) => cb(err, handler))
43-
}, (err, handlers) => {
44-
expect(err).to.not.exist()
45-
eachSeries(
46-
handlers,
47-
(handler, cb) => ipfs.pubsub.unsubscribe(someTopic, handler, cb),
48-
(err) => {
49-
expect(err).to.not.exist()
50-
// Assert unsubscribe worked
51-
ipfs.pubsub.ls((err, topics) => {
52-
expect(err).to.not.exist()
53-
expect(topics).to.eql([])
54-
done()
55-
})
56-
}
57-
)
58-
})
42+
for (let i = 0; i < count; i++) {
43+
await ipfs.pubsub.subscribe(someTopic, handlers[i])
44+
}
45+
46+
for (let i = 0; i < count; i++) {
47+
await ipfs.pubsub.unsubscribe(someTopic, handlers[i])
48+
}
49+
50+
const topics = await ipfs.pubsub.ls()
51+
expect(topics).to.eql([])
5952
})
6053

61-
it('should subscribe 10 handlers and unsunscribe once with no reference to the handlers', async () => {
62-
const count = 10
54+
it(`should subscribe ${count} handlers and unsunscribe once with no reference to the handlers`, async () => {
6355
const someTopic = getTopic()
6456
for (let i = 0; i < count; i++) {
6557
await ipfs.pubsub.subscribe(someTopic, (msg) => {})

0 commit comments

Comments
 (0)