-
Notifications
You must be signed in to change notification settings - Fork 0
/
state.go
144 lines (114 loc) · 2.63 KB
/
state.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
package flowstate
import (
"context"
"time"
)
var _ context.Context = &StateCtx{}
type StateID string
type State struct {
ID StateID `json:"id"`
Rev int64 `json:"rev"`
Annotations map[string]string `json:"annotations"`
Labels map[string]string `json:"labels"`
CommittedAtUnixMilli int64 `json:"committed_at_unix_milli"`
Transition Transition `json:"transition"`
}
func (s *State) SetCommitedAt(at time.Time) {
s.CommittedAtUnixMilli = at.UnixMilli()
}
func (s *State) CommittedAt() time.Time {
return time.UnixMilli(s.CommittedAtUnixMilli)
}
func (s *State) CopyTo(to *State) State {
to.ID = s.ID
to.Rev = s.Rev
to.CommittedAtUnixMilli = s.CommittedAtUnixMilli
s.Transition.CopyTo(&to.Transition)
for k, v := range s.Annotations {
to.SetAnnotation(k, v)
}
for k, v := range s.Labels {
to.SetLabel(k, v)
}
return *to
}
func (s *State) CopyToCtx(to *StateCtx) *StateCtx {
if s.Rev > 0 {
s.CopyTo(&to.Committed)
}
s.CopyTo(&to.Current)
return to
}
func (s *State) SetAnnotation(name, value string) {
if s.Annotations == nil {
s.Annotations = make(map[string]string)
}
s.Annotations[name] = value
}
func (s *State) SetLabel(name, value string) {
if s.Labels == nil {
s.Labels = make(map[string]string)
}
s.Labels[name] = value
}
type StateCtx struct {
noCopy noCopy
Current State `json:"current"`
Committed State `json:"committed"`
// Transitions between committed and current states
Transitions []Transition `json:"transitions"`
sessID int64 `json:"-"`
e Engine `json:"-"`
doneCh chan struct{}
}
func (s *StateCtx) SessID() int64 {
return s.sessID
}
func (s *StateCtx) CopyTo(to *StateCtx) *StateCtx {
s.Current.CopyTo(&to.Current)
s.Committed.CopyTo(&to.Committed)
if cap(to.Transitions) >= len(s.Transitions) {
to.Transitions = to.Transitions[:len(s.Transitions)]
} else {
to.Transitions = make([]Transition, len(s.Transitions))
}
for idx := range s.Transitions {
s.Transitions[idx].CopyTo(&to.Transitions[idx])
}
return to
}
func (s *StateCtx) NewTo(id StateID, to *StateCtx) *StateCtx {
s.CopyTo(to)
to.Current.ID = id
to.Current.Rev = 0
to.Current.ID = id
to.Current.Rev = 0
return to
}
func (s *StateCtx) Deadline() (time.Time, bool) {
return time.Time{}, false
}
func (s *StateCtx) Done() <-chan struct{} {
if s.e == nil {
return nil
}
return s.doneCh
}
func (s *StateCtx) Err() error {
if s.e == nil {
return nil
}
select {
case <-s.doneCh:
return context.Canceled
default:
return nil
}
}
func (s *StateCtx) Value(key any) any {
key1, ok := key.(string)
if !ok {
return nil
}
return s.Current.Annotations[key1]
}