Skip to content
This repository has been archived by the owner on Feb 4, 2019. It is now read-only.

Commit

Permalink
Applied addyosmani#160
Browse files Browse the repository at this point in the history
  • Loading branch information
KarlCastle committed Jul 16, 2016
1 parent f8ea6e9 commit ff25d43
Showing 1 changed file with 73 additions and 64 deletions.
137 changes: 73 additions & 64 deletions lib/basket.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,9 +104,9 @@

var isCacheValid = function(source, obj) {
return !source ||
source.expire - +new Date() < 0 ||
obj.unique !== source.unique ||
(basket.isValidItem && !basket.isValidItem(source, obj));
source.expire - +new Date() < 0 ||
obj.unique !== source.unique ||
(basket.isValidItem && !basket.isValidItem(source, obj));
};

var handleStackObject = function( obj ) {
Expand All @@ -118,7 +118,7 @@

obj.key = ( obj.key || obj.url );
source = basket.get( obj.key );

obj.execute = obj.execute !== false;

shouldFetch = isCacheValid(source, obj);
Expand All @@ -132,13 +132,13 @@

if( obj.live && !shouldFetch ) {
promise = promise
.then( function( result ) {
// If we succeed, just return the value
// RSVP doesn't have a .fail convenience method
return result;
}, function() {
return source;
});
.then( function( result ) {
// If we succeed, just return the value
// RSVP doesn't have a .fail convenience method
return result;
}, function() {
return source;
});
}
} else {
source.type = obj.type || source.originalType;
Expand All @@ -153,15 +153,24 @@

var injectScript = function( obj ) {
var script = document.createElement('script');
script.type = obj.type || 'text/javascript';
script.defer = true;
// Have to use .text, since we support IE8,
// which won't allow appending to a script
script.text = obj.data;
head.appendChild( script );
};

var injectStyle = function( obj ) {
var style = document.createElement('style');
style.type = 'text/css';
style.appendChild(document.createTextNode(obj.data));
head.appendChild( style );
};

var handlers = {
'default': injectScript
'default': injectScript,
'text/css': injectStyle,
};

var execute = function( obj ) {
Expand Down Expand Up @@ -202,67 +211,67 @@
};

window.basket = {
require: function() {
for ( var a = 0, l = arguments.length; a < l; a++ ) {
arguments[a].execute = arguments[a].execute !== false;

if ( arguments[a].once && inBasket.indexOf(arguments[a].url) >= 0 ) {
arguments[a].execute = false;
} else if ( arguments[a].execute !== false && inBasket.indexOf(arguments[a].url) < 0 ) {
inBasket.push(arguments[a].url);
require: function() {
for ( var a = 0, l = arguments.length; a < l; a++ ) {
arguments[a].execute = arguments[a].execute !== false;

if ( arguments[a].once && inBasket.indexOf(arguments[a].url) >= 0 ) {
arguments[a].execute = false;
} else if ( arguments[a].execute !== false && inBasket.indexOf(arguments[a].url) < 0 ) {
inBasket.push(arguments[a].url);
}
}
}

var promise = fetch.apply( null, arguments ).then( performActions );

promise.thenRequire = thenRequire;
return promise;
},

remove: function( key ) {
localStorage.removeItem( storagePrefix + key );
return this;
},

get: function( key ) {
var item = localStorage.getItem( storagePrefix + key );
try {
return JSON.parse( item || 'false' );
} catch( e ) {
return false;
}
},

clear: function( expired ) {
var item, key;
var now = +new Date();
var promise = fetch.apply( null, arguments ).then( performActions );

promise.thenRequire = thenRequire;
return promise;
},

for ( item in localStorage ) {
key = item.split( storagePrefix )[ 1 ];
if ( key && ( !expired || this.get( key ).expire <= now ) ) {
this.remove( key );
remove: function( key ) {
localStorage.removeItem( storagePrefix + key );
return this;
},

get: function( key ) {
var item = localStorage.getItem( storagePrefix + key );
try {
return JSON.parse( item || 'false' );
} catch( e ) {
return false;
}
}
},

return this;
},
clear: function( expired ) {
var item, key;
var now = +new Date();

isValidItem: null,
for ( item in localStorage ) {
key = item.split( storagePrefix )[ 1 ];
if ( key && ( !expired || this.get( key ).expire <= now ) ) {
this.remove( key );
}
}

timeout: 5000,
return this;
},

addHandler: function( types, handler ) {
if( !Array.isArray( types ) ) {
types = [ types ];
}
types.forEach( function( type ) {
handlers[ type ] = handler;
});
},
isValidItem: null,

removeHandler: function( types ) {
basket.addHandler( types, undefined );
}
timeout: 5000,

addHandler: function( types, handler ) {
if( !Array.isArray( types ) ) {
types = [ types ];
}
types.forEach( function( type ) {
handlers[ type ] = handler;
});
},

removeHandler: function( types ) {
basket.addHandler( types, undefined );
}
};

// delete expired keys
Expand Down

0 comments on commit ff25d43

Please sign in to comment.