-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathvalue.go
53 lines (47 loc) · 1.4 KB
/
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
package webthing
// Value A property value.
//
// This is used for communicating between the Thing representation and the
// actual physical thing implementation.
//
// Notifies all observers when the underlying value changes through an external
// update (command to turn the light off) or if the underlying sensor reports a
// new value.
type Value struct {
lastValue interface{}
valueForwarder []func(interface{})
}
// NewValue Initialize the object.
//
// @param {*} initialValue The initial value
// @param {function?} valueForwarder The method that updates the actual value
// on the thing
func NewValue(initialValue interface{}, valueForwarder ...func(interface{})) Value {
return Value{initialValue, valueForwarder}
}
// Set a new value for this thing.
//
// @param {*} value Value to set
func (v *Value) Set(value interface{}) {
if v.valueForwarder != nil {
for _, valueForwarder := range v.valueForwarder {
valueForwarder(value)
}
}
v.NotifyOfExternalUpdate(value)
}
// Get Return the last known value from the underlying thing.
//
// @returns the value.
func (v *Value) Get() interface{} {
return v.lastValue
}
// NotifyOfExternalUpdate Notify observers of a new value.
//
// @param {*} value New value
func (v *Value) NotifyOfExternalUpdate(value interface{}) {
if value != nil && value != v.lastValue {
v.lastValue = value
//v.emit('update', value);
}
}