Commit a9decd2c1b7403fe1c4122243fec563cc1214351
refactor out sort method, and add min, max options
Dominic Tarr committed on 6/30/2019, 11:01:41 AMParent: 569614847b383eac9d3dde8b590a056de60750a2
Files changed
index.js | changed |
index.js | ||
---|---|---|
@@ -305,9 +305,9 @@ | ||
305 | 305 … | }) |
306 | 306 … | |
307 | 307 … | if(opts.offline) return cb(null, near) |
308 | 308 … | |
309 | - var count = 3, found = [] | |
309 … | + var count = opts.min || 3, found = [] | |
310 | 310 … | |
311 | 311 … | function pushFound (pub, err, will) { |
312 | 312 … | found.push({ |
313 | 313 … | id: pub.id, address: pub.address, |
@@ -317,11 +317,9 @@ | ||
317 | 317 … | }) |
318 | 318 … | if(will) count -- |
319 | 319 … | //sort in order of wether they will replicate, |
320 | 320 … | //or availability |
321 | - found.sort(function (a, b) { | |
322 | - (!!b.willReplicate) - (!!a.willReplicate) || b.availability - a.availability | |
323 | - }) | |
321 … | + found = u.sort(found) | |
324 | 322 … | } |
325 | 323 … | |
326 | 324 … | pull( |
327 | 325 … | pull.values(near), |
@@ -359,10 +357,15 @@ | ||
359 | 357 … | return opts(new Error ('peer-invites: expected: options *must* be provided.')) |
360 | 358 … | |
361 | 359 … | var host_id = opts.id || sbot.id |
362 | 360 … | invites.getNearbyPubs(opts, function (err, near) { |
363 | - if(near.length == 0 && !opts.allowWithoutPubs) | |
364 | - return cb(new Error('failed to find any suitable pubs')) | |
361 … | + if(!opts.allowWithoutPubs) { | |
362 … | + near = near.filter(function (e) { | |
363 … | + return e.willReplicate | |
364 … | + }).slice(0, opts.max || 3) | |
365 … | + if(near.length == 0) | |
366 … | + return cb(new Error('failed to find any suitable pubs')) | |
367 … | + } | |
365 | 368 … | |
366 | 369 … | var seed = crypto.randomBytes(32).toString('base64') |
367 | 370 … | sbot.identities.publishAs({ |
368 | 371 … | id: host_id, |
@@ -491,6 +494,4 @@ | ||
491 | 494 … | } |
492 | 495 … | |
493 | 496 … | // I am not happy with how big this file is |
494 | 497 … | // but I can't see a really good line along which to break it up. |
495 | - | |
496 | - |
Built with git-ssb-web