Set changes the underlying value, triggers callbacks, and returns whether the value has changed
(val interface{})
| 60 | |
| 61 | // Set changes the underlying value, triggers callbacks, and returns whether the value has changed |
| 62 | func (t *stateTracker) Set(val interface{}) bool { |
| 63 | t.cond.L.Lock() |
| 64 | defer t.cond.L.Unlock() |
| 65 | |
| 66 | old := t.value |
| 67 | now := time.Now() |
| 68 | |
| 69 | sinceOld := now.Sub(t.lastSet[old]) |
| 70 | sinceLastNew := time.Duration(0) |
| 71 | if lastNew, ok := t.lastSet[val]; ok { |
| 72 | sinceLastNew = now.Sub(lastNew) |
| 73 | } |
| 74 | |
| 75 | t.value = val |
| 76 | t.lastSet[val] = now |
| 77 | |
| 78 | t.cond.Broadcast() |
| 79 | |
| 80 | if len(t.listeners) > 0 { |
| 81 | n := &Notification{ |
| 82 | New: val, |
| 83 | Old: old, |
| 84 | SinceOld: sinceOld, |
| 85 | SinceLastNew: sinceLastNew, |
| 86 | } |
| 87 | for _, listener := range t.listeners { |
| 88 | listener <- n |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | return old != val |
| 93 | } |
| 94 | |
| 95 | // Wait until underlying state is the desired one, returning whether it had to wait or not. |
| 96 | func (t *stateTracker) Wait(ctx context.Context, desired interface{}) bool { |