From a83076ff958597838b4d0f424c6f02c34451752e Mon Sep 17 00:00:00 2001 From: Jannis R Date: Tue, 29 Mar 2022 17:31:00 +0200 Subject: [PATCH] add control for feed sync interval fixes #25 --- index.html | 3 +++ stores/feed.js | 20 +++++++++++++++++++- stores/url.js | 7 +++++++ views/bar.js | 12 ++++++++++++ 4 files changed, 41 insertions(+), 1 deletion(-) diff --git a/index.html b/index.html index 77e3aca..b6e894a 100644 --- a/index.html +++ b/index.html @@ -142,6 +142,9 @@ .bar > .feed input { width: 100%; } + .bar > .feed > .interval { + width: 3em; + } @keyframes spinning { 0% { transform: rotate(0deg); diff --git a/stores/feed.js b/stores/feed.js index 1d5e3e9..04a9490 100644 --- a/stores/feed.js +++ b/stores/feed.js @@ -14,6 +14,7 @@ const CONTENT_TYPES = [ const feedStore = (state, bus) => { state.feedUrl = null state.feedSyncStopped = false + state.feedSyncInterval = 30 // 30s state.feedSyncing = false state.feedError = null state.feedRawData = null @@ -58,7 +59,7 @@ const feedStore = (state, bus) => { } const resetSync = debounce(() => { sync = syncViaPeriodicFetch(state.feedUrl, { - interval: 30 * 1000, // 30s + interval: state.feedSyncInterval * 1000, }) sync.on('fetch', () => setSyncing(true)) sync.on('fetch-done', () => setSyncing(false)) @@ -109,6 +110,23 @@ const feedStore = (state, bus) => { if (sync) sync.start() bus.emit(bus.STATE_CHANGE) }) + + bus.on('feed:set-sync-interval', (newInterval) => { + newInterval = Math.max(newInterval, 1) + if (newInterval === state.feedSyncInterval) return; // nothing changed, abort + + // todo: change `sync`'s interval directly + // clean up + if (sync) { + sync.stop() + sync = null + } + + state.feedSyncInterval = newInterval + if (state.feedUrl !== null) resetSync() + + bus.emit(bus.STATE_CHANGE) + }) } export default feedStore diff --git a/stores/url.js b/stores/url.js index 755ded0..06adc9c 100644 --- a/stores/url.js +++ b/stores/url.js @@ -7,6 +7,7 @@ const qsParse = memoize(_qsParse) const decodeString = str => str const decodeBoolean = str => str === 'true' +const decodeInteger = str => parseInt(str) const PERSISTED_STATE_FIELDS = { // stores/feed 'feedUrl': { @@ -19,6 +20,12 @@ const PERSISTED_STATE_FIELDS = { bus.emit(`feed:${stopped ? 'stop' : 'start'}-sync`) }, }, + 'feedSyncInterval': { + decode: decodeInteger, + apply: (interval, bus) => { + bus.emit('feed:set-sync-interval', interval) + }, + }, // stores/view 'view': { decode: decodeString, diff --git a/views/bar.js b/views/bar.js index 7e86556..c127cd9 100644 --- a/views/bar.js +++ b/views/bar.js @@ -34,9 +34,21 @@ const barView = ({state, emit}) => { if (state.feedSyncStopped) emit('feed:start-sync') else emit('feed:stop-sync') } + const handleSyncIntervalChange = (ev) => { + const val = parseInt(ev.target.value) + emit('feed:set-sync-interval', val) + } const feed = (
+