This repository has been archived by the owner on Mar 11, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 18
callbacks -> async / await #44
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
26f47f6
chore: callbacks -> async / await
dirkmc 355cfe5
test: add tests for canceling dials
dirkmc d17dea1
feat: Adapter class
dirkmc 639e84d
test: wait for listener.close() in tests
dirkmc baa6290
chore: update README for async/await
dirkmc 44501d5
chore: Reorder params in README
dirkmc 2488a89
chore: move semi-colons around
dirkmc File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
'use strict' | ||
|
||
const { Connection } = require('interface-connection') | ||
const toPull = require('async-iterator-to-pull-stream') | ||
const error = require('pull-stream/sources/error') | ||
const drain = require('pull-stream/sinks/drain') | ||
const noop = () => {} | ||
|
||
function callbackify (fn) { | ||
return async function (...args) { | ||
let cb = args.pop() | ||
if (typeof cb !== 'function') { | ||
args.push(cb) | ||
cb = noop | ||
} | ||
let res | ||
try { | ||
res = await fn(...args) | ||
} catch (err) { | ||
return cb(err) | ||
} | ||
cb(null, res) | ||
} | ||
} | ||
|
||
// Legacy adapter to old transport & connection interface | ||
class Adapter { | ||
constructor (transport) { | ||
this.transport = transport | ||
} | ||
|
||
dial (ma, options, callback) { | ||
if (typeof options === 'function') { | ||
callback = options | ||
options = {} | ||
} | ||
|
||
callback = callback || noop | ||
|
||
const conn = new Connection() | ||
|
||
this.transport.dial(ma, options) | ||
.then(socket => { | ||
conn.setInnerConn(toPull.duplex(socket)) | ||
conn.getObservedAddrs = callbackify(socket.getObservedAddrs.bind(socket)) | ||
conn.close = callbackify(socket.close.bind(socket)) | ||
callback(null, conn) | ||
}) | ||
.catch(err => { | ||
conn.setInnerConn({ sink: drain(), source: error(err) }) | ||
callback(err) | ||
}) | ||
|
||
return conn | ||
} | ||
|
||
createListener (options, handler) { | ||
if (typeof options === 'function') { | ||
handler = options | ||
options = {} | ||
} | ||
|
||
const server = this.transport.createListener(options, socket => { | ||
const conn = new Connection(toPull.duplex(socket)) | ||
conn.getObservedAddrs = callbackify(socket.getObservedAddrs.bind(socket)) | ||
handler(conn) | ||
}) | ||
|
||
const proxy = { | ||
listen: callbackify(server.listen.bind(server)), | ||
close: callbackify(server.close.bind(server)), | ||
getAddrs: callbackify(server.getAddrs.bind(server)), | ||
getObservedAddrs: callbackify(() => server.getObservedAddrs()) | ||
} | ||
|
||
return new Proxy(server, { get: (_, prop) => proxy[prop] || server[prop] }) | ||
} | ||
} | ||
|
||
module.exports = Adapter |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Perhaps this should be a follow up PR instead, but since we're doing a breaking change it might be worth making
listen
support an array of multiaddrs now https://github.com/libp2p/interface-transport/issues/41.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Makes sense... should
listen
require an array, or should it also support single multiaddr?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it makes sense for it to just support an array. In the majority of cases the switch is going to be handling the
.listen
call, so most users shouldn't notice the change.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree that we can leverage this breaking change and change the
multiaddr
param to an array. Should we do this in a separate PR, but only release a new version ofinterface-transport
once that is done?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@dirkmc I'm holding on the release until we've added the listening array support. Are you working on that? If not I can start it, just avoiding the potential duplicate effort :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@jacobheun please go ahead - I have some other things going on at the moment so only able to snatch a bit of time here and there to work