Commit 4b4412d6 authored by yPhil's avatar yPhil
Browse files

squash! Promisified lastItems()

- Correctly saving the right content :|
parent 71f1ed24
......@@ -214,15 +214,11 @@ PTL.feed = {
var newItems = 0;
var finalList;
var plop;
for (var key in data.feedItems) {
newItems++;
// console.log('YAAZ item:[%s], data[item]:[%s]', key, JSON.stringify(data.feedItems[key]));
var item = data.feedItems[key];
plop = plop + item.link + ', ';
if (newItems == nbItems) return false;
const $description = $.parseHTML(item.description),
......@@ -393,20 +389,12 @@ PTL.feed = {
$tempDom.empty();
// finalList = $feedBodyUl;
}
console.log('plop: %s (%s)',plop);
return resolve([$feedBodyUl.html(), newItems]);
// return true;
});
// console.log('plop: %s (%s)', plop);
},
errorFeed:function(error, feedUrl) {
......@@ -559,7 +547,6 @@ PTL.feed = {
}
if (feedName) {
feedTitle = feedName;
} else {
......@@ -598,168 +585,174 @@ PTL.feed = {
request.onsuccess = function(event) {
var db = event.target.result;
var transaction = db.transaction([PTL.DbStore]);
var objectStore = transaction.objectStore(PTL.DbStore);
var request = objectStore.get(feedUrl);
let transaction = db.transaction(PTL.DbStore, "readonly");
let objectStore = transaction.objectStore(PTL.DbStore);
let request = objectStore.get(feedUrl);
request.onerror = function(event) {
// Handle errors!
console.log('I NEVER happen :(', JSON.stringify(event));
};
request.onsuccess = function(event) {
if (typeof event.target.result !== 'undefined') {
console.log('event.result: %s (%s)', JSON.stringify(event.target.result));
if (event.target.result) {
saved = event.target.result.content;
// console.log('Kayn: %s (%s)', feedUrl);
console.log('Kayn: [%s] (%s)', saved, feedUrl);
isSaved = true;
} else {
console.log('Makayn\'sh: %s (%s)', feedUrl);
}
};
};
console.log('yo: %s (%s)', saved);
// console.log('yolo: %s (%s)', isSaved, feedUrl);
if ($feedBody.is(':empty')) {
$feedBody.append(saved);
}
if (isSaved) {
PTL.feed.getit(feedUrl, feedLastItem, feedNbItems)
.then(function(data) {
// console.log('NEW items: %s (%s)', $feedBodyUl[1], feedUrl);
if ($feedBody.is(':empty')) {
$feedBody.append(saved);
}
PTL.feed.lastItems(data, $dataStore)
.then((list) => {
PTL.feed.getit(feedUrl, feedLastItem, feedNbItems)
.then(function(data) {
var $feedBodyUl = list[0];
// console.log('NEW items: %s (%s)', $feedBodyUl[1], feedUrl);
if (data.lastItem) {
try {
$dataStore.data('lastitem', data.lastItem);
// localStorage.setItem(feedUrl, $html);
PTL.tab.saveTabs();
} catch(error) {
console.log('CATCHED!: %s (%s)', error);
}
}
var $feedBodyUl = PTL.feed.lastItems(data, $dataStore);
$badge.text(list[1]);
if (list[1] > 0) {
$feedBody.prepend($feedBodyUl[0]);
$badge.fadeIn('slow');
} else {
$badge.fadeOut('slow');
}
const $html = $feedBody.html();
$refreshButton
.prop('title', PTL.tr('Refresh this feed (%1 - %2)', feedName || feedUrl, timeStamp) + ' (' + $feedBodyUl[1] + ' new items)' )
.removeClass('spin');
if (data.lastItem) {
try {
$dataStore.data('lastitem', data.lastItem);
// localStorage.setItem(feedUrl, $html);
PTL.tab.saveTabs();
} catch(error) {
console.log('CATCHED!: %s (%s)', error);
}
}
})
.catch();
$badge.text($feedBodyUl[1]);
})
.catch(function(error) {
console.log('whoops: %s (%s)', JSON.stringify(error), feedUrl);
$feedBody.empty().append(PTL.feed.errorFeed(error, feedUrl));
$feedBody.css('height', '');
if ($feedBodyUl[1] > 0) {
$feedBody.prepend($feedBodyUl[0]);
$badge.fadeIn('slow');
} else {
$badge.fadeOut('slow');
}
$refreshButton.removeClass('spin');
});
$refreshButton
.prop('title', PTL.tr('Refresh this feed (%1 - %2)', feedName || feedUrl, timeStamp) + ' (' + $feedBodyUl[1] + ' new items)' )
.removeClass('spin');
// console.log('if: %s (%s)');
})
.catch(function(error) {
console.log('whoops: %s (%s)', JSON.stringify(error), feedUrl);
$feedBody.empty().append(PTL.feed.errorFeed(error, feedUrl));
$feedBody.css('height', '');
$refreshButton.removeClass('spin');
});
} else {
console.log('Makayn\'sh: %s (%s)', feedUrl);
console.log('if: %s (%s)');
} else {
// console.log('else: %s (%s)');
console.log('else: %s (%s)');
PTL.feed.getit(feedUrl, 'feedLastItem', feedNbItems)
.then(function(data) {
PTL.feed.getit(feedUrl, 'feedLastItem', feedNbItems)
.then(function(data) {
// console.log('YAAA: %s (%s)', JSON.stringify(data));
// console.log('YAAA: %s (%s)', JSON.stringify(data));
if (feedName) {
feedTitle = feedName;
} else if (data.feedTitle) {
feedTitle = data.feedTitle;
$dataStore.data('name', feedTitle);
}
if (feedName) {
feedTitle = feedName;
} else if (data.feedTitle) {
feedTitle = data.feedTitle;
$dataStore.data('name', feedTitle);
}
$feedLink.text(feedTitle)
.attr('href', data.feedLink)
.attr('title', feedTitle + ' (' + feedUrl + ')');
$feedLink.text(feedTitle)
.attr('href', data.feedLink)
.attr('title', feedTitle + ' (' + feedUrl + ')');
if (data.lastItem) {
// console.log('YEP: %s (%s)', data.lastItem);
$dataStore.attr('data-lastitem', data.lastItem);
}
if (data.lastItem) {
// console.log('YEP: %s (%s)', data.lastItem);
$dataStore.attr('data-lastitem', data.lastItem);
}
PTL.feed.lastItems(data, $dataStore).then((list) => {
var $feedBodyUl = PTL.feed.lastItems(data, $dataStore).then((list) => {
console.log('$feedBodyUl: %s (%s)', JSON.stringify(list[0]), list[1]);
$feedBody.html(list[0]);
$feedBody.html(list[0]);
let openRequest = indexedDB.open(PTL.DbName, PTL.DbVersion);
let openRequest = indexedDB.open(PTL.DbName, PTL.DbVersion);
$badge.text(list[1]);
$badge.text(list[1]);
if (list[1] > 0) {
$badge.fadeIn('slow');
} else {
$badge.fadeOut('slow');
}
if (list[1] > 0) {
$badge.fadeIn('slow');
} else {
$badge.fadeOut('slow');
}
$refreshButton
.prop('title', PTL.tr('Refresh this feed (%1 - %2)', feedName || feedUrl, timeStamp) + ' (' + $feedBodyUl[1] + ' new items)' )
.removeClass('spin');
$refreshButton
.prop('title', PTL.tr('Refresh this feed (%1 - %2)', feedName || feedUrl, timeStamp) + ' (' + $feedBodyUl[1] + ' new items)' )
.removeClass('spin');
openRequest.onsuccess = function() {
openRequest.onsuccess = function() {
let db = openRequest.result;
let db = openRequest.result;
if (!db.objectStoreNames.contains(PTL.DbStore)) {
db.createObjectStore(PTL.DbStore, {keyPath: PTL.DbKey});
}
if (!db.objectStoreNames.contains(PTL.DbStore)) {
db.createObjectStore(PTL.DbStore, {keyPath: PTL.DbKey});
}
let transaction = db.transaction(PTL.DbStore, "readwrite");
let transaction = db.transaction(PTL.DbStore, "readwrite");
let feeds = transaction.objectStore(PTL.DbStore);
let feeds = transaction.objectStore(PTL.DbStore);
let feed = {
url: feedUrl,
content: list[0]
};
let feed = {
url: feedUrl,
content: list[0]
};
let request = feeds.put(feed);
let request = feeds.put(feed);
request.onsuccess = function() {
console.log("Feed added to the store: ", request.result);
console.log('ADDED: %s (%s)', request.result, list[0]);
};
request.onsuccess = function() {
console.log("Feed added to the store: ", request.result);
};
request.onerror = function() {
console.log("Error: ", request.error);
};
request.onerror = function() {
console.log("Error: ", request.error);
};
};
};
});
});
// const $html = $feedBody.html();
// localStorage.setItem(feedUrl, $html);
})
.catch(function(error) {
console.log('whoops: %s (%s)', JSON.stringify(error));
$feedBody.empty().append(PTL.feed.errorFeed(error, feedUrl));
$feedBody.css('height', '');
// const $html = $feedBody.html();
// localStorage.setItem(feedUrl, $html);
$badge.fadeOut('fast');
})
.catch(function(error) {
console.log('whoops: %s (%s)', JSON.stringify(error));
$feedBody.empty().append(PTL.feed.errorFeed(error, feedUrl));
$feedBody.css('height', '');
$refreshButton.removeClass('spin');
});
$badge.fadeOut('fast');
}
};
$refreshButton.removeClass('spin');
});
};
}
// console.log('yolo: %s (%s)', isSaved, feedUrl);
if (progress) progress.increment();
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment