-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbridge.go
More file actions
158 lines (135 loc) · 3.69 KB
/
bridge.go
File metadata and controls
158 lines (135 loc) · 3.69 KB
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
package main
import (
"sync"
"time"
)
// BridgeState manages application state and synchronization between
// the HTTP server goroutines and the main stdin loop.
type BridgeState struct {
mu sync.Mutex
// Cached unread RSS data
unreadItems []interface{}
timestamp float64
dataCh chan struct{} // signaled when unread data is updated
// Pending mark-as-read requests: itemId -> result channel
markReadResults map[string]chan bool
// Pending single-item requests: itemId -> result channel
singleItemResults map[string]chan interface{}
// Pending folder requests: folderPath -> result channel
folderResults map[string]chan []interface{}
}
// NewBridgeState creates a new BridgeState.
func NewBridgeState() *BridgeState {
return &BridgeState{
dataCh: make(chan struct{}, 1),
markReadResults: make(map[string]chan bool),
singleItemResults: make(map[string]chan interface{}),
folderResults: make(map[string]chan []interface{}),
}
}
// UpdateRSSData updates the cached unread items and signals waiters.
func (s *BridgeState) UpdateRSSData(data []interface{}) {
s.mu.Lock()
s.unreadItems = data
s.timestamp = float64(time.Now().Unix())
s.mu.Unlock()
// Non-blocking signal
select {
case s.dataCh <- struct{}{}:
default:
}
}
// GetRSSData returns the currently cached unread items.
func (s *BridgeState) GetRSSData() []interface{} {
s.mu.Lock()
defer s.mu.Unlock()
return s.unreadItems
}
// DrainDataCh drains the data channel so the next wait is fresh.
func (s *BridgeState) DrainDataCh() {
select {
case <-s.dataCh:
default:
}
}
// --- Mark Read ---
// RegisterMarkRead creates a channel for a mark-read request.
func (s *BridgeState) RegisterMarkRead(itemID string) chan bool {
ch := make(chan bool, 1)
s.mu.Lock()
s.markReadResults[itemID] = ch
s.mu.Unlock()
return ch
}
// CompleteMarkRead delivers the mark-read result.
func (s *BridgeState) CompleteMarkRead(itemID string, success bool) {
s.mu.Lock()
ch, ok := s.markReadResults[itemID]
if ok {
delete(s.markReadResults, itemID)
}
s.mu.Unlock()
if ok {
ch <- success
}
}
// CleanupMarkRead removes a pending mark-read request (on timeout).
func (s *BridgeState) CleanupMarkRead(itemID string) {
s.mu.Lock()
delete(s.markReadResults, itemID)
s.mu.Unlock()
}
// --- Single Item ---
// RegisterSingleItem creates a channel for a single-item request.
func (s *BridgeState) RegisterSingleItem(itemID string) chan interface{} {
ch := make(chan interface{}, 1)
s.mu.Lock()
s.singleItemResults[itemID] = ch
s.mu.Unlock()
return ch
}
// CompleteSingleItem delivers the single-item result.
func (s *BridgeState) CompleteSingleItem(itemID string, data interface{}) {
s.mu.Lock()
ch, ok := s.singleItemResults[itemID]
if ok {
delete(s.singleItemResults, itemID)
}
s.mu.Unlock()
if ok {
ch <- data
}
}
// CleanupSingleItem removes a pending single-item request (on timeout).
func (s *BridgeState) CleanupSingleItem(itemID string) {
s.mu.Lock()
delete(s.singleItemResults, itemID)
s.mu.Unlock()
}
// --- Folder ---
// RegisterFolder creates a channel for a folder request.
func (s *BridgeState) RegisterFolder(folderPath string) chan []interface{} {
ch := make(chan []interface{}, 1)
s.mu.Lock()
s.folderResults[folderPath] = ch
s.mu.Unlock()
return ch
}
// CompleteFolder delivers the folder result.
func (s *BridgeState) CompleteFolder(folderPath string, data []interface{}) {
s.mu.Lock()
ch, ok := s.folderResults[folderPath]
if ok {
delete(s.folderResults, folderPath)
}
s.mu.Unlock()
if ok {
ch <- data
}
}
// CleanupFolder removes a pending folder request (on timeout).
func (s *BridgeState) CleanupFolder(folderPath string) {
s.mu.Lock()
delete(s.folderResults, folderPath)
s.mu.Unlock()
}