-
Notifications
You must be signed in to change notification settings - Fork 0
/
options.go
66 lines (55 loc) · 1.52 KB
/
options.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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package emitter
import (
"crypto/rand"
"encoding/hex"
"fmt"
"time"
)
// EmitterOption defines a function type for Emitter configuration options.
type EmitterOption func(Emitter)
var DefaultErrorHandler = func(event Event, err error) error {
return err
}
// DefaultIDGenerator generates a unique identifier using a combination of the current time
// and random bytes, encoded in hexadecimal.
var DefaultIDGenerator = func() string {
timestamp := time.Now().UnixNano()
randomBytes := make([]byte, 16) // 128 bits
_, err := rand.Read(randomBytes)
if err != nil {
panic(err)
}
return hex.EncodeToString(randomBytes) + fmt.Sprintf("%x", timestamp)
}
var DefaultPanicHandler = func(p interface{}) {
fmt.Printf("Panic occurred: %v\n", p)
}
// WithErrorHandler sets a custom error handler for an Emitter.
func WithErrorHandler(errHandler func(Event, error) error) EmitterOption {
return func(m Emitter) {
m.SetErrorHandler(errHandler)
}
}
// WithIDGenerator sets a custom ID generator for an Emitter.
func WithIDGenerator(idGen func() string) EmitterOption {
return func(m Emitter) {
m.SetIDGenerator(idGen)
}
}
// WithPool sets a custom pool for an Emitter.
func WithPool(pool Pool) EmitterOption {
return func(m Emitter) {
m.SetPool(pool)
}
}
type PanicHandler func(interface{})
func WithPanicHandler(panicHandler PanicHandler) EmitterOption {
return func(m Emitter) {
m.SetPanicHandler(panicHandler)
}
}
func WithErrChanBufferSize(size int) EmitterOption {
return func(m Emitter) {
m.SetErrChanBufferSize(size)
}
}