stop all samplers on pause

This commit is contained in:
sqshq 2019-06-07 01:39:11 -04:00
parent ac75e7e891
commit 72665f55a4
4 changed files with 43 additions and 19 deletions

View File

@ -9,8 +9,8 @@ import (
) )
const ( const (
bindingsIndent = 3
pauseText = " P A U S E D " pauseText = " P A U S E D "
bindingsIndent = 3
) )
type StatusBar struct { type StatusBar struct {

View File

@ -12,26 +12,30 @@ type Sampler struct {
triggers []*Trigger triggers []*Trigger
triggersChannel chan *Sample triggersChannel chan *Sample
variables []string variables []string
pause bool
} }
func NewSampler(consumer *Consumer, items []*Item, triggers []*Trigger, options config.Options, fileVariables map[string]string, rateMs int) Sampler { func NewSampler(consumer *Consumer, items []*Item, triggers []*Trigger, options config.Options, fileVariables map[string]string, rateMs int) *Sampler {
ticker := time.NewTicker(time.Duration(rateMs * int(time.Millisecond))) ticker := time.NewTicker(time.Duration(rateMs * int(time.Millisecond)))
sampler := Sampler{ sampler := &Sampler{
consumer, consumer,
items, items,
triggers, triggers,
make(chan *Sample), make(chan *Sample),
mergeVariables(fileVariables, options.Environment), mergeVariables(fileVariables, options.Environment),
false,
} }
go func() { go func() {
for ; true; <-ticker.C { for ; true; <-ticker.C {
for _, item := range sampler.items { for _, item := range sampler.items {
if !sampler.pause {
go sampler.sample(item, options) go sampler.sample(item, options)
} }
} }
}
}() }()
go func() { go func() {
@ -39,10 +43,12 @@ func NewSampler(consumer *Consumer, items []*Item, triggers []*Trigger, options
select { select {
case sample := <-sampler.triggersChannel: case sample := <-sampler.triggersChannel:
for _, t := range sampler.triggers { for _, t := range sampler.triggers {
if !sampler.pause {
t.Execute(sample) t.Execute(sample)
} }
} }
} }
}
}() }()
return sampler return sampler
@ -77,3 +83,7 @@ func mergeVariables(fileVariables map[string]string, optionsVariables []string)
return result return result
} }
func (s *Sampler) Pause(pause bool) {
s.pause = pause
}

View File

@ -5,6 +5,7 @@ import (
"github.com/sqshq/sampler/component/layout" "github.com/sqshq/sampler/component/layout"
"github.com/sqshq/sampler/config" "github.com/sqshq/sampler/config"
"github.com/sqshq/sampler/console" "github.com/sqshq/sampler/console"
"github.com/sqshq/sampler/data"
"time" "time"
) )
@ -13,21 +14,23 @@ const (
) )
type Handler struct { type Handler struct {
samplers []*data.Sampler
options config.Options
layout *layout.Layout layout *layout.Layout
renderTicker *time.Ticker renderTicker *time.Ticker
consoleEvents <-chan ui.Event consoleEvents <-chan ui.Event
renderRate time.Duration renderRate time.Duration
options config.Options
} }
func NewHandler(layout *layout.Layout, options config.Options) *Handler { func NewHandler(samplers []*data.Sampler, options config.Options, layout *layout.Layout) *Handler {
renderRate := calcMinRenderRate(layout) renderRate := calcMinRenderRate(layout)
return &Handler{ return &Handler{
samplers: samplers,
options: options,
layout: layout, layout: layout,
consoleEvents: ui.PollEvents(), consoleEvents: ui.PollEvents(),
renderTicker: time.NewTicker(renderRate), renderTicker: time.NewTicker(renderRate),
renderRate: renderRate, renderRate: renderRate,
options: options,
} }
} }
@ -71,10 +74,19 @@ func (h *Handler) handleModeChange(m layout.Mode) {
switch m { switch m {
case layout.ModeDefault: case layout.ModeDefault:
h.renderTicker = time.NewTicker(h.renderRate) h.renderTicker = time.NewTicker(h.renderRate)
h.pause(false)
case layout.ModePause: case layout.ModePause:
h.pause(true)
// proceed with stopped timer // proceed with stopped timer
default: default:
h.renderTicker = time.NewTicker(console.MinRenderInterval) h.renderTicker = time.NewTicker(console.MinRenderInterval)
h.pause(false)
}
}
func (h *Handler) pause(pause bool) {
for _, s := range h.samplers {
s.Pause(pause)
} }
} }

24
main.go
View File

