FIX: avoid adding same song in a row to the recent list
This commit is contained in:
parent
d4e15290fd
commit
d52ed226fa
1 changed files with 30 additions and 8 deletions
38
js/db.js
38
js/db.js
|
|
@ -85,18 +85,40 @@ export class MusicDatabase {
|
||||||
async addToHistory(track) {
|
async addToHistory(track) {
|
||||||
const storeName = 'history_tracks';
|
const storeName = 'history_tracks';
|
||||||
const minified = this._minifyItem('track', track);
|
const minified = this._minifyItem('track', track);
|
||||||
// Use a unique timestamp even if called rapidly
|
const timestamp = Date.now();
|
||||||
// (though unlikely to be <1ms for playback start)
|
const entry = { ...minified, timestamp };
|
||||||
const entry = { ...minified, timestamp: Date.now() };
|
|
||||||
|
|
||||||
const db = await this.open();
|
const db = await this.open();
|
||||||
const transaction = db.transaction(storeName, 'readwrite');
|
|
||||||
const store = transaction.objectStore(storeName);
|
|
||||||
|
|
||||||
// Add new entry
|
return new Promise((resolve, reject) => {
|
||||||
store.put(entry);
|
const transaction = db.transaction(storeName, 'readwrite');
|
||||||
|
const store = transaction.objectStore(storeName);
|
||||||
|
const index = store.index('timestamp');
|
||||||
|
|
||||||
return entry;
|
// Check the most recent entry
|
||||||
|
const cursorReq = index.openCursor(null, 'prev');
|
||||||
|
|
||||||
|
cursorReq.onsuccess = (e) => {
|
||||||
|
const cursor = e.target.result;
|
||||||
|
if (cursor) {
|
||||||
|
const lastTrack = cursor.value;
|
||||||
|
if (lastTrack.id === track.id) {
|
||||||
|
// If same track, delete the old entry so we just update the timestamp
|
||||||
|
store.delete(cursor.primaryKey);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// Add the new entry
|
||||||
|
store.put(entry);
|
||||||
|
};
|
||||||
|
|
||||||
|
cursorReq.onerror = (e) => {
|
||||||
|
// If cursor fails, just try to put (fallback)
|
||||||
|
store.put(entry);
|
||||||
|
};
|
||||||
|
|
||||||
|
transaction.oncomplete = () => resolve(entry);
|
||||||
|
transaction.onerror = (e) => reject(e.target.error);
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
async getHistory() {
|
async getHistory() {
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue