Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Comparing changes

Choose two branches to see what's changed or to start a new pull request. If you need to, you can also compare across forks.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also compare across forks.
base fork: felipemorais/basket.js
base: bf1b306768
...
head fork: felipemorais/basket.js
compare: 85a2879fbc
  • 3 commits
  • 5 files changed
  • 0 commit comments
  • 3 contributors
View
3  .travis.yml
@@ -0,0 +1,3 @@
+language: node_js
+node_js:
+ - 0.8
View
189 dist/basket.js
@@ -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,50 +86,55 @@
}
};
- 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;
@@ -136,17 +142,17 @@
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
12 dist/basket.min.js
@@ -1,5 +1,9 @@
-/*! 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(a,b){"use strict";var c="basket-",d=[],e=0,f=0,g=[],h=function(a,b){var c=new XMLHttpRequest;c.open("GET",a,!0),c.onreadystatechange=function(){c.readyState===4&&b(c.responseText)},c.send()},i=function(a,b,c){h(a,function(a){localStorage.setItem(b,a),j(c)&&c()})},j=function(a){return Object.prototype.toString.call(a)==="[object Function]"},k=function(a){var c=b.createElement("script"),d=b.head||b.getElementsByTagName("head")[0];c.defer=!0,c.text=a,d.appendChild(c)},l=function(a){var b,c,f,h;if(e>=a)for(b=0;b<d.length;b++){f=d[b];if(!f)continue;d[b]=null,k(f),e++;for(c=b;c<e;c++)h=g[c],j(h)&&(g[c]=null,h())}};a.basket={require:function(a,b){b=b||{};var e=f,g=d.length,i=c+(b.key||a),j=localStorage.getItem(i);return d[g]=null,j?(d[g]=j,l(e)):h(a,function(a){localStorage.setItem(i,a),d[g]=a,l(e)}),this},add:function(a,b,d){b=b||{};var e=c+(b.key||a);return typeof b.overwrite=="undefined"&&(b.overwrite=!0),localStorage.getItem(e)?b.overwrite&&i(a,e,d):i(a,e,d),this},remove:function(a){return localStorage.removeItem(c+a),this},wait:function(a){return f=d.length-1,a&&(e>f?a():g[f]=a),this},get:function(a){return localStorage.getItem(c+a)||null}}})(this,document)
+* (c) Addy Osmani; MIT License
+* Created by: Addy Osmani, Sindre Sorhus, Andrée Hansson
+* Contributors: Ironsjp, Mathias Bynens, Rick Waldron
+*/
+(function(e,t){"use strict";var n=t.head||t.getElementsByTagName("head")[0],r="basket-",i=[],s=0,o=0,u=[],a=function(e){return{}.toString.call(e)==="[object Function]"},f=function(e,t){var n=new XMLHttpRequest;n.open("GET",e),n.onreadystatechange=function(){n.readyState===4&&n.status===200&&t(n.responseText)},n.send()},l=function(e,t,n,r){f(e,function(e){var i=p(e,n);localStorage.setItem(t,JSON.stringify(i)),a(r)&&r(e)})},c=function(e){var r=t.createElement("script");r.defer=!0,r.text=e,n.appendChild(r)},h=function(e,t){var n,r,o,f;if(s>=e)for(n=0;n<i.length;n++){o=i[n];if(!o)continue;i[n]=null,t&&c(o),s++;for(r=n;r<s;r++)f=u[r],a(f)&&(u[r]=null,f())}},p=function(e,t){var n=+(new Date),r={data:e,stamp:n};return t&&(r.expire=n+t*60*60*1e3),r},d=function(e){var t=r+(e.key||e.url),n=o,s=i.length,u=JSON.parse(localStorage.getItem(t)),f=function(t){i[s]=t,h(n,e.execute)};if(!e.url)return;e.execute=e.execute!==!1,i[s]=null,u&&u.expire&&u.expire- +(new Date)>0?f(u.data):l(e.url,t,e.expire,f),a(e.wait)&&basket.wait(e.wait)};e.basket={require:function(){var e,t;for(e=0,t=arguments.length;e<t;e++)d(arguments[e]);return this},remove:function(e){return localStorage.removeItem(r+e),this},wait:function(e){return o=i.length-1,a(e)&&(s>o?e():u[o]=e),this},get:function(e){return localStorage.getItem(r+e)||null},clear:function(){var e,t=localStorage;for(e in t)e.indexOf(r)===0&&delete t[e];return this}}})(this,document);
View
2  lib/basket.js
@@ -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
17 test/tests.js
@@ -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();
});
});

No commit comments for this range

Something went wrong with that request. Please try again.