Skip to content

Commit 5dd40a1

Browse files
Added Listener generic type.
1 parent 01f6094 commit 5dd40a1

File tree

2 files changed

+24
-20
lines changed

2 files changed

+24
-20
lines changed

README.md

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -25,19 +25,7 @@ type User struct {
2525
var HookUserInsert = hooks.NewHook[User]("user.insert")
2626
```
2727

28-
2) Dispatch the data to the hook _listeners_:
29-
30-
```go
31-
func (u *User) Insert() {
32-
db.Insert("INSERT INTO users ...")
33-
34-
HookUserInsert.Dispatch(&u)
35-
}
36-
```
37-
38-
Or, dispatch all listeners asynchronously with `HookUserInsert.DispatchAsync(u)`.
39-
40-
3) Listen to a hook:
28+
2Listen to a hook:
4129

4230
```go
4331
package greeter
@@ -49,6 +37,18 @@ func init() {
4937
}
5038
```
5139

40+
3) Dispatch the data to the hook _listeners_:
41+
42+
```go
43+
func (u *User) Insert() {
44+
db.Insert("INSERT INTO users ...")
45+
46+
HookUserInsert.Dispatch(&u)
47+
}
48+
```
49+
50+
Or, dispatch all listeners asynchronously with `HookUserInsert.DispatchAsync(u)`.
51+
5252
### Things to know
5353

5454
- The `Listen()` callback does not have to be an anonymous function. You can also do:
@@ -206,15 +206,16 @@ Hook listeners can also provide validation or other similar input on data that i
206206
```go
207207
type UserValidation struct {
208208
User User
209-
Errors []error
209+
Errors *[]error
210210
}
211211

212212
var HookUserValidate = hooks.NewHook[UserValidation]("user.validate")
213213

214214
func (u *User) Validate() []error {
215-
uv := &UserValidation{
216-
User: &u,
217-
Errors: make([]error, 0)
215+
errs := make([]error, 0)
216+
uv := UserValidation{
217+
User: *u,
218+
Errors: &errs,
218219
}
219220

220221
if u.Email == "" {

hook.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,16 @@ import (
44
"sync"
55
)
66

7+
// Listener is a function that can listen and react to a hook event
8+
type Listener[T any] func(event Event[T])
9+
710
// Hook is a mechanism which supports the ability to dispatch data to arbitrary listener callbacks
811
type Hook[T any] struct {
912
// name stores the name of the hook
1013
name string
1114

1215
// listeners stores the functions which will be invoked during dispatch
13-
listeners []func(event Event[T])
16+
listeners []Listener[T]
1417

1518
// mu stores the mutex to provide concurrency-safe operations
1619
mu sync.RWMutex
@@ -22,7 +25,7 @@ func NewHook[T any](name string) *Hook[T] {
2225

2326
return &Hook[T]{
2427
name: name,
25-
listeners: make([]func(event Event[T]), 0),
28+
listeners: make([]Listener[T], 0),
2629
mu: sync.RWMutex{},
2730
}
2831
}
@@ -33,7 +36,7 @@ func (h *Hook[T]) GetName() string {
3336
}
3437

3538
// Listen registers a callback function to be invoked when the hook dispatches data
36-
func (h *Hook[T]) Listen(callback func(event Event[T])) {
39+
func (h *Hook[T]) Listen(callback Listener[T]) {
3740
h.mu.Lock()
3841
defer h.mu.Unlock()
3942

0 commit comments

Comments
 (0)