-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathresult.go
90 lines (75 loc) · 1.66 KB
/
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
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
package roulette
// Result interface provides methods to get/put an interface{} value from a rule.
type Result interface {
Put(val interface{}, prevVal ...bool) bool
Get() interface{}
}
// ResultCallback holds the out channel
type ResultCallback struct {
fn func(result interface{})
}
// Put receives a value and put's it on the parser's out channel
func (q ResultCallback) Put(val interface{}, prevVal ...bool) bool {
if len(prevVal) > 0 {
if !prevVal[0] {
return false
}
}
q.fn(val)
return true
}
// Get the callback function.
func (q ResultCallback) Get() interface{} {
return q.fn
}
// NewResultCallback return a new ResultCallback
func NewResultCallback(fn func(interface{})) *ResultCallback {
return &ResultCallback{fn: fn}
}
// ResultQueue holds the out channel
type ResultQueue struct {
get chan interface{}
}
// Put receives a value and put's it on the parser's out channel
func (q ResultQueue) Put(val interface{}, prevVal ...bool) bool {
//fmt.Println(val, prevVal)
if len(prevVal) > 0 {
if !prevVal[0] {
return false
}
}
go func(val interface{}) {
q.get <- val
}(val)
return true
}
type empty struct{}
type quit struct{}
// Get ...
func (q ResultQueue) Get() interface{} {
return q.get
}
func (q ResultQueue) block() {
// nil channel blocks forever
// blocks := make(chan interface{})
block:
for {
select {
case v := <-q.get:
switch v.(type) {
case quit:
break block
default:
// is not quit, put it back
q.get <- v
}
case q.get <- empty{}:
}
}
}
// NewResultQueue returns a new ResultQueue
func NewResultQueue() ResultQueue {
q := ResultQueue{get: make(chan interface{})}
go q.block()
return q
}