Skip to content
This repository has been archived by the owner on Jan 7, 2022. It is now read-only.

opts.autoListen (?) - some way to make listen async #1

Open
joehand opened this issue Jan 20, 2017 · 0 comments
Open

opts.autoListen (?) - some way to make listen async #1

joehand opened this issue Jan 20, 2017 · 0 comments

Comments

@joehand
Copy link
Collaborator

joehand commented Jan 20, 2017

I want to store the port option in a database for dat-node so we can reuse whatever port worked last. But I think we need some way to make listen & join async. Maybe opts.autoListen or something (default: true), would be a way to do this? Or any other more straightforward suggestions?

Sudo code implementation in dat-node:

// Check for existing port saved
// PROBLEM: Happens async
if (opts.port && db) {
    var subDb = sub(db, `${encoding.toStr(archive.discoveryKey)}-network`)
    subDb.get('port', function (err, val) {
      if (err && !err.notFound) throw new Error(err)
      if (val) opts.port = val
      doListen()
    })
}

// Happens sync
opts.autoListen = false
var swarm = hyperdiscovery(archive, opts)


// ... later with `autoListen = false`

function doListen () {
  swarm.once('error', function () {
    swarm.listen(0)
  })
  swarm.listen(opts.port)
  swarm.join(this.archive.discoveryKey)
  swarm.once('listening', function () {
    subDb.put('port', swarm.port) // save whatever port succeeded
  })
}

We could make the whole network function async in dat-node too. Maybe that is the easier solution but the rest of the networking APIs are sync.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant