Rename FlagController to BroadcastFlag

This commit is contained in:
Daniel 2021-04-03 14:14:26 +02:00
parent 0d6ed34520
commit 2b07a6900e

View file

@ -6,79 +6,79 @@ import (
"github.com/tevino/abool" "github.com/tevino/abool"
) )
// FlagController is a simple system to broadcast a flag value. // BroadcastFlag is a simple system to broadcast a flag value.
type FlagController struct { type BroadcastFlag struct {
flag *abool.AtomicBool flag *abool.AtomicBool
signal chan struct{} signal chan struct{}
lock sync.Mutex lock sync.Mutex
} }
// Flag receives changes from its FlagController. // Flag receives changes from its broadcasting flag.
// A Flag must only be used in one goroutine and is not concurrency safe, // A Flag must only be used in one goroutine and is not concurrency safe,
// but fast. // but fast.
type Flag struct { type Flag struct {
flag *abool.AtomicBool flag *abool.AtomicBool
signal chan struct{} signal chan struct{}
controller *FlagController broadcaster *BroadcastFlag
} }
// NewFlagController returns a new FlagController. // NewBroadcastFlag returns a new BroadcastFlag.
// In the initial state, the flag is not set and the singal does not trigger. // In the initial state, the flag is not set and the singal does not trigger.
func NewFlagController() *FlagController { func NewBroadcastFlag() *BroadcastFlag {
return &FlagController{ return &BroadcastFlag{
flag: abool.New(), flag: abool.New(),
signal: make(chan struct{}), signal: make(chan struct{}),
lock: sync.Mutex{}, lock: sync.Mutex{},
} }
} }
// NewFlag returns a new Flag controlled by this controller. // NewFlag returns a new Flag that listens to this broadcasting flag.
// In the initial state, the flag is set and the singal triggers. // In the initial state, the flag is set and the singal triggers.
// You can call Refresh immediately to get the current state from the // You can call Refresh immediately to get the current state from the
// controller. // broadcasting flag.
func (cfc *FlagController) NewFlag() *Flag { func (bf *BroadcastFlag) NewFlag() *Flag {
newFlag := &Flag{ newFlag := &Flag{
flag: abool.NewBool(true), flag: abool.NewBool(true),
signal: make(chan struct{}), signal: make(chan struct{}),
controller: cfc, broadcaster: bf,
} }
close(newFlag.signal) close(newFlag.signal)
return newFlag return newFlag
} }
// NotifyAndReset notifies all flags of this controller and resets the // NotifyAndReset notifies all flags of this broadcasting flag and resets the
// controller state. // internal broadcast flag state.
func (cfc *FlagController) NotifyAndReset() { func (bf *BroadcastFlag) NotifyAndReset() {
cfc.lock.Lock() bf.lock.Lock()
defer cfc.lock.Unlock() defer bf.lock.Unlock()
// Notify all flags of the change. // Notify all flags of the change.
cfc.flag.Set() bf.flag.Set()
close(cfc.signal) close(bf.signal)
// Reset // Reset
cfc.flag = abool.New() bf.flag = abool.New()
cfc.signal = make(chan struct{}) bf.signal = make(chan struct{})
} }
// Signal returns a channel that waits for the flag to be set. This does not // Signal returns a channel that waits for the flag to be set. This does not
// reset the Flag itself, you'll need to call Refresh for that. // reset the Flag itself, you'll need to call Refresh for that.
func (cf *Flag) Signal() <-chan struct{} { func (f *Flag) Signal() <-chan struct{} {
return cf.signal return f.signal
} }
// IsSet returns whether the flag was set since the last Refresh. // IsSet returns whether the flag was set since the last Refresh.
// This does not reset the Flag itself, you'll need to call Refresh for that. // This does not reset the Flag itself, you'll need to call Refresh for that.
func (cf *Flag) IsSet() bool { func (f *Flag) IsSet() bool {
return cf.flag.IsSet() return f.flag.IsSet()
} }
// Refresh fetches the current state from the controller. // Refresh fetches the current state from the broadcasting flag.
func (cf *Flag) Refresh() { func (f *Flag) Refresh() {
cf.controller.lock.Lock() f.broadcaster.lock.Lock()
defer cf.controller.lock.Unlock() defer f.broadcaster.lock.Unlock()
// Copy current flag and signal from the controller. // Copy current flag and signal from the broadcasting flag.
cf.flag = cf.controller.flag f.flag = f.broadcaster.flag
cf.signal = cf.controller.signal f.signal = f.broadcaster.signal
} }