Skip to content

Commit

Permalink
add control for feed sync interval
Browse files Browse the repository at this point in the history
fixes #25
  • Loading branch information
derhuerst committed Mar 29, 2022
1 parent 159d2aa commit a83076f
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 1 deletion.
3 changes: 3 additions & 0 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,9 @@
.bar > .feed input {
width: 100%;
}
.bar > .feed > .interval {
width: 3em;
}
@keyframes spinning {
0% {
transform: rotate(0deg);
Expand Down
20 changes: 19 additions & 1 deletion stores/feed.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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))
Expand Down Expand Up @@ -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
7 changes: 7 additions & 0 deletions stores/url.js
Original file line number Diff line number Diff line change
Expand Up @@ -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': {
Expand All @@ -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,
Expand Down
12 changes: 12 additions & 0 deletions views/bar.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = (
<div class="feed">
<input onInput={handleFeedUrlInput} value={state.feedUrl} />
<input
class="interval"
type="number"
min="1"
step="10"
onChange={handleSyncIntervalChange}
value={state.feedSyncInterval}
/>
<button
class={state.feedSyncing ? 'sync syncing' : 'sync'}
onClick={handleSyncClick}
Expand Down

0 comments on commit a83076f

Please sign in to comment.