summaryrefslogtreecommitdiff
path: root/interval
diff options
context:
space:
mode:
authorLaria Carolin Chabowski <laria@laria.me>2020-10-04 22:58:03 +0200
committerLaria Carolin Chabowski <laria@laria.me>2020-10-04 22:58:03 +0200
commit5b456afb49bfa6ff1567510bc1d9362377d32216 (patch)
treee9f13973251405095dd8b0a31930ca10caa57163 /interval
parent1f700deeb1a26ab289178b76518a33faa3f51545 (diff)
downloadstartpage-5b456afb49bfa6ff1567510bc1d9362377d32216.tar.gz
startpage-5b456afb49bfa6ff1567510bc1d9362377d32216.tar.bz2
startpage-5b456afb49bfa6ff1567510bc1d9362377d32216.zip
New config format and new features
We're now using json as a config format instead of our weird own format. The weather icon is now optional, simply don't define WeatherPlace in your config. You can now specify which subreddit to get background images from. The default is still /r/EarthPorn.
Diffstat (limited to 'interval')
-rw-r--r--interval/interval.go33
1 files changed, 33 insertions, 0 deletions
diff --git a/interval/interval.go b/interval/interval.go
new file mode 100644
index 0000000..35996bb
--- /dev/null
+++ b/interval/interval.go
@@ -0,0 +1,33 @@
+package interval
+
+import (
+ "time"
+)
+
+// IntervalRunner is used to run a function again only after a certain time
+type IntervalRunner struct {
+ durationSuccess time.Duration
+ durationFailure time.Duration
+ lastRun time.Time
+ success bool
+}
+
+func NewIntervalRunner(durationSuccess, durationFailure time.Duration) *IntervalRunner {
+ return &IntervalRunner{durationSuccess: durationSuccess, durationFailure: durationFailure}
+}
+
+// Run runs the passed in fn function, if the last run is a certain duration ago.
+func (ir *IntervalRunner) Run(fn func() bool) {
+ var duration time.Duration
+ if ir.success {
+ duration = ir.durationSuccess
+ } else {
+ duration = ir.durationFailure
+ }
+
+ now := time.Now()
+ if ir.lastRun.Add(duration).Before(now) {
+ ir.success = fn()
+ ir.lastRun = now
+ }
+}