From 177958b78d2bec123b5178a81dd2c468732baf37 Mon Sep 17 00:00:00 2001 From: Simon Weber Date: Wed, 25 Nov 2015 17:34:57 -0500 Subject: [PATCH] support periodic updates [closes #1] --- CHANGELOG.md | 6 +++++ src/js/background.js | 52 +++++++++++++++++++++++++++++--------------- 2 files changed, 41 insertions(+), 17 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ae75f4b..2f3363d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,9 @@ +## 0.1.1 () + +Features: + + - support periodic updates: https://github.com/simon-weber/Autoplaylists-for-Google-Music/issues/1 + ## 0.0.1 (November 24, 2015) First packaged release! diff --git a/src/js/background.js b/src/js/background.js index 39c49cc..13b6425 100644 --- a/src/js/background.js +++ b/src/js/background.js @@ -146,6 +146,26 @@ function renameAndSync(playlist) { }); } +function forceUpdate(userId) { + getPollTimestamp(userId, timestamp => { + if (!(dbIsInit[userId])) { + console.warn('refusing forceUpdate because db is not init'); + return; + } else if (!timestamp) { + console.warn('db was init, but no timestamp found'); + return; + } + + diffUpdateLibrary(userId, timestamp, () => { + Storage.getPlaylistsForUser(userId, playlists => { + for (let i = 0; i < playlists.length; i++) { + renameAndSync(playlists[i]); + } + }); + }); + }); +} + function main() { Storage.addPlaylistChangeListener(change => { const hasOld = 'oldValue' in change; @@ -173,28 +193,26 @@ function main() { Chrometools.focusOrCreateTab(managerUrl); }); + // Update periodically. + setInterval(() => { + // TODO probably need to lock on playlist id to avoid race conditions + console.log('starting periodic update'); + for (const userId in users) { + forceUpdate(userId); + } + }, 60 * 1000); + chrome.runtime.onMessage.addListener((request, sender, sendResponse) => { // respond to manager / content script requests. if (request.action === 'forceUpdate') { - getPollTimestamp(request.userId, timestamp => { - if (!(dbIsInit[request.userId])) { - console.warn('refusing forceUpdate because db is not init'); - return; - } else if (!timestamp) { - console.warn('db was init, but no timestamp found'); - return; - } - - diffUpdateLibrary(request.userId, timestamp, () => { - Storage.getPlaylistsForUser(request.userId, playlists => { - for (let i = 0; i < playlists.length; i++) { - renameAndSync(playlists[i]); - } - }); - }); - }); + forceUpdate(request.userId); } else if (request.action === 'showPageAction') { + if (!(request.userId)) { + console.warn('received falsey user id from page action'); + return false; + } + if (!(request.userId in dbs)) { // init the db. Trackcache.openDb(request.userId, db => {