-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathopts.go
More file actions
41 lines (35 loc) · 1.21 KB
/
Copy pathopts.go
File metadata and controls
41 lines (35 loc) · 1.21 KB
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
36
37
38
39
40
41
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
}
}
// WithFileSystem sets the filesystem to use for file operations.
// If not set, the default osFileSystem will be used.
func WithFileSystem[T any](fsys WriteFS) Opt[T] {
return func(s *LiveFile[T]) {
s.fs = fsys
}
}