-
Notifications
You must be signed in to change notification settings - Fork 20
/
history.go
40 lines (34 loc) · 1.17 KB
/
history.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
package flows
// SessionHistory provides information about the sessions that caused this session
type SessionHistory struct {
ParentUUID SessionUUID `json:"parent_uuid"`
Ancestors int `json:"ancestors"`
AncestorsSinceInput int `json:"ancestors_since_input"`
}
// Advance moves history forward to a new parent
func (h *SessionHistory) Advance(newParent SessionUUID, receivedInput bool) *SessionHistory {
ancestorsSinceinput := 0
if !receivedInput {
ancestorsSinceinput = h.AncestorsSinceInput + 1
}
return &SessionHistory{
ParentUUID: newParent,
Ancestors: h.Ancestors + 1,
AncestorsSinceInput: ancestorsSinceinput,
}
}
// EmptyHistory is used for a session which has no history
var EmptyHistory = &SessionHistory{}
// NewChildHistory creates a new history for a child of the given session
func NewChildHistory(parent Session) *SessionHistory {
return parent.History().Advance(parent.UUID(), sessionReceivedInput(parent))
}
// looks through a session's events to see if it received input
func sessionReceivedInput(s Session) bool {
for _, r := range s.Runs() {
if r.ReceivedInput() {
return true
}
}
return false
}