-
Notifications
You must be signed in to change notification settings - Fork 0
/
prj_spoke.go
71 lines (62 loc) · 1.58 KB
/
prj_spoke.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
package spokes
import (
"github.com/discomco/go-cart/sdk/comps"
"github.com/discomco/go-cart/sdk/schema"
"golang.org/x/net/context"
"time"
)
type (
ProjectionSpokeFtor func() IProjectionSpoke
ProjectionSpokeBuilder func() IProjectionSpoke
)
type ProjectionSpoke struct {
*Spoke
projector comps.IProjector
projections map[schema.Name]comps.IMediatorReaction
}
func (f *ProjectionSpoke) run(ctx context.Context) error {
for _, handler := range f.projections {
err := handler.Activate(ctx)
if err != nil {
return err
}
}
time.Sleep(5 * time.Second)
return f.projector.Activate(ctx)
}
func (f *ProjectionSpoke) down(ctx context.Context) {
_ = f.projector.Deactivate(ctx)
for _, handler := range f.projections {
_ = handler.Deactivate(ctx)
}
}
func (f *ProjectionSpoke) registerProjection(handler comps.IMediatorReaction) {
_, ok := f.projections[handler.GetName()]
if !ok {
f.projections[handler.GetName()] = handler
}
}
func (f *ProjectionSpoke) registerProjector(projector comps.IProjector) {
f.projector = projector
}
func (f *ProjectionSpoke) registerReactions(reactions []comps.ISpokePlugin) {
for _, plugin := range reactions {
switch plugin.(type) {
case comps.IMediatorReaction:
f.registerProjection(plugin.(comps.IMediatorReaction))
case comps.IProjector:
f.registerProjector(plugin.(comps.IProjector))
default:
continue
}
}
}
func NewPrjSpoke(
name schema.Name,
) *ProjectionSpoke {
f := &ProjectionSpoke{
projections: make(map[schema.Name]comps.IMediatorReaction),
}
f.Spoke = NewSpoke(name, f.run, f.down, f.registerReactions)
return f
}