-
Notifications
You must be signed in to change notification settings - Fork 1
/
tab_mgmt.go
223 lines (187 loc) · 4.39 KB
/
tab_mgmt.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
package main
import (
"log"
"sync"
)
type tabWithContext struct {
tabContext
tab tab
}
type tabContext struct {
servConn *serverConnection
servState *serverState
chanState *channelState
pmState *privmsgState
}
type finderFunc func(*tabWithContext) bool
type tabRequestCreate struct {
ctx *tabContext
index int
finder finderFunc
ret chan *tabWithContext
}
type tabRequestCount struct {
ret chan int
}
type tabRequestSearch struct {
finder finderFunc
ret chan []*tabWithContext
}
type tabRequestDelete struct {
tabs []*tabWithContext
ret chan struct{}
}
type tabManager struct {
tabs []*tabWithContext
create chan *tabRequestCreate
count chan *tabRequestCount
search chan *tabRequestSearch
delete chan *tabRequestDelete
destroy chan struct{}
}
func (tabMan *tabManager) Shutdown() {
close(tabMan.destroy)
}
func (tabMan *tabManager) Create(ctx *tabContext, index int) *tabWithContext {
ret := make(chan *tabWithContext)
go func() {
tabMan.create <- &tabRequestCreate{ctx, index, nil, ret}
}()
return <-ret
}
func (tabMan *tabManager) CreateIfNotFound(ctx *tabContext, index int, finder finderFunc) *tabWithContext {
ret := make(chan *tabWithContext)
tabMan.create <- &tabRequestCreate{ctx, index, finder, ret}
return <-ret
}
func (tabMan *tabManager) Len() int {
ret := make(chan int)
tabMan.count <- &tabRequestCount{ret}
return <-ret
}
func (tabMan *tabManager) Find(finder finderFunc) *tabWithContext {
ret := tabMan.FindAll(finder)
if len(ret) > 0 {
return ret[0]
}
return nil
}
func (tabMan *tabManager) FindAll(finder finderFunc) []*tabWithContext {
if len(tabMan.tabs) == 0 {
return nil
}
ret := make(chan []*tabWithContext)
tabMan.search <- &tabRequestSearch{finder, ret}
return <-ret
}
// finder funcs
// usage: tabMan.Find(currentTabFinder)
//
// tabMan.Find(serverTabFinder(servState))
// tabMan.Find(channelTabFinder(chanState))
// tabMan.Find(someotherTabFinder) ...
func allTabsFinder(t *tabWithContext) bool {
return true
}
func currentTabFinder(t *tabWithContext) bool {
return t.tab != nil && t.tab.Index() == tabWidget.CurrentIndex()
}
func allServerTabsFinder(servState *serverState) finderFunc {
return func(t *tabWithContext) bool {
if t.servState == servState {
return true
}
return false
}
}
func currentServerTabFinder(servState *serverState) finderFunc {
return func(t *tabWithContext) bool {
if currentTabFinder(t) && t.servState == servState {
return true
}
if t.servState == servState && t.chanState == nil && t.pmState == nil {
return true
}
return false
}
}
func identityFinder(me tab) finderFunc {
return func(t *tabWithContext) bool {
if t.tab == me {
return true
}
return false
}
}
func (tabMan *tabManager) Delete(tabs ...*tabWithContext) {
ret := make(chan struct{})
tabMan.delete <- &tabRequestDelete{tabs, ret}
<-ret
return
}
var t_mu sync.Mutex
func newTabManager() *tabManager {
tabMan := &tabManager{
tabs: []*tabWithContext{},
create: make(chan *tabRequestCreate),
count: make(chan *tabRequestCount),
search: make(chan *tabRequestSearch),
delete: make(chan *tabRequestDelete),
destroy: make(chan struct{}),
}
go func() {
for {
here:
select {
case <-tabMan.destroy:
return
case req := <-tabMan.create:
t_mu.Lock()
if req.finder != nil {
for _, t := range tabMan.tabs {
if req.finder(t) {
t_mu.Unlock()
req.ret <- t
log.Println("found ctx:", t)
break here
}
}
}
t := &tabWithContext{}
t.servConn = req.ctx.servConn
t.servState = req.ctx.servState
t.chanState = req.ctx.chanState
t.pmState = req.ctx.pmState
tabMan.tabs = append(tabMan.tabs, t)
log.Println("created ctx:", t)
t_mu.Unlock()
req.ret <- t
case req := <-tabMan.count:
req.ret <- len(tabMan.tabs)
case req := <-tabMan.search:
ret := []*tabWithContext{}
for _, t := range tabMan.tabs {
if t.tab != nil && req.finder(t) {
ret = append(ret, t)
}
}
req.ret <- ret
case req := <-tabMan.delete:
indices := []int{}
for _, t := range req.tabs {
indices = append(indices, t.tab.Index())
}
for _, index := range indices {
for i, t := range tabMan.tabs {
if t.tab.Index() == index {
tabMan.tabs = append(tabMan.tabs[0:i], tabMan.tabs[i+1:]...)
}
}
}
req.ret <- struct{}{}
// t.Close()
}
}
}()
return tabMan
}