-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
value_node.go
109 lines (87 loc) · 1.77 KB
/
value_node.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
package nodes
import (
"fmt"
"github.com/EliCDavis/polyform/refutil"
)
type ValueNodeOutput[T any] struct {
Val *ValueNode[T]
}
func (sno ValueNodeOutput[T]) Value() T {
return sno.Val.Value()
}
func (sno ValueNodeOutput[T]) Node() Node {
return sno.Val
}
func (sno ValueNodeOutput[T]) Port() string {
return "Out"
}
type ValueNode[T any] struct {
VersionData
subs []Alertable
value T
}
func Value[T any](startingValue T) *ValueNode[T] {
return &ValueNode[T]{
value: startingValue,
}
}
func FuncValue[T any](f func() T) *ValueNode[T] {
return &ValueNode[T]{
value: f(),
}
}
func (in ValueNode[T]) Name() string {
switch any(in.value).(type) {
case int, string, float32, float64:
return fmt.Sprintf("%v", in.value)
default:
return "Value"
}
}
func (in *ValueNode[T]) Node() Node {
return in
}
func (in *ValueNode[T]) Port() string {
return "Out"
}
func (in *ValueNode[T]) Out() ValueNodeOutput[T] {
return ValueNodeOutput[T]{Val: in}
}
func (vn ValueNode[T]) SetInput(input string, output Output) {
panic("input can not be set")
}
func (tn ValueNode[T]) Inputs() []Input {
return []Input{}
}
func (tn *ValueNode[T]) Outputs() []Output {
return []Output{
{
Type: refutil.GetTypeWithPackage(new(T)),
NodeOutput: ValueNodeOutput[T]{Val: tn},
},
}
}
func (in ValueNode[T]) Value() T {
return in.value
}
func (in *ValueNode[T]) Set(value T) {
in.value = value
in.Increment()
in.alertSubscribers()
}
func (v *ValueNode[T]) AddSubscription(a Alertable) {
v.subs = append(v.subs, a)
}
func (v *ValueNode[T]) alertSubscribers() {
for _, a := range v.subs {
if a != nil {
a.Alert(v.version, Processed)
}
}
}
func (v *ValueNode[T]) State() NodeState {
return Processed
}
func (v *ValueNode[T]) Dependencies() []NodeDependency {
return nil
}