Skip to content

Commit

Permalink
fix: intermittent failures to retrieve feed in new layout
Browse files Browse the repository at this point in the history
  • Loading branch information
teddy-gustiaux committed Jun 8, 2019
1 parent cd90d4d commit f16d787
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 12 deletions.
29 changes: 17 additions & 12 deletions src/lib/EventManager.js
Expand Up @@ -20,30 +20,35 @@ class EventManager {
* Show the page action to the user if it exists, and hide it if does not.
* @param {number} tabId The ID of the current tab
* @param {URL} url An instance of the URL object for the current tab address
* @param {number} [delay=1000] Minimum time to wait before trying to retrieve the feed
* @returns {Promise<boolean>} `true` if the feed retrieval succeeded, `false` otherwise
*/
async _retrieveFeed(tabId, urlString) {
async _retrieveFeed(tabId, urlString, delay = 1000) {
/**
* As per the manifest, the page action icon is only shown on YouTube domain.
* So, we only have to hide it if we are on YouTube, before trying to retrieve a feed.
*/
browser.pageAction.hide(tabId);
this.rssFeed = null;
const url = Utils.buildUrlObject(urlString);
if (url === null) return;
if (url === null) return false;

Utils.debug(`Trying to retrieve the feed for the current page [${urlString}]`);
const feedBuilder = new FeedBuilder(tabId, url);
await feedBuilder.getContentAddress();
const feed = feedBuilder.buildContentIdentifier().buildContentFeed();
return Utils.delay(delay).then(async () => {
const feedBuilder = new FeedBuilder(tabId, url);
await feedBuilder.getContentAddress();
const feed = feedBuilder.buildContentIdentifier().buildContentFeed();

if (!Utils.isValidURL(feed)) {
Utils.debug(`Feed is invalid - determined as [${feed}]`);
return;
}
if (!Utils.isValidURL(feed)) {
Utils.debug(`Feed is invalid - determined as [${feed}]`);
return false;
}

Utils.debug(`Feed has been determined as [${feed}]`);
this.rssFeed = feed;
browser.pageAction.show(tabId);
Utils.debug(`Feed has been determined as [${feed}]`);
this.rssFeed = feed;
browser.pageAction.show(tabId);
return true;
});
}

/**
Expand Down
10 changes: 10 additions & 0 deletions src/lib/Utils.js
Expand Up @@ -102,4 +102,14 @@ class Utils {
Utils.debug(`Successfully located the canonical URL as [${canonicalLink.href}]`);
return canonicalLink.href;
}

/**
* Get a promise that will resolve after the provided duration.
* @param {number} ms Minimum time in milliseconds to wait before resolving the promise
* @returns {Promise<void>} TOOD
* @async
*/
static delay(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
}

0 comments on commit f16d787

Please sign in to comment.