Skip to content

Commit

Permalink
feat: move hooks to rootscope instead of injectorOpts
Browse files Browse the repository at this point in the history
  • Loading branch information
samber committed May 10, 2024
1 parent 2f0c70f commit 6a1325b
Show file tree
Hide file tree
Showing 5 changed files with 109 additions and 103 deletions.
4 changes: 0 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,10 +80,6 @@ This library has no dependencies except the Go std lib.
- [Examples](https://github.com/samber/do/tree/master/examples)
- [Project templates](https://do.samber.dev/examples)

## 🛩 Benchmark

// @TODO

## 🤝 Contributing

- Ping me on Twitter [@samuelberthe](https://twitter.com/samuelberthe) (DMs, mentions, whatever :))
Expand Down
12 changes: 6 additions & 6 deletions docs/docs/container/options.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,12 +64,12 @@ injector := do.New(stores.Package)
import "github.com/samber/do/v2"

injector := do.NewWithOps(&do.InjectorOpts{
HookAfterRegistration func(scope *do.Scope, serviceName string) {
// ...
},
HookAfterShutdown func(scope *do.Scope, serviceName string) {
// ...
},
HookBeforeRegistration []func(scope *do.Scope, serviceName string),
HookAfterRegistration []func(scope *do.Scope, serviceName string),
HookBeforInvocation []func(scope *do.Scope, serviceName string),
HookAfterInvocation []func(scope *do.Scope, serviceName string, err error),
HookBeforeShutdown []func(scope *do.Scope, serviceName string),
HookAfterShutdown []func(scope *do.Scope, serviceName string, err error),

Logf func(format string, args ...any) {
// ...
Expand Down
24 changes: 0 additions & 24 deletions options.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,30 +23,6 @@ type InjectorOpts struct {
StructTagKey string
}

func (o *InjectorOpts) AddBeforeRegistrationHook(hook func(*Scope, string)) {
o.HookBeforeRegistration = append(o.HookBeforeRegistration, hook)
}

func (o *InjectorOpts) AddAfterRegistrationHook(hook func(*Scope, string)) {
o.HookAfterRegistration = append(o.HookAfterRegistration, hook)
}

func (o *InjectorOpts) AddBeforeInvocationHook(hook func(*Scope, string)) {
o.HookBeforeInvocation = append(o.HookBeforeInvocation, hook)
}

func (o *InjectorOpts) AddAfterInvocationHook(hook func(*Scope, string, error)) {
o.HookAfterInvocation = append(o.HookAfterInvocation, hook)
}

func (o *InjectorOpts) AddBeforeShutdownHook(hook func(*Scope, string)) {
o.HookBeforeShutdown = append(o.HookBeforeShutdown, hook)
}

func (o *InjectorOpts) AddAfterShutdownHook(hook func(*Scope, string, error)) {
o.HookAfterShutdown = append(o.HookAfterShutdown, hook)
}

func (o *InjectorOpts) onBeforeRegistration(scope *Scope, serviceName string) {
for _, fn := range o.HookBeforeRegistration {
fn(scope, serviceName)
Expand Down
138 changes: 69 additions & 69 deletions options_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,69 +16,69 @@ func TestInjectorOpts_addHook(t *testing.T) {
hookBeforeShutdown := func(scope *Scope, serviceName string) {}
hookAfterShutdown := func(scope *Scope, serviceName string, err error) {}

opts := &InjectorOpts{}
i := New()

//
is.Len(opts.HookBeforeRegistration, 0)
is.Len(opts.HookAfterRegistration, 0)
is.Len(opts.HookBeforeInvocation, 0)
is.Len(opts.HookAfterInvocation, 0)
is.Len(opts.HookBeforeShutdown, 0)
is.Len(opts.HookAfterShutdown, 0)
is.Len(i.opts.HookBeforeRegistration, 0)
is.Len(i.opts.HookAfterRegistration, 0)
is.Len(i.opts.HookBeforeInvocation, 0)
is.Len(i.opts.HookAfterInvocation, 0)
is.Len(i.opts.HookBeforeShutdown, 0)
is.Len(i.opts.HookAfterShutdown, 0)

//
opts.AddBeforeRegistrationHook(hookBeforeRegistration)
is.Len(opts.HookBeforeRegistration, 1)
is.Len(opts.HookAfterRegistration, 0)
is.Len(opts.HookBeforeInvocation, 0)
is.Len(opts.HookAfterInvocation, 0)
is.Len(opts.HookBeforeShutdown, 0)
is.Len(opts.HookAfterShutdown, 0)
i.AddBeforeRegistrationHook(hookBeforeRegistration)
is.Len(i.opts.HookBeforeRegistration, 1)
is.Len(i.opts.HookAfterRegistration, 0)
is.Len(i.opts.HookBeforeInvocation, 0)
is.Len(i.opts.HookAfterInvocation, 0)
is.Len(i.opts.HookBeforeShutdown, 0)
is.Len(i.opts.HookAfterShutdown, 0)

//
opts.AddAfterRegistrationHook(hookAfterRegistration)
is.Len(opts.HookBeforeRegistration, 1)
is.Len(opts.HookAfterRegistration, 1)
is.Len(opts.HookBeforeInvocation, 0)
is.Len(opts.HookAfterInvocation, 0)
is.Len(opts.HookBeforeShutdown, 0)
is.Len(opts.HookAfterShutdown, 0)
i.AddAfterRegistrationHook(hookAfterRegistration)
is.Len(i.opts.HookBeforeRegistration, 1)
is.Len(i.opts.HookAfterRegistration, 1)
is.Len(i.opts.HookBeforeInvocation, 0)
is.Len(i.opts.HookAfterInvocation, 0)
is.Len(i.opts.HookBeforeShutdown, 0)
is.Len(i.opts.HookAfterShutdown, 0)

//
opts.AddBeforeInvocationHook(hookBeforeInvocation)
is.Len(opts.HookBeforeRegistration, 1)
is.Len(opts.HookAfterRegistration, 1)
is.Len(opts.HookBeforeInvocation, 1)
is.Len(opts.HookAfterInvocation, 0)
is.Len(opts.HookBeforeShutdown, 0)
is.Len(opts.HookAfterShutdown, 0)
i.AddBeforeInvocationHook(hookBeforeInvocation)
is.Len(i.opts.HookBeforeRegistration, 1)
is.Len(i.opts.HookAfterRegistration, 1)
is.Len(i.opts.HookBeforeInvocation, 1)
is.Len(i.opts.HookAfterInvocation, 0)
is.Len(i.opts.HookBeforeShutdown, 0)
is.Len(i.opts.HookAfterShutdown, 0)

//
opts.AddAfterInvocationHook(hookAfterInvocation)
is.Len(opts.HookBeforeRegistration, 1)
is.Len(opts.HookAfterRegistration, 1)
is.Len(opts.HookBeforeInvocation, 1)
is.Len(opts.HookAfterInvocation, 1)
is.Len(opts.HookBeforeShutdown, 0)
is.Len(opts.HookAfterShutdown, 0)
i.AddAfterInvocationHook(hookAfterInvocation)
is.Len(i.opts.HookBeforeRegistration, 1)
is.Len(i.opts.HookAfterRegistration, 1)
is.Len(i.opts.HookBeforeInvocation, 1)
is.Len(i.opts.HookAfterInvocation, 1)
is.Len(i.opts.HookBeforeShutdown, 0)
is.Len(i.opts.HookAfterShutdown, 0)

//
opts.AddBeforeShutdownHook(hookBeforeShutdown)
is.Len(opts.HookBeforeRegistration, 1)
is.Len(opts.HookAfterRegistration, 1)
is.Len(opts.HookBeforeInvocation, 1)
is.Len(opts.HookAfterInvocation, 1)
is.Len(opts.HookBeforeShutdown, 1)
is.Len(opts.HookAfterShutdown, 0)
i.AddBeforeShutdownHook(hookBeforeShutdown)
is.Len(i.opts.HookBeforeRegistration, 1)
is.Len(i.opts.HookAfterRegistration, 1)
is.Len(i.opts.HookBeforeInvocation, 1)
is.Len(i.opts.HookAfterInvocation, 1)
is.Len(i.opts.HookBeforeShutdown, 1)
is.Len(i.opts.HookAfterShutdown, 0)

//
opts.AddAfterShutdownHook(hookAfterShutdown)
is.Len(opts.HookBeforeRegistration, 1)
is.Len(opts.HookAfterRegistration, 1)
is.Len(opts.HookBeforeInvocation, 1)
is.Len(opts.HookAfterInvocation, 1)
is.Len(opts.HookBeforeShutdown, 1)
is.Len(opts.HookAfterShutdown, 1)
i.AddAfterShutdownHook(hookAfterShutdown)
is.Len(i.opts.HookBeforeRegistration, 1)
is.Len(i.opts.HookAfterRegistration, 1)
is.Len(i.opts.HookBeforeInvocation, 1)
is.Len(i.opts.HookAfterInvocation, 1)
is.Len(i.opts.HookBeforeShutdown, 1)
is.Len(i.opts.HookAfterShutdown, 1)
}

func TestInjectorOpts_onEvent(t *testing.T) {
Expand All @@ -93,39 +93,39 @@ func TestInjectorOpts_onEvent(t *testing.T) {
hookBeforeShutdown := func(scope *Scope, serviceName string) { result += "e" }
hookAfterShutdown := func(scope *Scope, serviceName string, err error) { result += "f" }

opts := &InjectorOpts{
i := NewWithOpts(&InjectorOpts{
HookBeforeRegistration: []func(scope *Scope, serviceName string){hookBeforeRegistration},
HookAfterRegistration: []func(scope *Scope, serviceName string){hookAfterRegistration},
HookBeforeInvocation: []func(scope *Scope, serviceName string){hookBeforeInvocation},
HookAfterInvocation: []func(scope *Scope, serviceName string, err error){hookAfterInvocation},
HookBeforeShutdown: []func(scope *Scope, serviceName string){hookBeforeShutdown},
HookAfterShutdown: []func(scope *Scope, serviceName string, err error){hookAfterShutdown},
}
})

opts.onBeforeRegistration(&Scope{id: "id", name: "name"}, "name")
opts.onAfterRegistration(&Scope{id: "id", name: "name"}, "name")
opts.onBeforeInvocation(&Scope{id: "id", name: "name"}, "name")
opts.onAfterInvocation(&Scope{id: "id", name: "name"}, "name", nil)
opts.onBeforeShutdown(&Scope{id: "id", name: "name"}, "name")
opts.onAfterShutdown(&Scope{id: "id", name: "name"}, "name", nil)
i.opts.onBeforeRegistration(&Scope{id: "id", name: "name"}, "name")
i.opts.onAfterRegistration(&Scope{id: "id", name: "name"}, "name")
i.opts.onBeforeInvocation(&Scope{id: "id", name: "name"}, "name")
i.opts.onAfterInvocation(&Scope{id: "id", name: "name"}, "name", nil)
i.opts.onBeforeShutdown(&Scope{id: "id", name: "name"}, "name")
i.opts.onAfterShutdown(&Scope{id: "id", name: "name"}, "name", nil)

is.Equal("abcdef", result)

opts.AddBeforeRegistrationHook(func(scope *Scope, serviceName string) { result += "1" })
opts.AddAfterRegistrationHook(func(scope *Scope, serviceName string) { result += "2" })
opts.AddBeforeInvocationHook(func(scope *Scope, serviceName string) { result += "3" })
opts.AddAfterInvocationHook(func(scope *Scope, serviceName string, err error) { result += "4" })
opts.AddBeforeShutdownHook(func(scope *Scope, serviceName string) { result += "5" })
opts.AddAfterShutdownHook(func(scope *Scope, serviceName string, err error) { result += "6" })
i.AddBeforeRegistrationHook(func(scope *Scope, serviceName string) { result += "1" })
i.AddAfterRegistrationHook(func(scope *Scope, serviceName string) { result += "2" })
i.AddBeforeInvocationHook(func(scope *Scope, serviceName string) { result += "3" })
i.AddAfterInvocationHook(func(scope *Scope, serviceName string, err error) { result += "4" })
i.AddBeforeShutdownHook(func(scope *Scope, serviceName string) { result += "5" })
i.AddAfterShutdownHook(func(scope *Scope, serviceName string, err error) { result += "6" })

result = ""

opts.onBeforeRegistration(&Scope{id: "id", name: "name"}, "name")
opts.onAfterRegistration(&Scope{id: "id", name: "name"}, "name")
opts.onBeforeInvocation(&Scope{id: "id", name: "name"}, "name")
opts.onAfterInvocation(&Scope{id: "id", name: "name"}, "name", nil)
opts.onBeforeShutdown(&Scope{id: "id", name: "name"}, "name")
opts.onAfterShutdown(&Scope{id: "id", name: "name"}, "name", nil)
i.opts.onBeforeRegistration(&Scope{id: "id", name: "name"}, "name")
i.opts.onAfterRegistration(&Scope{id: "id", name: "name"}, "name")
i.opts.onBeforeInvocation(&Scope{id: "id", name: "name"}, "name")
i.opts.onAfterInvocation(&Scope{id: "id", name: "name"}, "name", nil)
i.opts.onBeforeShutdown(&Scope{id: "id", name: "name"}, "name")
i.opts.onAfterShutdown(&Scope{id: "id", name: "name"}, "name", nil)

is.Equal("a1b2c3d4e5f6", result)
}
34 changes: 34 additions & 0 deletions root_scope.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,40 @@ func (s *RootScope) queueServiceHealthcheck(ctx context.Context, scope *Scope, s
})
}

/**
* RootScope stuff
*/

// AddBeforeRegistrationHook adds a hook that will be called before a service is registered.
func (s *RootScope) AddBeforeRegistrationHook(hook func(*Scope, string)) {
s.opts.HookBeforeRegistration = append(s.opts.HookBeforeRegistration, hook)
}

// AddAfterRegistrationHook adds a hook that will be called after a service is registered.
func (s *RootScope) AddAfterRegistrationHook(hook func(*Scope, string)) {
s.opts.HookAfterRegistration = append(s.opts.HookAfterRegistration, hook)
}

// AddBeforeInvocationHook adds a hook that will be called before a service is invoked.
func (s *RootScope) AddBeforeInvocationHook(hook func(*Scope, string)) {
s.opts.HookBeforeInvocation = append(s.opts.HookBeforeInvocation, hook)
}

// AddAfterInvocationHook adds a hook that will be called after a service is invoked.
func (s *RootScope) AddAfterInvocationHook(hook func(*Scope, string, error)) {
s.opts.HookAfterInvocation = append(s.opts.HookAfterInvocation, hook)
}

// AddBeforeShutdownHook adds a hook that will be called before a service is shutdown.
func (s *RootScope) AddBeforeShutdownHook(hook func(*Scope, string)) {
s.opts.HookBeforeShutdown = append(s.opts.HookBeforeShutdown, hook)
}

// AddAfterShutdownHook adds a hook that will be called after a service is shutdown.
func (s *RootScope) AddAfterShutdownHook(hook func(*Scope, string, error)) {
s.opts.HookAfterShutdown = append(s.opts.HookAfterShutdown, hook)
}

// Clone clones injector with provided services but not with invoked instances.
func (s *RootScope) Clone() *RootScope {
return s.CloneWithOpts(s.opts)
Expand Down

0 comments on commit 6a1325b

Please sign in to comment.