box art caching now works

This commit is contained in:
R. Aidan Campbell 2016-07-10 13:37:56 -04:00
parent 74069f3f0f
commit 34f22aacc8

View File

@ -45,17 +45,24 @@ function NvHTTP(address, clientUid) {
_self = this; _self = this;
}; };
function ab2str(buf) { function _arrayBufferToBase64( buffer ) {
return String.fromCharCode.apply(null, new Uint16Array(buf)); var binary = '';
var bytes = new Uint8Array( buffer );
var len = bytes.byteLength;
for (var i = 0; i < len; i++) {
binary += String.fromCharCode( bytes[ i ] );
}
return window.btoa( binary );
} }
function str2ab(str) { function _base64ToArrayBuffer(base64) {
var buf = new ArrayBuffer(str.length*2); // 2 bytes for each char var binary_string = window.atob(base64);
var bufView = new Uint16Array(buf); var len = binary_string.length;
for (var i=0, strLen=str.length; i < strLen; i++) { var bytes = new Uint8Array( len );
bufView[i] = str.charCodeAt(i); for (var i = 0; i < len; i++) {
bytes[i] = binary_string.charCodeAt(i);
} }
return buf; return bytes.buffer;
} }
NvHTTP.prototype = { NvHTTP.prototype = {
@ -167,16 +174,18 @@ NvHTTP.prototype = {
var storedBoxArtArray; var storedBoxArtArray;
if (JSONCachedBoxArtArray.boxArtCache != undefined) { if (JSONCachedBoxArtArray.boxArtCache != undefined) {
storedBoxArtArray = JSONCachedBoxArtArray.boxArtCache; storedBoxArtArray = JSONCachedBoxArtArray.boxArtCache;
for (key in storedBoxArtArray) { for (key in storedBoxArtArray) { // decode all the data
storedBoxArtArray[key] = str2ab(storedBoxArtArray[key]); storedBoxArtArray[key] = _base64ToArrayBuffer(storedBoxArtArray[key]);
} }
} else { } else {
storedBoxArtArray = {}; storedBoxArtArray = {};
} }
// if we already have it, load it. // if we already have it, load it.
if (storedBoxArtArray[appId] !== undefined && Object.keys(storedBoxArtArray[appId]).length !== 0 && storedBoxArtArray[appId].constructor !== Object) { if (storedBoxArtArray[appId] !== undefined && Object.keys(storedBoxArtArray).length !== 0 && storedBoxArtArray[appId].constructor !== Object) {
resolve(_self._memCachedBoxArtArray[appId]); console.log('returning cached box art');
resolve(storedBoxArtArray[appId]);
return;
} }
// otherwise, put it in our cache, then return it // otherwise, put it in our cache, then return it
@ -192,11 +201,13 @@ NvHTTP.prototype = {
var obj = {}; var obj = {};
var arrayToStore = {} var arrayToStore = {}
for (key in _self._memCachedBoxArtArray) { // convert the arraybuffer into a string for (key in _self._memCachedBoxArtArray) { // convert the arraybuffer into a string
arrayToStore[key] = ab2str(_self._memCachedBoxArtArray[key]); arrayToStore[key] = _arrayBufferToBase64(_self._memCachedBoxArtArray[key]);
} }
obj['boxArtCache'] = arrayToStore; // storage is in JSON format. JSON does not support binary data. obj['boxArtCache'] = arrayToStore; // storage is in JSON format. JSON does not support binary data.
chrome.storage.local.set(obj, function(onSuccess) {}); chrome.storage.local.set(obj, function(onSuccess) {});
console.log('returning streamed box art');
resolve(streamedBoxArt); resolve(streamedBoxArt);
return;
}); });