|
1 | 1 | 'use strict'
|
2 | 2 |
|
3 |
| -const race = require('async/race') |
| 3 | +const tryEach = require('async/tryEach') |
4 | 4 | const debug = require('debug')
|
5 |
| -const once = require('once') |
6 | 5 |
|
7 | 6 | const log = debug('libp2p:switch:dialer')
|
8 | 7 |
|
9 | 8 | const DialQueue = require('./queue')
|
10 |
| -const { CONNECTION_FAILED } = require('../errors') |
11 | 9 |
|
12 | 10 | /**
|
13 | 11 | * Track dials per peer and limited them.
|
@@ -39,29 +37,26 @@ class LimitDialer {
|
39 | 37 | log('dialMany:start')
|
40 | 38 | // we use a token to track if we want to cancel following dials
|
41 | 39 | const token = { cancel: false }
|
42 |
| - callback = once(callback) // only call callback once |
43 | 40 |
|
44 | 41 | let errors = []
|
45 | 42 | const tasks = addrs.map((m) => {
|
46 |
| - return (cb) => this.dialSingle(peer, transport, m, token, (err, res) => { |
47 |
| - if (res) return cb(null, res) |
48 |
| - |
49 |
| - errors.push(err || CONNECTION_FAILED()) |
50 |
| - |
51 |
| - if (errors.length === tasks.length) { |
52 |
| - cb(errors) |
| 43 | + return (cb) => this.dialSingle(peer, transport, m, token, (err, result) => { |
| 44 | + if (err) { |
| 45 | + errors.push(err) |
| 46 | + return cb(err) |
53 | 47 | }
|
| 48 | + return cb(null, result) |
54 | 49 | })
|
55 | 50 | })
|
56 | 51 |
|
57 |
| - race(tasks, (_, successfulDial) => { |
58 |
| - if (successfulDial) { |
| 52 | + tryEach(tasks, (_, result) => { |
| 53 | + if (result && result.conn) { |
59 | 54 | log('dialMany:success')
|
60 |
| - return callback(null, successfulDial) |
| 55 | + return callback(null, result) |
61 | 56 | }
|
62 | 57 |
|
63 | 58 | log('dialMany:error')
|
64 |
| - return callback(errors) |
| 59 | + callback(errors) |
65 | 60 | })
|
66 | 61 | }
|
67 | 62 |
|
|
0 commit comments