Permalink
Browse files

Fixed issue with expiration, fixes #39

  • Loading branch information...
1 parent 9d72f3e commit f972ee3cedec87dd794dff1b76525d4fdd536ab7 @peol peol committed Nov 14, 2012
Showing with 125 additions and 95 deletions.
  1. +104 −85 dist/basket.js
  2. +8 −4 dist/basket.min.js
  3. +1 −1 lib/basket.js
  4. +12 −5 test/tests.js
View
@@ -1,64 +1,61 @@
-/*! basket.js - v0.2.0 - 3/9/2012
+/*!
+* basket.js
+* v0.2.0 - 2012-11-14
* http://addyosmani.github.com/basket.js
-* Copyright (c) 2012 Addy Osmani; Licensed MIT, GPL
-* Credits: Addy Osmani, Ironsjp, Mathias Bynens, Rick Waldron, Sindre Sorhus, Andrée Hansson */
-
-;(function ( window, document ) {
- "use strict";
-
- var
- storagePrefix = "basket-",
- scripts = [],
- scriptsExecuted = 0,
- waitCount = 0,
- waitCallbacks = [],
+* (c) Addy Osmani; MIT License
+* Created by: Addy Osmani, Sindre Sorhus, Andrée Hansson
+* Contributors: Ironsjp, Mathias Bynens, Rick Waldron
+*/
+
+(function( window, document ) {
+ 'use strict';
+
+ var head = document.head || document.getElementsByTagName('head')[0];
+ var storagePrefix = 'basket-';
+ var scripts = [];
+ var scriptsExecuted = 0;
+ var globalWaitCount = 0;
+ var waitCallbacks = [];
+
+ var isFunc = function( fn ) {
+ return {}.toString.call( fn ) === '[object Function]';
+ };
- getUrl = function( url, callback ) {
+ var getUrl = function( url, callback ) {
var xhr = new XMLHttpRequest();
- xhr.open( "GET", url, true );
+ xhr.open( 'GET', url );
xhr.onreadystatechange = function() {
- if ( xhr.readyState === 4 ) {
+ if ( xhr.readyState === 4 && xhr.status === 200 ) {
callback( xhr.responseText );
}
};
xhr.send();
- },
+ };
- saveUrl = function( url, key, callback ) {
+ var saveUrl = function( url, key, expire, callback ) {
getUrl( url, function( text ) {
- localStorage.setItem( key, text );
+ var storeObj = wrapStoreData( text, expire );
+ localStorage.setItem( key, JSON.stringify( storeObj ) );
- if ( isFunc(callback) ) {
- callback();
+ if ( isFunc( callback ) ) {
+ callback( text );
}
});
- },
-
- isFunc = function( func ) {
- return Object.prototype.toString.call( func ) === "[object Function]";
- },
-
- injectScript = function( text ) {
- var
- script = document.createElement("script"),
- head = document.head || document.getElementsByTagName("head")[ 0 ];
+ };
+ var injectScript = function( text ) {
+ var script = document.createElement('script');
script.defer = true;
// Have to use .text, since we support IE8,
// which won't allow appending to a script
script.text = text;
-
head.appendChild( script );
- },
+ };
- queueExec = function( waitCount ) {
- var
- i,
- j,
- script,
- callback;
+ var queueExec = function( waitCount, doExecute ) {
+ var i, j, script, callback;
if ( scriptsExecuted >= waitCount ) {
for ( i = 0; i < scripts.length; i++ ) {
@@ -70,7 +67,11 @@
}
scripts[ i ] = null;
- injectScript( script );
+
+ if ( doExecute ) {
+ injectScript( script );
+ }
+
scriptsExecuted++;
for ( j = i; j < scriptsExecuted; j++ ) {
@@ -85,68 +86,73 @@
}
};
- window.basket = {
- require: function ( uri, options ) {
- options = options || {};
-
- var
- localWaitCount = waitCount,
- scriptIndex = scripts.length,
- key = storagePrefix + ( options.key || uri ),
- source = localStorage.getItem( key );
-
- scripts[ scriptIndex ] = null;
-
- if ( source ) {
- scripts[ scriptIndex ] = source;
- queueExec( localWaitCount );
- } else {
- getUrl( uri, function( text ) {
- localStorage.setItem( key, text );
- scripts[ scriptIndex ] = text;
- queueExec( localWaitCount );
- });
- }
+ var wrapStoreData = function( data, expiration ) {
+ var now = +new Date();
+ var storeObj = {
+ data: data,
+ stamp: now
+ };
- return this;
- },
+ if ( expiration ) {
+ storeObj.expire = now + ( expiration * 60 * 60 * 1000 );
+ }
- add: function( uri, options, callback ) {
- options = options || {};
+ return storeObj;
+ };
- var key = storagePrefix + ( options.key || uri );
+ var handleStackObject = function( obj ) {
+ var key = storagePrefix + ( obj.key || obj.url );
+ var waitCount = globalWaitCount;
+ var scriptIndex = scripts.length;
+ var source = JSON.parse( localStorage.getItem( key ) );
- // default is to overwrite
- if ( typeof options.overwrite === "undefined" ) {
- options.overwrite = true;
- }
+ var callback = function( text ) {
+ scripts[ scriptIndex ] = text;
+ queueExec( waitCount, obj.execute );
+ };
- // if they key exists and overwrite true, overwrite
- if ( localStorage.getItem(key) ) {
- if( options.overwrite ) {
- saveUrl( uri, key, callback );
- }
- } else {
- //key doesnt exist, add key as new entry
- saveUrl( uri, key, callback );
+ if ( !obj.url ) {
+ return;
+ }
+
+ obj.execute = obj.execute !== false;
+ scripts[ scriptIndex ] = null;
+
+ if ( source && ( source.expire && source.expire - +new Date() > 0 ) ) {
+ callback( source.data );
+ } else {
+ saveUrl( obj.url, key, obj.expire, callback );
+ }
+
+ if ( isFunc( obj.wait ) ) {
+ basket.wait( obj.wait );
+ }
+ };
+
+ window.basket = {
+ require: function() {
+ var i, l;
+
+ for ( i = 0, l = arguments.length; i < l; i++ ) {
+ handleStackObject( arguments[ i ] );
}
return this;
},
remove: function( key ) {
localStorage.removeItem( storagePrefix + key );
-
return this;
},
wait: function( callback ) {
- waitCount = scripts.length - 1;
- if ( callback ) {
- if ( scriptsExecuted > waitCount ) {
+ globalWaitCount = scripts.length - 1;
+
+ if ( isFunc( callback ) ) {
+ if ( scriptsExecuted > globalWaitCount ) {
callback();
} else {
- waitCallbacks[ waitCount ] = callback;
+ waitCallbacks[ globalWaitCount ] = callback;
}
}
@@ -155,7 +161,20 @@
get: function( key ) {
return localStorage.getItem( storagePrefix + key ) || null;
+ },
+
+ clear: function() {
+ var key;
+ var ls = localStorage;
+
+ for ( key in ls ) {
+ if ( key.indexOf( storagePrefix ) === 0 ) {
+ delete ls[ key ];
+ }
+ }
+
+ return this;
}
};
-}( this, document ));
+})( this, document );
View

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.
View
@@ -110,7 +110,7 @@
obj.execute = obj.execute !== false;
scripts[ scriptIndex ] = null;
- if ( source && ( source.expire && source.expire - +new Date() < 0 ) ) {
+ if ( source && ( source.expire && source.expire - +new Date() > 0 ) ) {
callback( source.data );
} else {
saveUrl( obj.url, key, obj.expire, callback );
View
@@ -1,3 +1,4 @@
+/*global module, asyncTest, ok, start, basket*/
module( 'Test script API', {
teardown: function() {
localStorage.clear();
@@ -95,19 +96,23 @@ asyncTest( 'clear()', 1, function() {
});
});
+function getLSData() {
+ return JSON.parse(localStorage.getItem('basket-fixtures/stamp-script.js'));
+}
asyncTest( 'store data using expiration (non-expired)', 2, function() {
basket
.require({ url: 'fixtures/stamp-script.js', expire: 1 })
.wait(function() {
- var stamp = basket.lastXHR;
+ var stamp = getLSData().stamp;
ok( basket.get('fixtures/stamp-script.js'), 'Data exists in localStorage' );
basket
.require({ url: 'fixtures/stamp-script.js' })
.wait(function() {
- console.log( basket , stamp)
- ok( basket.lastXHR === stamp, 'Data retrieved from localStorage' );
+ var stampAfter = getLSData().stamp;
+ ok( stamp === stampAfter, 'Data retrieved from localStorage' );
+
start();
});
});
@@ -118,13 +123,15 @@ asyncTest( 'store data using expiration (expired)', 2, function() {
basket
.require({ url: 'fixtures/stamp-script.js', expire: -1 })
.wait(function() {
- var stamp = basket.lastXHR;
+ var stamp = getLSData().stamp;
ok( basket.get('fixtures/stamp-script.js'), 'Data exists in localStorage' );
basket
.require({ url: 'fixtures/stamp-script.js' })
.wait(function() {
- ok( basket.lastXHR !== stamp, 'Data retrieved from server' );
+ var stampAfter = getLSData().stamp;
+ ok( stamp !== stampAfter, 'Data retrieved from server' );
+
start();
});
});

0 comments on commit f972ee3

Please sign in to comment.