-
Notifications
You must be signed in to change notification settings - Fork 0
/
hook.go
35 lines (30 loc) · 1.11 KB
/
hook.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
package smartpoll
import (
"context"
"reflect"
)
type (
// Hook will be called with the values `value, ok := <-ch`, where ch
// represents the channel this hook was configured with. The context will
// be a descendent of the Scheduler.Run context, and will be cancelled
// after the hook returns.
Hook[T any] func(ctx context.Context, internal *Internal, value T, ok bool) error
// RunHook is a hook which is called on each Scheduler.Run, just prior to
// starting the main loop. The context will be a descendent of the
// Scheduler.Run context, and will be cancelled after the hook returns.
RunHook func(ctx context.Context, internal *Internal) error
)
func (x Hook[T]) call(ctx context.Context, internal *Internal, value reflect.Value, ok bool) error {
ctx, cancel := context.WithCancel(ctx)
defer cancel()
var v T
if value.IsValid() && (value.Kind() != reflect.Interface || !value.IsNil()) {
v = value.Interface().(T)
}
return x(ctx, internal, v, ok)
}
func (x RunHook) call(ctx context.Context, internal *Internal) error {
ctx, cancel := context.WithCancel(ctx)
defer cancel()
return x(ctx, internal)
}