Skip to content

Commit

Permalink
Request: Cleaned up implementation and tests for expiring persistent …
Browse files Browse the repository at this point in the history
…caches.
  • Loading branch information
scottgonzalez committed Mar 25, 2011
1 parent 68e5c31 commit 5513f12
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 43 deletions.
7 changes: 3 additions & 4 deletions request/amplify.request.js
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ if ( amplify.store ) {
}
var success = ajaxSettings.success;
ajaxSettings.success = function( data ) {
amplify.store[ type]( cacheKey, data , { expires: resource.cache.expires } );
amplify.store[ type ]( cacheKey, data, { expires: resource.cache.expires } );
success.apply( this, arguments );
};
};
Expand All @@ -210,9 +210,8 @@ if ( amplify.store ) {
amplify.subscribe( "request.before.ajax", function( resource ) {
var cacheType = resource.cache;
if ( cacheType ) {
if ( typeof cacheType === 'object' ) {
cacheType = cacheType.type;
}
// normalize between objects and strings/booleans/numbers
cacheType = cacheType.type || cacheType;
return cache[ cacheType in cache ? cacheType : "_default" ]
.apply( this, arguments );
}
Expand Down
70 changes: 31 additions & 39 deletions request/test/unit.js
Original file line number Diff line number Diff line change
Expand Up @@ -835,15 +835,16 @@ asyncTest( "cache: Number", function() {
});

if ( amplify.store ) {
asyncTest( "cache: persist - no expires", {
var storeExpiresLifecycle = {
setup: function() {
$.each( amplify.store(), function( key ) {
if ( /^request/.test( key ) ) {
amplify.store( key, null );
}
});
}
}, function() {
};
asyncTest( "cache: persist - no expires", storeExpiresLifecycle, function() {
expect( 15 );

var ajaxCalls = 0;
Expand Down Expand Up @@ -887,80 +888,71 @@ if ( amplify.store ) {
});
});
});

asyncTest( "cache: persist - with cache options", {
setup: function() {
$.each( amplify.store(), function( key ) {
if ( /^request/.test( key ) ) {
amplify.store( key, null );
}
});
}
}, function() {
expect( 17 );

var ajaxCalls = 0;

asyncTest( "cache: persist - expires", storeExpiresLifecycle, function() {
expect( 24 );

var shouldCache = false;
$( "#ajax-listener" ).ajaxComplete(function( event, xhr ) {
if ( xhr.statusText !== "abort" ) {
ok( !ajaxCalls++, "ajax call completed" );
ok( !shouldCache, "ajax call completed" );
}
});

amplify.request.define( "persist-cache", "ajax", {
url: "data/data.json",
dataType: "json",
cache: { type: 'persist', expires: 450 }
cache: { type: "persist", expires: 450 }
});
// should execute for both requests

// should execute for 3 requests
subscribe( "request.before", function( settings ) {
equal( settings.resourceId, "persist-cache", "before message: settings.resourceId" );
});
// should execute for first request only
// should execute for 2 requests
subscribe( "request.before.ajax", function( resource ) {
equal( resource.resourceId, "persist-cache", "before.ajax message: resource.resourceId" );
equal( resource.cache.expires, 450, "before.ajax message: resource.expires");
});
// should execute for both requests
// should execute 3 requests
subscribe( "request.success", function( settings, data ) {
equal( settings.resourceId, "persist-cache", "success message: settings.resourceId" );
deepEqual( data, { foo: "bar" }, "success message: data" );
});
// should execute for both requests
// should execute for 3 requests
subscribe( "request.complete", function( settings, data ) {
equal( settings.resourceId, "persist-cache", "complete message: settings.resourceId" );
deepEqual( data, { foo: "bar" }, "complete message: data" );
deepEqual( data, { foo: "bar" }, "complete message: data" );
});
subscribe( "request.error", function() {
ok( false, "error message published" );
});

amplify.request( "persist-cache", function( data ) {
// delay setting the flag because the success callback will be invoked
// before the ajaxComplete event is triggered
setTimeout(function() {
shouldCache = true;
}, 1 );
deepEqual( data, { foo: "bar" }, "first request callback" );
amplify.request( "persist-cache", function( data ) {
deepEqual( data, { foo: "bar" }, "second request callback" );
});
setTimeout(function() {
shouldCache = false;
amplify.request( "persist-cache", function( data ) {
deepEqual( data, { foo: "bar" }, "third request callback" );
start();
});
}, 500 );
});

setTimeout( function() {
deepEqual( amplify.store( amplify.request.cache._key( "persist-cache", "" ) ), { foo: "bar" }, "data not expired: 300" );
}, 300 );

setTimeout( function() {
deepEqual( amplify.store( amplify.request.cache._key( "persist-cache", "" ) ), undefined, "data should be expired: 500" );
start();
}, 500 );



});

test( "cache types", function() {
$.each( amplify.store.types, function( type ) {
ok( type in amplify.request.cache, type );
});
});

}

asyncTest( "decoder: Function - success", function() {
Expand Down

0 comments on commit 5513f12

Please sign in to comment.