/
session.go
130 lines (108 loc) · 2.22 KB
/
session.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
package evil
import (
"sync"
"time"
"github.com/chrisseto/evil/template"
"github.com/olahol/melody"
)
type SessionClaims struct {
ID string
View string
}
func (s *SessionClaims) Valid() error {
return nil
}
type Session interface {
Done() <-chan struct{}
Set(key string, value interface{})
}
type session struct {
ID string
RootView View
mu sync.Mutex
assigns map[string]interface{}
participants map[*melody.Session]bool
started bool
changed bool
broadcast func(string, interface{}) error
done <-chan struct{}
}
func newSession(
id string, rootView View, broadcast func(string, interface{}) error,
) *session {
return &session{
ID: id,
RootView: rootView,
broadcast: broadcast,
done: make(<-chan struct{}),
assigns: make(map[string]interface{}),
}
}
func (s *session) Done() <-chan struct{} {
return s.done
}
func (s *session) Set(key string, value interface{}) {
s.mu.Lock()
defer s.mu.Unlock()
// I bet a sync.condition would be great here
s.changed = true
s.assigns[key] = value
}
func (i *session) Claims() *SessionClaims {
return &SessionClaims{
ID: i.ID,
View: viewName(i.RootView),
}
}
func (s *session) start() error {
s.mu.Lock()
if s.started {
return nil
}
s.started = true
go s.doChangeLoop()
s.mu.Unlock()
return s.RootView.OnMount(s)
}
func (s *session) doChangeLoop() {
for {
select {
case <-s.done:
return
case <-time.After(time.Second):
}
if !s.changed {
continue
}
diff, err := s.RenderDiff()
if err != nil {
panic(err)
}
if err := s.broadcast("diff", diff); err != nil {
panic(err)
}
}
}
func (s *session) addParticpant(session *melody.Session) {
s.mu.Lock()
defer s.mu.Unlock()
s.participants[session] = true
}
func (s *session) removeParticpant(session *melody.Session) {
s.mu.Lock()
defer s.mu.Unlock()
delete(s.participants, session)
}
func (s *session) RenderDiff() (template.Diff, error) {
s.mu.Lock()
defer s.mu.Unlock()
diff, err := s.RootView.Template().Execute(s.assigns)
if err != nil {
// Should probably be broadcast this
// Maybe just logging
// Likely need an evil.Error marker/type/thing
return template.Diff{}, err
}
s.changed = false
return diff, nil
}