8 // OrderedValue is a struct that keeps track of a value in the order
9 // it is set. Each time Value() is called, it will return the most recent
10 // calls value then discard it.
12 // This is unlike Value that returns the same value once it is set.
13 type OrderedValue struct {
19 // Value returns the last value that was set, or blocks until one
21 func (w *OrderedValue) Value() interface{} {
24 // If we have a pending value already, use it
25 if w.values != nil && w.values.Len() > 0 {
26 front := w.values.Front()
27 w.values.Remove(front)
32 // No pending value, create a waiter
34 w.waiters = list.New()
38 w.waiters.PushBack(&val)
41 // Return the value once we have it
45 // SetValue sets the latest value.
46 func (w *OrderedValue) SetValue(v interface{}) {
50 // If we have a waiter, notify it
51 if w.waiters != nil && w.waiters.Len() > 0 {
52 front := w.waiters.Front()
53 w.waiters.Remove(front)
55 val := front.Value.(*Value)
60 // Add it to the list of values