diff --git a/app/scheduler.go b/app/scheduler.go new file mode 100644 index 00000000..10a38207 --- /dev/null +++ b/app/scheduler.go @@ -0,0 +1,37 @@ +package app + +import ( + "time" + + "github.com/wtfutil/wtf/wtf" +) + +// Schedule kicks off the first refresh of a module's data and then queues the rest of the +// data refreshes on a timer +func Schedule(widget wtf.Wtfable) { + widget.Refresh() + + interval := time.Duration(widget.RefreshInterval()) * time.Second + + if interval <= 0 { + return + } + + tick := time.NewTicker(interval) + quit := make(chan struct{}) + + for { + select { + case <-tick.C: + if widget.Enabled() { + widget.Refresh() + } else { + tick.Stop() + return + } + case <-quit: + tick.Stop() + return + } + } +} diff --git a/app/wtf_app.go b/app/wtf_app.go index 289ad7e3..7d9ad76e 100644 --- a/app/wtf_app.go +++ b/app/wtf_app.go @@ -47,8 +47,6 @@ func NewWtfApp(app *tview.Application, config *config.Config, configFilePath str wtf.ValidateWidgets(wtfApp.Widgets) - wtfApp.scheduleWidgets() - return &wtfApp } @@ -56,13 +54,12 @@ func NewWtfApp(app *tview.Application, config *config.Config, configFilePath str // Start initializes the app func (wtfApp *WtfApp) Start() { + wtfApp.scheduleWidgets() go wtfApp.watchForConfigChanges() } // Stop kills all the currently-running widgets in this app func (wtfApp *WtfApp) Stop() { - // TODO: Pretty sure we should kill their go routines that run them as well - // otherwise....? wtfApp.disableAllWidgets() } @@ -115,7 +112,7 @@ func (wtfApp *WtfApp) refreshAllWidgets() { func (wtfApp *WtfApp) scheduleWidgets() { for _, widget := range wtfApp.Widgets { - go wtf.Schedule(widget) + go Schedule(widget) } } diff --git a/wtf/enablable.go b/wtf/enablable.go index fc02ab88..fe95afd2 100644 --- a/wtf/enablable.go +++ b/wtf/enablable.go @@ -2,8 +2,7 @@ package wtf // Enablable is the interface that enforces enable/disable capabilities on a module type Enablable interface { + Disable() Disabled() bool Enabled() bool - - Disable() } diff --git a/wtf/schedulable.go b/wtf/schedulable.go index 5ebd0fc4..24ee6dce 100644 --- a/wtf/schedulable.go +++ b/wtf/schedulable.go @@ -1,42 +1,8 @@ package wtf -import ( - "time" -) - // Schedulable is the interface that enforces scheduling capabilities on a module type Schedulable interface { Refresh() Refreshing() bool RefreshInterval() int } - -// Schedule kicks off the first refresh of a module's data and then queues the rest of the -// data refreshes on a timer -func Schedule(widget Wtfable) { - widget.Refresh() - - interval := time.Duration(widget.RefreshInterval()) * time.Second - - if interval <= 0 { - return - } - - tick := time.NewTicker(interval) - quit := make(chan struct{}) - - for { - select { - case <-tick.C: - if widget.Enabled() { - widget.Refresh() - } else { - tick.Stop() - return - } - case <-quit: - tick.Stop() - return - } - } -}