-
Notifications
You must be signed in to change notification settings - Fork 1
/
freeze.go
52 lines (43 loc) · 1.08 KB
/
freeze.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
package incr
import (
"context"
"fmt"
)
// Freeze yields an incremental that takes the value of an
// input incremental on the first stabilization and
// doesn't change thereafter.
//
// Stabilization propagates through this node even
// after the first stabilization.
func Freeze[A any](scope Scope, i Incr[A]) Incr[A] {
return WithinScope(scope, &freezeIncr[A]{
n: NewNode("freeze"),
i: i,
})
}
var (
_ Incr[string] = (*freezeIncr[string])(nil)
_ IStabilize = (*freezeIncr[string])(nil)
_ INode = (*freezeIncr[string])(nil)
_ fmt.Stringer = (*freezeIncr[string])(nil)
)
type freezeIncr[A any] struct {
n *Node
i Incr[A]
freezeAt uint64
v A
}
func (f *freezeIncr[T]) Parents() []INode {
return []INode{f.i}
}
func (f *freezeIncr[T]) Node() *Node { return f.n }
func (f *freezeIncr[T]) Value() T { return f.v }
func (f *freezeIncr[T]) String() string { return f.n.String() }
func (f *freezeIncr[A]) Stabilize(_ context.Context) error {
if f.freezeAt > 0 {
return nil
}
f.freezeAt = GraphForNode(f).stabilizationNum
f.v = f.i.Value()
return nil
}