@ -30,40 +30,42 @@ type Starter struct {
cfg config.Config cfg config.Config
} }
func (s *Starter) startAll() { func (s *Starter) startAll() []*data.Sampler {
samplers := make([]*data.Sampler, 0)
for _, c := range s.cfg.RunCharts { for _, c := range s.cfg.RunCharts {
cpt := runchart.NewRunChart(c, s.palette) cpt := runchart.NewRunChart(c, s.palette)
s.start(cpt, cpt.Consumer, c.ComponentConfig, c.Items, c.Triggers) samplers = append(samplers, s.start(cpt, cpt.Consumer, c.ComponentConfig, c.Items, c.Triggers))
} }
for _, c := range s.cfg.SparkLines { for _, c := range s.cfg.SparkLines {
cpt := sparkline.NewSparkLine(c, s.palette) cpt := sparkline.NewSparkLine(c, s.palette)
s.start(cpt, cpt.Consumer, c.ComponentConfig, []config.Item{c.Item}, c.Triggers) samplers = append(samplers, s.start(cpt, cpt.Consumer, c.ComponentConfig, []config.Item{c.Item}, c.Triggers))
} }
for _, c := range s.cfg.BarCharts { for _, c := range s.cfg.BarCharts {
cpt := barchart.NewBarChart(c, s.palette) cpt := barchart.NewBarChart(c, s.palette)
s.start(cpt, cpt.Consumer, c.ComponentConfig, c.Items, c.Triggers) samplers = append(samplers, s.start(cpt, cpt.Consumer, c.ComponentConfig, c.Items, c.Triggers))
} }
for _, c := range s.cfg.Gauges { for _, c := range s.cfg.Gauges {
cpt := gauge.NewGauge(c, s.palette) cpt := gauge.NewGauge(c, s.palette)
s.start(cpt, cpt.Consumer, c.ComponentConfig, []config.Item{c.Cur, c.Min, c.Max}, c.Triggers) samplers = append(samplers, s.start(cpt, cpt.Consumer, c.ComponentConfig, []config.Item{c.Cur, c.Min, c.Max}, c.Triggers))
} }
for _, c := range s.cfg.AsciiBoxes { for _, c := range s.cfg.AsciiBoxes {
cpt := asciibox.NewAsciiBox(c, s.palette) cpt := asciibox.NewAsciiBox(c, s.palette)
s.start(cpt, cpt.Consumer, c.ComponentConfig, []config.Item{c.Item}, c.Triggers) samplers = append(samplers, s.start(cpt, cpt.Consumer, c.ComponentConfig, []config.Item{c.Item}, c.Triggers))
} }
for _, c := range s.cfg.TextBoxes { for _, c := range s.cfg.TextBoxes {
cpt := textbox.NewTextBox(c, s.palette) cpt := textbox.NewTextBox(c, s.palette)
s.start(cpt, cpt.Consumer, c.ComponentConfig, []config.Item{c.Item}, c.Triggers) samplers = append(samplers, s.start(cpt, cpt.Consumer, c.ComponentConfig, []config.Item{c.Item}, c.Triggers))
} }
return samplers
} }
func (s *Starter) start(drawable ui.Drawable, consumer *data.Consumer, componentConfig config.ComponentConfig, itemsConfig []config.Item, triggersConfig []config.TriggerConfig) { func (s *Starter) start(drawable ui.Drawable, consumer *data.Consumer, componentConfig config.ComponentConfig, itemsConfig []config.Item, triggersConfig []config.TriggerConfig) *data.Sampler {
cpt := component.NewComponent(drawable, consumer, componentConfig) cpt := component.NewComponent(drawable, consumer, componentConfig)
triggers := data.NewTriggers(triggersConfig, consumer, s.opt, s.player) triggers := data.NewTriggers(triggersConfig, consumer, s.opt, s.player)
items := data.NewItems(itemsConfig, *componentConfig.RateMs) items := data.NewItems(itemsConfig, *componentConfig.RateMs)
data.NewSampler(consumer, items, triggers, s.opt, s.cfg.Variables, *componentConfig.RateMs)
s.lout.AddComponent(cpt) s.lout.AddComponent(cpt)
time.Sleep(10 * time.Millisecond) // desync coroutines time.Sleep(10 * time.Millisecond) // desync coroutines
return data.NewSampler(consumer, items, triggers, s.opt, s.cfg.Variables, *componentConfig.RateMs)
} }
func main() { func main() {
@ -103,9 +105,9 @@ func main() {
metadata.PersistStatistics(cfg) metadata.PersistStatistics(cfg)
starter := &Starter{player, lout, palette, opt, *cfg} starter := &Starter{player, lout, palette, opt, *cfg}
starter.startAll() samplers := starter.startAll()
handler := event.NewHandler(lout, opt) handler := event.NewHandler(samplers, opt, lout)
handler.HandleEvents() handler.HandleEvents()
} }