-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathresult_core.go
58 lines (46 loc) · 1.35 KB
/
result_core.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
package query
import (
"sync/atomic"
)
type resultCore struct {
stopPropagation *uint32
handled *uint32
fresh *uint32
}
func newResultCore() resultCore {
res := resultCore{
stopPropagation: new(uint32),
handled: new(uint32),
fresh: new(uint32),
}
atomic.SwapUint32(res.fresh, 1)
return res
}
//------Core------//
// Handled explicitly marks this result as handled (so no error is thrown if no data is found)
func (res *resultCore) Handled() {
atomic.CompareAndSwapUint32(res.handled, 0, 1)
}
// Done explicitly marks this result as handled and final. Meaning it will not be propagated to other handlers.
func (res *resultCore) Done() {
res.Handled()
atomic.CompareAndSwapUint32(res.stopPropagation, 0, 1)
}
// IsFresh can be used to verify if this result is fresh.
func (res *resultCore) IsFresh() bool {
return atomic.LoadUint32(res.fresh) == 1
}
// IsCached can be used to verify if this result was retrieved from cache.
func (res *resultCore) IsCached() bool {
return atomic.LoadUint32(res.fresh) == 0
}
//------Internal------//
func (res *resultCore) propagationStopped() bool {
return atomic.LoadUint32(res.stopPropagation) == 1
}
func (res *resultCore) isHandled() bool {
return atomic.LoadUint32(res.handled) == 1
}
func (res *resultCore) loadedFromCache() {
atomic.CompareAndSwapUint32(res.fresh, 1, 0)
}