-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhandler.go
50 lines (41 loc) · 1.44 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
package joyride
import (
"context"
"errors"
"fmt"
"reflect"
)
// Handler is designed to be embedded into another type that implements the MessageHandler interface such that the
// only method necessary for the embedding type to implement is the remaining method of HandleMessage. In essence, this
// structure becomes, in C# terminology an, "abstract class" which needs some additional behavior in order to function
// properly. In addition, because this struct is embedded, the existing methods of Handle (and more especially) the
// Run method can be overridden by the embedding struct.
type Handler struct {
runner TaskRunner
inner MessageHandler
tasks []Executable
}
func NewHandler(inner MessageHandler, runner TaskRunner, tasks ...Executable) *Handler {
return &Handler{
inner: inner,
runner: runner,
tasks: tasks,
}
}
func (this *Handler) Handle(ctx context.Context, messages ...interface{}) {
for _, message := range messages {
if !this.inner.HandleMessage(ctx, message) {
panic(fmt.Errorf("%w: [%s]", ErrUnknownType, reflect.TypeOf(message)))
}
}
this.inner.Run(ctx)
}
func (this *Handler) Add(task Executable) {
this.tasks = append(this.tasks, task)
}
func (this *Handler) Run(ctx context.Context) {
this.runner.Run(ctx, CompositeTask(this.tasks))
this.tasks = nil
}
func (this *Handler) Tasks() []Executable { return this.tasks }
var ErrUnknownType = errors.New("the handler does not understand the message type provided")