utils: github.com/juju/utils/voyeur Index | Examples | Files

package voyeur

import "github.com/juju/utils/voyeur"

Package voyeur implements a concurrency-safe value that can be watched for changes.

Index

Examples

Package Files

value.go

type Value Uses

type Value struct {
    // contains filtered or unexported fields
}

Value represents a shared value that can be watched for changes. Methods on a Value may be called concurrently. The zero Value is ok to use, and is equivalent to a NewValue result with a nil initial value.

func NewValue Uses

func NewValue(initial interface{}) *Value

NewValue creates a new Value holding the given initial value. If initial is nil, any watchers will wait until a value is set.

func (*Value) Close Uses

func (v *Value) Close() error

Close closes the Value, unblocking any outstanding watchers. Close always returns nil.

func (*Value) Closed Uses

func (v *Value) Closed() bool

Closed reports whether the value has been closed.

func (*Value) Get Uses

func (v *Value) Get() interface{}

Get returns the current value.

func (*Value) Set Uses

func (v *Value) Set(val interface{})

Set sets the shared value to val.

func (*Value) Watch Uses

func (v *Value) Watch() *Watcher

Watch returns a Watcher that can be used to watch for changes to the value.

type Watcher Uses

type Watcher struct {
    // contains filtered or unexported fields
}

Watcher represents a single watcher of a shared value.

func (*Watcher) Close Uses

func (w *Watcher) Close()

Close closes the Watcher without closing the underlying value. It may be called concurrently with Next.

func (*Watcher) Next Uses

func (w *Watcher) Next() bool

Next blocks until there is a new value to be retrieved from the value that is being watched. It also unblocks when the value or the Watcher itself is closed. Next returns false if the value or the Watcher itself have been closed.

Code:

v := NewValue(nil)

// The channel is not necessary for normal use of the watcher.
// It just makes the test output predictable.
ch := make(chan bool)

go func() {
    for x := 0; x < 3; x++ {
        v.Set(fmt.Sprintf("value%d", x))
        ch <- true
    }
    v.Close()
}()
w := v.Watch()
for w.Next() {
    fmt.Println(w.Value())
    <-ch
}

Output:

value0
value1
value2

func (*Watcher) Value Uses

func (w *Watcher) Value() interface{}

Value returns the last value that was retrieved from the watched Value by Next.

Package voyeur imports 1 packages (graph) and is imported by 160 packages. Updated 2018-03-02. Refresh now. Tools for package owners.