diff --git a/api/subscriptions.js b/api/subscriptions.js index ef1d8c5..80beaaa 100644 --- a/api/subscriptions.js +++ b/api/subscriptions.js @@ -3,6 +3,7 @@ const db = require("../utils/db") const {fetchChannelLatest} = require("../utils/youtube") const {getUser} = require("../utils/getuser") const {timeToPastText} = require("../utils/converters") +const {refresher} = require("../background/feed-update") module.exports = [ { @@ -13,6 +14,8 @@ module.exports = [ let channels = [] let refreshed = null if (user.token) { + // trigger a background refresh, needed if they came back from being inactive + refresher.skipWaiting() // get channels const subscriptions = user.getSubscriptions() const template = Array(subscriptions.length).fill("?").join(", ") diff --git a/background/feed-update.js b/background/feed-update.js index cacc72d..3de5edd 100644 --- a/background/feed-update.js +++ b/background/feed-update.js @@ -61,43 +61,68 @@ class RefreshQueue { } } -const refreshQueue = new RefreshQueue() +class Refresher { + constructor() { + this.sym = constants.symbols.refresher + this.refreshQueue = new RefreshQueue() + this.state = this.sym.ACTIVE + this.waitingTimeout = null + this.next() + } -function refreshChannel(ucid) { - return fetch(`http://localhost:3000/api/v1/channels/${ucid}/latest`).then(res => res.json()).then(root => { - if (Array.isArray(root)) { - root.forEach(video => { - // organise - video.descriptionHtml = video.descriptionHtml.replace(/ res.json()).then(root => { + if (Array.isArray(root)) { + root.forEach(video => { + // organise + video.descriptionHtml = video.descriptionHtml.replace(/ this.next(), timeToWait) + return + } else { + this.refreshQueue.load() + } } - }) -} -function refreshNext() { - if (refreshQueue.isEmpty()) { - const timeSinceLastLoop = Date.now() - refreshQueue.lastLoadTime - if (timeSinceLastLoop < constants.caching.subscriptions_refresh_loop_min) { - const timeToWait = constants.caching.subscriptions_refresh_loop_min - timeSinceLastLoop - console.log(`waiting ${timeToWait} before next loop`) - return setTimeout(refreshNext, timeToWait) + if (!this.refreshQueue.isEmpty()) { + this.state = this.sym.ACTIVE + const ucid = this.refreshQueue.next() + this.refreshChannel(ucid).then(() => this.next()) } else { - refreshQueue.load() + this.state = this.sym.EMPTY } } - const ucid = refreshQueue.next() - refreshChannel(ucid).then(refreshNext) + skipWaiting() { + if (this.state !== this.sym.ACTIVE) { + clearTimeout(this.waitingTimeout) + this.refreshQueue.lastLoadTime = 0 + this.next() + } + } } -refreshNext() +const refresher = new Refresher() + +module.exports.refresher = refresher diff --git a/utils/constants.js b/utils/constants.js index 88bdc32..4625208 100644 --- a/utils/constants.js +++ b/utils/constants.js @@ -19,6 +19,14 @@ const constants = { regex: { ucid: "[A-Za-z0-9-_]+", video_id: "[A-Za-z0-9-_]+" + }, + + symbols: { + refresher: { + ACTIVE: Symbol("ACTIVE"), + WAITING: Symbol("WAITING"), + EMPTY: Symbol("EMPTY") + } } }