-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathiterator_result.go
56 lines (46 loc) · 1.08 KB
/
iterator_result.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
package query
import "time"
// IteratorResult is the struct returned from iterator queries.
type IteratorResult struct {
resultCore
proxy chan interface{}
listening chan bool
}
func newIteratorResult(buffer int) *IteratorResult {
return &IteratorResult{
resultCore: newResultCore(),
proxy: make(chan interface{}, buffer),
listening: make(chan bool, 1),
}
}
//------Provide Data------//
// Yield is used to provide values while they are being processed
func (res *IteratorResult) Yield(data interface{}) {
res.Handled()
res.proxy <- data
}
//------Fetch Data------//
// Iterate is used to process the values that are being yielded
func (res *IteratorResult) Iterate() <-chan interface{} {
res.listening <- true
return res.proxy
}
//------Internal------//
func (res *IteratorResult) waitListener(timeout time.Duration) bool {
select {
case <-res.listening:
return true
default:
t := time.NewTimer(timeout)
select {
case <-res.listening:
t.Stop()
return true
case <-t.C:
return false
}
}
}
func (res *IteratorResult) close() {
close(res.proxy)
}