Skip to content
This repository has been archived by the owner on Nov 3, 2021. It is now read-only.

Commit

Permalink
Merge pull request #34266 from Cwiiis/fix-iconshelper-error-handling
Browse files Browse the repository at this point in the history
Bug 1267675 - Fix IconsHelper to cope with lack of datastore. r=albertopq
  • Loading branch information
Cwiiis committed Apr 27, 2016
2 parents 2c0e66f + 3d1dc8a commit 0b0d254
Showing 1 changed file with 43 additions and 25 deletions.
68 changes: 43 additions & 25 deletions shared/js/icons_helper.js
Expand Up @@ -92,6 +92,27 @@
});
}

function processIconBlob(iconBlob, iconUrl, uri, iconTargetSize) {
return new Promise((resolve, reject) => {
var img = document.createElement('img');
var icon = new Icon(img, uri);
icon.renderBlob(iconBlob, {
size: iconTargetSize,
onLoad: function(blob) {
var iconObj = {
blob: blob,
originalUrl: iconUrl.toString(),
timestamp: Date.now()
};
resolve(iconObj);
},
onerror: function(e) {
reject(`Failed to fetch icon ${iconUrl}`);
}
});
});
}

/**
* Same as above except the promise resolves as an object containing the blob
* of the icon and its size in pixels.
Expand All @@ -113,41 +134,38 @@
Date.now() - iconObj.timestamp >= ICON_CACHE_PERIOD) {
return fetchIconBlob(iconUrl)
.then(iconBlob => {
var img = document.createElement('img');
var icon = new Icon(img, uri);
icon.renderBlob(iconBlob, {
size: iconTargetSize,
onLoad: function(blob) {
var iconObj = {
blob: blob,
originalUrl: iconUrl.toString(),
timestamp: Date.now()
};
// We resolve here to avoid I/O blocking on
// dataStore and quicker display.
// Persisting to the dataStore takes place subsequently.
resolve(iconObj);

iconStore.add(iconObj, iconUrl);
},
onerror: function(e) {
reject(`Failed to fetch icon ${iconUrl}`);
}
});
})
.catch(err => {
processIconBlob(iconBlob, iconUrl, uri, iconTargetSize)
.then(
iconObj => {
// We resolve here to avoid I/O blocking on
// dataStore and quicker display.
// Persisting to the dataStore takes place after.
resolve(iconObj);

iconStore.add(iconObj, iconUrl);
});
}).catch(err => {
reject(`Failed to fetch icon ${iconUrl}: ${err}`);
});
}

return resolve(iconObj);
}).catch(err => {
// We should fetch the icon and resolve the promise here, anyhow.
reject(`Failed to get icon from dataStore: ${err}`);
});
}).catch(err => {
console.error(`Error opening the dataStore: ${err}`);

// We should fetch the icon and resolve the promise here, anyhow.
reject(`Error opening the dataStore: ${err}`);
fetchIconBlob(iconUrl).then(iconBlob => {
processIconBlob(iconBlob, iconUrl, uri, iconTargetSize)
.then(iconObj => {
console.log('Successfully fetched icon');
resolve(iconObj);
});
}).catch(err => {
reject(`Failed to fetch icon ${iconUrl}: ${err}`);
});
});
});
});
Expand Down

0 comments on commit 0b0d254

Please sign in to comment.