generated from dogmatiq/template-go
/
handler.go
72 lines (62 loc) · 1.98 KB
/
handler.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
67
68
69
70
71
72
package fixtures
import (
"context"
"time"
"github.com/dogmatiq/dogma"
bolt "go.etcd.io/bbolt"
)
// MessageHandler is a test implementation of boltdb.MessageHandler.
type MessageHandler struct {
ConfigureFunc func(c dogma.ProjectionConfigurer)
HandleEventFunc func(context.Context, *bolt.Tx, dogma.ProjectionEventScope, dogma.Message) error
TimeoutHintFunc func(m dogma.Message) time.Duration
CompactFunc func(context.Context, *bolt.DB, dogma.ProjectionCompactScope) error
}
// Configure configures the behavior of the engine as it relates to this
// handler.
//
// c provides access to the various configuration options, such as specifying
// which types of event messages are routed to this handler.
//
// If h.ConfigureFunc is non-nil, it calls h.ConfigureFunc(c).
func (h *MessageHandler) Configure(c dogma.ProjectionConfigurer) {
if h.ConfigureFunc != nil {
h.ConfigureFunc(c)
}
}
// HandleEvent handles a domain event message that has been routed to this
// handler.
//
// If h.HandleEventFunc is non-nil it returns h.HandleEventFunc(ctx, tx, s, m).
func (h *MessageHandler) HandleEvent(
ctx context.Context,
tx *bolt.Tx,
s dogma.ProjectionEventScope,
m dogma.Message,
) error {
if h.HandleEventFunc != nil {
return h.HandleEventFunc(ctx, tx, s, m)
}
return nil
}
// TimeoutHint returns a duration that is suitable for computing a deadline for
// the handling of the given message by this handler.
//
// If h.TimeoutHintFunc is non-nil it returns h.TimeoutHintFunc(m), otherwise it
// returns 0.
func (h *MessageHandler) TimeoutHint(m dogma.Message) time.Duration {
if h.TimeoutHintFunc != nil {
return h.TimeoutHintFunc(m)
}
return 0
}
// Compact reduces the size of the projection's data.
//
// If h.CompactFunc is non-nil it returns h.CompactFunc(ctx,db,s), otherwise it
// returns nil.
func (h *MessageHandler) Compact(ctx context.Context, db *bolt.DB, s dogma.ProjectionCompactScope) error {
if h.CompactFunc != nil {
return h.CompactFunc(ctx, db, s)
}
return nil
}