-
Notifications
You must be signed in to change notification settings - Fork 0
/
runner.go
66 lines (53 loc) · 1.4 KB
/
runner.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 joyride
import "context"
type Runner struct {
reader StorageReader
writer StorageWriter
dispatcher MessageDispatcher
}
func NewRunner(options ...RunnerOption) Runner {
runner := Runner{reader: nop{}, writer: nop{}, dispatcher: nop{}}
for _, option := range options {
option(&runner)
}
return runner
}
func (this Runner) Run(ctx context.Context, task Executable) {
if task == nil {
return
}
this.performRequiredReads(ctx, task)
result := task.Execute(ctx)
if result == nil {
return
}
this.performWrites(ctx, result)
this.performDispatches(ctx, result)
this.Run(ctx, result.SubsequentTask())
}
func (this Runner) performRequiredReads(ctx context.Context, task Executable) {
reader, ok := task.(RequiredReads)
if !ok {
return
}
reads := reader.RequiredReads()
if len(reads) > 0 {
this.reader.Read(ctx, reads...)
}
}
func (this Runner) performWrites(ctx context.Context, result TaskResult) {
writes := result.PendingWrites()
if len(writes) > 0 {
this.writer.Write(ctx, writes...)
}
}
func (this Runner) performDispatches(ctx context.Context, result TaskResult) {
messages := result.PendingMessages()
if len(messages) > 0 {
this.dispatcher.Dispatch(ctx, messages...)
}
}
type nop struct{}
func (nop) Dispatch(_ context.Context, _ ...interface{}) {}
func (nop) Read(_ context.Context, _ ...interface{}) {}
func (nop) Write(_ context.Context, _ ...interface{}) {}