watcher

package module
v0.0.0-...-2ecbbe4 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 3, 2020 License: MIT Imports: 3 Imported by: 0

README

watcher

Documentation

Index

Examples

Constants

This section is empty.

Variables

View Source
var ErrWatcherStopped = errors.New("watcher stopped")

Functions

This section is empty.

Types

type Source

type Source struct {
	sync.RWMutex
	// contains filtered or unexported fields
}

func NewSource

func NewSource() *Source
Example
source := NewSource()
w1, err := source.Watch()
if err != nil {
	panic(err)
}
w2, err := source.Watch()
if err != nil {
	panic(err)
}

if source.watchers.Len() != 2 {
	panic("watchers's len should be 2")
}

wg := sync.WaitGroup{}
wg.Add(2)
go func() {
	v, err := w1.Next()
	if err != nil {
		panic(err)
	}

	fmt.Println(v)
	wg.Done()
}()
go func() {
	if err := w2.Stop(); err != nil && err != ErrWatcherStopped {
		panic(err)
	}
	_, err := w2.Next()
	if err == nil {
		panic(err)
	}

	wg.Done()
}()

time.Sleep(time.Second)
source.Update("hello")

wg.Wait()

if source.watchers.Len() != 1 {
	panic("watchers's len should be 1")
}

if err := w1.Stop(); err != nil {
	panic(err)
}
time.Sleep(time.Second)
if source.watchers.Len() != 0 {
	panic("watchers's len should be 0")
}
Output:

hello

func (*Source) BUpdate

func (s *Source) BUpdate(v interface{})

BUpdate blocking update

func (*Source) Read

func (s *Source) Read() (interface{}, error)

func (*Source) Update

func (s *Source) Update(v interface{})

func (*Source) Watch

func (s *Source) Watch() (*Watcher, error)

type Watcher

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

func (*Watcher) Next

func (w *Watcher) Next() (interface{}, error)

func (*Watcher) Stop

func (w *Watcher) Stop() error

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL