-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Add wasm-ext-transport #1070
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add wasm-ext-transport #1070
Conversation
@jacogr, could you give me your opinion on this JavaScript API, or if you don't have time ping someone who could do this? The API is the following:
Let me know if this looks idiomatic, or if there are changes that I should make. One remaining issue I have is that in |
Traveling a bit, however will take a peek and see if it is usable as-is for the JS client. From a “does it make sense as an API”, @amaurymartiny could also have a peek, more eyes are always better. Generally for “multiple stuff will appear” either callbacks or eventemitters are generally be used (or even iterators) |
Oh, there's probably a confusion. This API would have to be implemented in JavaScript (using WebSockets for example), and the object that implements it gets passed to the Rust code.
I must admit that I went for |
100% - yes, came though quickly ;) Understand the Promises interface, just probably need something where data is streamed - the callback approach may work best there. However, will look in detail and stop popping in and out ;) |
While this works, it's quite rare to see this pattern. A more idiomatic would be to use iterators (very similar): const iterator = listen_on(addr);
iterator.next().then(console.log); // { value: {new_addrs1, expired_addrs1, new_connections1}, done: false }
iterator.next().then(console.log); // { value: {new_addrs2, expired_addrs2, new_connections2}, done: false } An alternative (and actually more common way in JS, because iterators are still relatively new) it to use callbacks as Jaco proposed: In js it's quite easy to switch from one to the other, so imo any of these 2 patterns would work, so do the one that's the easiest for you.
In js Promises are one-shots, so
The easiest way I guess is that the Promise rejects with an |
Would it make sense for |
// scenario 1
const promise1 = connection.write(data1);
const promise2 = connection.write(data2); // promise1 hasn't finished yet, so promise2 will reject with an error "connection still writing"
// scenario 2
connection
.write(data1)
.then(() => connection.write(data2)) // Writing data2 when data1 has finished
.then(() => /* do something else when data2 has been written */) I don't think there's a need for iterators here |
I think it would be a bit awkward though if |
Could read be |
Ready for review! As for the error thing, I added a |
Fwiw, here is the prototype I had to test my code: https://gist.github.com/tomaka/45265c26f93af0246c929116150359b7 |
I've just realized that this doesn't work. For WebSockets we support dialing but not listening. Trying to listen should indicate that we don't support the address (which makes libp2p try a different transport if there is one), and not that an error happened. Any suggestion is welcome. |
This reverts commit 97eb514.
I'm now differentiate the errors based on |
Close #1004
Close #974
Instead of trying to match the API of js-libp2p, we just expose the API of rust-libp2p to the WASM outside world. A compatibility layer with js-libp2p may then be written in the future, but more like we would write an implementation that directly uses WebSockets or WebRTC for example.