waku-org / js-waku

JavaScript implementation of Waku v2
https://js.waku.org
Apache License 2.0
163 stars 41 forks source link

Remove needs of `waitForRemotePeer` #1240

Open fryorcraken opened 1 year ago

fryorcraken commented 1 year ago

This is a change request

Problem

Currently, a user needs to use waitForRemotePeer before they are able to use their Waku node. Whether it is to send message via Light Push, subscribe via Filter or retrieve via Store.

waitForRemotePeer hosts a complex logic which creates a number of code smells (maintainability is C).

Acceptance Criteria

Proposed Solutions

The solution would be to embed the waitForRemotePeer logic directly in the protocols. However, because it is embedded than the logic would be simpler as we would expect only one libp2p protocol codec.

For example, when calliing LightPush.push, if no peer is available, wait for a peer using libp2p events and send the message using said peer as soon as the peer connect/protocol change event is emitted.

Notes

weboko commented 1 year ago

To remove waitForRemotePeer as explicit dependency is a good idea but I think the title is a bit broader than the problem proposed and we should also consider preferring event based approach over async/await for other places, not able to name yet but this one can be an example.

fryorcraken commented 1 year ago

I think the title is a bit broader than the problem proposed

Good point, title changed.

we should also consider preferring event based approach over async/await for other places, not able to name yet

We can track one issue at a time where we can see benefit of using event based pattern.

fryorcraken commented 1 year ago

See https://github.com/waku-org/js-waku/pull/1305#issuecomment-1514086900

fryorcraken commented 1 year ago

Discussion: