-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathstates.js
62 lines (52 loc) · 1.45 KB
/
states.js
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
'use strict'
// Convenience functions for extracting non-subsystem-specific data from protobuf states
function getSubsystems(state) {
if (!state) return null
return state.getSubsystems()
}
function getLatestState(states) {
if (!states.length) return null
return states[states.length - 1]
}
const emptyTimes = {
start: 0,
end: 0,
duration: 0,
}
function getStateTime(state) {
if (!state) return null
const ts = state.getInstantTs()
return ts
}
function getPreviousStateTime(timeIndex, states) {
if (!states.length) return 0
if (states.length === 1) return getStateTime(states[0])
if (timeIndex > 0) return getStateTime(states[timeIndex - 1])
// If there's no previous time, start at one state interval before the first
const nextTime = getStateTime(states[timeIndex + 1])
const currentTime = getStateTime(states[timeIndex])
const firstStateInterval = nextTime - currentTime
return currentTime - firstStateInterval
}
function getStateRangeTimes(states) {
if (!states || !states.length) return emptyTimes
const start = getPreviousStateTime(0, states)
const end = states[states.length - 1].getInstantTs()
const duration = end - start
return {
start,
end,
duration,
}
}
function getStateIndex(states, timestamp) {
return states.findIndex(state => getStateTime(state) === timestamp)
}
module.exports = {
getLatestState,
getSubsystems,
getStateRangeTimes,
getStateTime,
getPreviousStateTime,
getStateIndex,
}