-
Notifications
You must be signed in to change notification settings - Fork 1
/
watch.go
70 lines (56 loc) · 1.39 KB
/
watch.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
package incr
import (
"context"
"fmt"
)
// Watch returns a new watch incremental that tracks
// values for a given incremental each time it stabilizes.
func Watch[A any](scope Scope, i Incr[A]) WatchIncr[A] {
return WithinScope(scope, &watchIncr[A]{
n: NewNode("watch"),
incr: i,
})
}
// WatchIncr is a type that implements the watch interface.
type WatchIncr[A any] interface {
Incr[A]
// Reset empties the tracked values.
Reset()
// Values returns the input incremental values the [Watch] node
// has seen through stabilization passes. This array of values will
// continue to grow until you call [Reset] on the node.
Values() []A
}
var (
_ Incr[string] = (*watchIncr[string])(nil)
_ INode = (*watchIncr[string])(nil)
_ IStabilize = (*watchIncr[string])(nil)
_ fmt.Stringer = (*watchIncr[string])(nil)
)
type watchIncr[A any] struct {
n *Node
incr Incr[A]
value A
values []A
}
func (w *watchIncr[A]) Parents() []INode {
return []INode{w.incr}
}
func (w *watchIncr[A]) Value() A {
return w.value
}
func (w *watchIncr[A]) Stabilize(ctx context.Context) error {
w.value = w.incr.Value()
w.values = append(w.values, w.value)
return nil
}
func (w *watchIncr[A]) Reset() {
w.values = nil
}
func (w *watchIncr[A]) Values() []A {
return w.values
}
func (w *watchIncr[A]) Node() *Node {
return w.n
}
func (w *watchIncr[A]) String() string { return w.n.String() }