forked from nytlabs/st-core
/
store_value.go
76 lines (67 loc) · 1.2 KB
/
store_value.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
package core
import "sync"
func ValueStore() SourceSpec {
return SourceSpec{
Name: "value",
Type: VALUE_PRIMITIVE,
New: NewValue,
}
}
type Value struct {
value interface{}
quit chan bool
sync.Mutex
}
func NewValue() Source {
return &Value{
value: nil,
quit: make(chan bool),
}
}
func (v Value) GetType() SourceType {
return VALUE_PRIMITIVE
}
func (v *Value) Get() interface{} {
return v.value
}
func (v *Value) Set(nv interface{}) error {
v.value = nv
return nil
}
// ValueGet emits the value stored
func ValueGet() Spec {
return Spec{
Name: "valueGet",
Inputs: []Pin{
Pin{"trigger", ANY},
},
Outputs: []Pin{
Pin{"value", ANY},
},
Source: VALUE_PRIMITIVE,
Kernel: func(in, out, internal MessageMap, s Source, i chan Interrupt) Interrupt {
v := s.(*Value)
out[0] = v.value
return nil
},
}
}
// ValueSet sets the value stored
func ValueSet() Spec {
return Spec{
Name: "valueSet",
Inputs: []Pin{
Pin{"value", ANY},
},
Outputs: []Pin{
Pin{"out", ANY},
},
Source: VALUE_PRIMITIVE,
Kernel: func(in, out, internal MessageMap, s Source, i chan Interrupt) Interrupt {
v := s.(*Value)
v.value = in[0]
out[0] = true
return nil
},
}
}