go-libp2p icon indicating copy to clipboard operation
go-libp2p copied to clipboard

feat: Add WithFxOption

Open MarcoPolo opened this issue 1 year ago • 2 comments

While doing some refactoring of the p2p-forge client, I found that I wanted a way to get access to parts of the host earlier than .New() returning. I also wanted an option I could provide the constructor that would give me the host (or peer.ID, or something else) without having to have the user explicitly pass it to me.

In other words I think it's nicer to do this:

// someServiceThatDependsOnHostAndConfiguresHost
service := NewServiceThatDependsOnHostAndConfiguresHost()
h, err := New(service.Libp2pOptions())

Rather than

// someServiceThatDependsOnHostAndConfiguresHost
service := NewServiceThatDependsOnHostAndConfiguresHost()
h, err := New(service.Libp2pOptions())
service.ProvideHost(h)

And this change would enable the former (along with many other things as well).

The biggest downside here is that fx is really powerful and this could cause some confusing errors to users.

Another concern is that it ties us to fx as long as we support this option. But I'm not worried about this for two reasons:

  1. Fx is pretty nice, and as long as it doesn't break, I don't see a reason to move away from it.
  2. I've marked it as experimental so we can feel less bad about break this.

MarcoPolo avatar Sep 10 '24 02:09 MarcoPolo