-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathstream.go
More file actions
50 lines (39 loc) · 684 Bytes
/
stream.go
File metadata and controls
50 lines (39 loc) · 684 Bytes
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
package pulse
import "sync"
type streamState int
const (
idle streamState = iota
running
paused
closed
serverLost
)
type stateMachine struct {
state streamState
lock *sync.RWMutex
}
func newStateMachine() *stateMachine {
return &stateMachine{
state: idle,
lock: &sync.RWMutex{},
}
}
func (s *stateMachine) set(state streamState) {
s.lock.Lock()
defer s.lock.Unlock()
s.state = state
}
func (s *stateMachine) get() streamState {
s.lock.RLock()
defer s.lock.RUnlock()
return s.state
}
func (s *stateMachine) is(states ...streamState) bool {
current := s.get()
for _, state := range states {
if current == state {
return true
}
}
return false
}