Skip to content
This repository has been archived by the owner on Jan 23, 2024. It is now read-only.

Commit

Permalink
support periodic updates [closes #1]
Browse files Browse the repository at this point in the history
  • Loading branch information
simon-weber committed Nov 25, 2015
1 parent 50d2b48 commit 177958b
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 17 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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!
Expand Down
52 changes: 35 additions & 17 deletions src/js/background.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 => {
Expand Down

0 comments on commit 177958b

Please sign in to comment.