-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathopts.go
33 lines (28 loc) · 1013 Bytes
/
opts.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
package livefile
import "context"
type Opt[T any] func(s *LiveFile[T])
// WithDefault sets the function that will be called to get the default value of
// the file data.
// If not set, the default value will be the zero value of the type.
func WithDefault[T any](f func() T) Opt[T] {
return func(s *LiveFile[T]) {
s.defaultFunc = f
}
}
// WithErrorHandler sets the function that will be called when an error occurs.
// If not set, the default error handler will panic.
func WithErrorHandler[T any](f func(context.Context, error)) Opt[T] {
return func(s *LiveFile[T]) {
s.errHandler = f
}
}
// WithLoadedCallback sets the function that will be called when the file is
// reloaded from the filesystem.
// The function will be called with the context and a pointer to the new data.
// Any access to the data MUST happen inside the callback and MUST NOT be
// stored outside of it.
func WithLoadedCallback[T any](f func(context.Context, *T)) Opt[T] {
return func(s *LiveFile[T]) {
s.onLoaded = f
}
}