Closed erickskrauch closed 4 years ago
Yes, you are. It is not supported in Invoke()
. How critical is this for you? I can make a quick fix.
But ideally, I would like to merge this into a single release with #9.
This mechanism can be made better at the internal implementation level.
I think it will be ready by Sunday evening.
Ou. This is already working in the #9 =D
https://github.com/goava/di/pull/9/commits/fae7fe988a5ffa87454c46d3b5db0c3e54a5b5aa
Thank you very much for such a quick response! It's nice to see a responsible maintainer :)
I use Invoke
to initialize subscribers to dispatcher events. As a temporary solution, I began to receive a Container
that I threw into the dependency graph. But unfortunately, I now get a bug about changing the dependency graph after compiling it, which is weird.
Container creation: https://github.com/elyby/chrly/blob/di/di/di.go
Module creation (extended https://github.com/elyby/chrly/blob/di/di/dispatcher.go):
package di
import (
"github.com/goava/di"
"github.com/mono83/slf"
dispatcherModule "github.com/elyby/chrly/dispatcher"
"github.com/elyby/chrly/eventsubscribers"
"github.com/elyby/chrly/http"
"github.com/elyby/chrly/mojangtextures"
)
var dispatcher = di.Options(
di.Provide(newDispatcher,
di.As(new(http.Emitter)),
di.As(new(mojangtextures.Emitter)),
),
di.Invoke(enableEventsHandlers),
)
func newDispatcher() dispatcherModule.EventDispatcher {
return dispatcherModule.New()
}
func enableEventsHandlers(
container *di.Container,
dispatcher dispatcherModule.EventDispatcher,
logger slf.Logger,
) error {
(&eventsubscribers.Logger{Logger: logger}).ConfigureWithDispatcher(dispatcher)
var statsReporter slf.StatsReporter
err := container.Provide(&statsReporter)
if err != nil {
return err
}
if statsReporter != nil {
(&eventsubscribers.StatsReporter{StatsReporter: statsReporter}).ConfigureWithDispatcher(dispatcher)
}
return nil
}
The error I get:
di.Invoke(..) failed: /home/erickskrauch/golang/src/github.com/elyby/chrly/di/dispatcher.go:18: dependency providing restricted after container compile
My bad. I used container.Provide
instead of container.Resolve
. Everything works :)
Thank you very much for such a quick response! It's nice to see a responsible maintainer :)
No problem. The quarantine is to blame =D
Can you try the master branch with new features? I'm merging #9 for release.
Sure, I will update right now and give you feedback.
It works perfectly:
Nothing is broken, I just removed di.WithCompile()
and container.Compile()
calls and that's all migration :)
@erickskrauch I read your screenshot. You can use di.As()
for process all type with ConfigureWithDispatcher
method:
Define an interface for event subscribers:
type EventSubscriber interface {
ConfigureWithDispatcher(dispatcher EventDispatcher)
}
Provide implementation as EventSubscriber
:
di.Provide(eventsubscribers.NewLogger, di.As(new(EventSubscriber))),
di.Provide(eventsubscribers.NewStatsReporter, di.As(new(EventSubscriber))),
And process all interfaces together:
func Configure(dispatcher EventDispatcher, subscribers []EventSubscriber) {
for _, sub := range subscribers {
sub.ConfigureWithDispatcher(dispatcher)
}
}
container.Invoke(Configure)
Or modify EventDispatcher
constructor:
func NewEventDispatcher(subscribers []EventSubscriber) *EventDispatcher {
d := &EventDispatcher{}
for _, sub := range subscribers {
sub.ConfigureWithDispatcher(d)
}
return d
}
Amazing, thanks for the suggestion!
Actually, the title. I've prepared an example that shows the problem: https://play.golang.org/p/vZ2JvfmvJBB