Permalink
Browse files

Merge branch 'override-jQuery.ajax'

  • Loading branch information...
2 parents 3586ae4 + da97b3a commit 62ba7ba7f1bf74db276cddb7b51e99091ad9f310 @execjosh execjosh committed Jul 11, 2010
Showing with 273 additions and 78 deletions.
  1. +46 −17 src/jquery.ajaxretry.js
  2. +4 −0 test/test.css
  3. +20 −61 test/test.html
  4. +198 −0 test/test.js
  5. +5 −0 test/test.php
View
@@ -23,28 +23,46 @@
DEF_DELAY_FUNC = function(i){
return Math.floor(Math.random() * ((2 << i) - 1));
},
+ DEF_ERROR_CODES = [502,503,504],
DEF_SLOT_TIME = 1000,
DEF_OPTS = {
attempts: DEF_ATTEMPTS,
cutoff: DEF_CUTOFF,
delay_func: DEF_DELAY_FUNC,
+ error_codes: DEF_ERROR_CODES,
slot_time: DEF_SLOT_TIME,
tick: NOP_FUNC
},
- ajaxWithRetry = function(req){
+ original_ajax_func = $.ajax,
+ ajaxWithRetry = function(settings){
+ settings = $.extend(true, {}, $.ajaxSettings, settings);
+
+ if (!settings.retry) {
+ return original_ajax_func(settings);
+ }
+
var failures = 0,
- opts = $.extend(true, {}, DEF_OPTS, req.retry || {}),
- orig_err_func = req.error || NOP_FUNC;
+ opts = $.extend(true, {}, $.ajaxRetrySettings, settings.retry),
+ orig_err_func = settings.error || NOP_FUNC;
- function retry_delay(time) {
- if (0 > time) {
- $.ajax(req);
+ function retry_delay(ticks) {
+ if (0 > ticks) {
+ original_ajax_func(settings);
}
else {
// Send tick event to listener
- window.setTimeout(function(){opts.tick(time)}, 0);
+ window.setTimeout(function(){
+ opts.tick({
+ attempts: opts.attempts,
+ cutoff: opts.cutoff,
+ failures: failures,
+ slot_time: opts.slot_time,
+ ticks: ticks
+ })
+ }, 0);
+
// Wait for slot_time
- window.setTimeout(function(){retry_delay(time - 1)}, opts.slot_time);
+ window.setTimeout(function(){retry_delay(ticks - 1)}, opts.slot_time);
}
}
@@ -56,9 +74,10 @@
opts.delay_func = opts.delay_func || DEF_DELAY_FUNC;
// Override error function
- req.error = function(xhr_obj, textStatus, errorThrown){
+ settings.error = function(xhr_obj, textStatus, errorThrown){
+ var can_retry = 0 <= $.inArray(xhr_obj.status, opts.error_codes);
failures++;
- if (failures >= opts.attempts) {
+ if (!can_retry || failures >= opts.attempts) {
// Give up and call the original error function
window.setTimeout(function(){orig_err_func(xhr_obj, textStatus, errorThrown)}, 0);
}
@@ -68,14 +87,24 @@
}
};
- return $.ajax(req);
- };
+ // Save the XHR object for reuse!
+ var xhr = original_ajax_func(settings);
+ settings.xhr = function(){
+ return xhr;
+ };
- ajaxWithRetry.retrySetup = function(opts){
- DEF_OPTS = $.extend(true, DEF_OPTS, opts || {});
- return DEF_OPTS;
- };
+ return xhr;
+ },
+ ajaxRetrySetup = function(opts){
+ DEF_OPTS = $.extend(true, DEF_OPTS, opts);
+ $.ajaxRetrySettings = DEF_OPTS;
+ return DEF_OPTS;
+ };
- $['ajaxWithRetry'] = ajaxWithRetry;
+ $['ajaxRetrySettings'] = DEF_OPTS;
+ $['ajaxRetrySetup'] = ajaxRetrySetup,
+ $['ajax'] = ajaxWithRetry;
})(jQuery);
+// vim: ts=4:sw=4:sts=4:noet:
+
View
@@ -0,0 +1,4 @@
+table {border:none;}
+th {text-align:right;}
+td {text-align:center;}
+#ajax, #ajax-vals, #ajaxRetrySetup {width:100%;}
View
@@ -3,10 +3,7 @@
<head>
<meta charset="utf-8" />
<title>TEST - jQuery Ajax Retry</title>
- <style type="text/css">
- table {border:none;}
- th {text-align:right;}
- </style>
+ <link href="test.css" type="text/css" rel="stylesheet" />
</head>
<body>
<table border="0" cellpadding="0" cellspacing="0">
@@ -23,70 +20,32 @@
<td><input type="text" id="slot_time" value="" /></td>
</tr>
<tr>
- <td colspan="2"><input type="button" id="run" value="Run" /></td>
+ <th><label for="http_code">Http Code:</label></th>
+ <td><select id="http_code"></select></td>
+ </tr>
+ <tr>
+ <td colspan="2">
+ <center>
+ <table border="0" cellpadding="0" cellspacing="0">
+ <tr>
+ <td><input type="button" id="ajax" value="$.ajax()" /></td>
+ <td><input type="button" id="ajax-vals" value="$.ajax({})" /></td>
+ </tr>
+ <tr>
+ <td colspan="2"><input type="button" id="ajaxRetrySetup" value="$.ajaxRetrySetup()" /></td>
+ </tr>
+ </table>
+ </center>
+ </td>
</tr>
</table>
<div id="page"></div>
<script src="http://code.jquery.com/jquery-1.4.2.min.js"></script>
<script src="../src/jquery.ajaxretry.js"></script>
- <script>
- (function($) {
- // Backoff tick callback
- function on_tick(count) {
- var minutes = Math.floor(count / 60),
- seconds = count % 60,
- display = seconds + '';
-
- if (0 < minutes) {
- display = [minutes, '', display].join('');
- }
-
- $('#page').html('<div>' + display + '後にリトライします</div>');
- }
-
- // Error handler
- function on_error() {
- $('#page').html("<div>OnError</div>");
- }
-
- // Success handler
- function on_success(data) {
- $('#page').html("<div>OnSuccess<br />" + data + "</div>");
- }
-
- // Set-up
- $(document).ready(function(){
- var retry_defaults = $.ajaxWithRetry.retrySetup({
- attempts: 30,
- cutoff: 7,
- slot_time: 100,
- });
-
- $('#attempts').val(retry_defaults.attempts);
- $('#cutoff').val(retry_defaults.cutoff);
- $('#slot_time').val(retry_defaults.slot_time);
-
- $('#run').click(function(){
- var retry_opts = {
- attempts: $('#attempts').val(), // Attempts after which to give up
- cutoff: $('#cutoff').val(), // Attempts after which to stop exponentiation
- tick: on_tick, // Tick callback
- slot_time: $('#slot_time').val() // Slot time (duration between ticks)
- };
- $.ajaxWithRetry({
- url: 'this/file/does/not/exist',
- retry: retry_opts,
- error: on_error,
- success: on_success,
- async: true,
- cache: false
- });
- });
- });
- })(jQuery);
- </script>
+ <script src="test.js"></script>
+<!-- vim: set ts=4 sw=4 sts=4 noet :-->
</body>
</html>
Oops, something went wrong.

0 comments on commit 62ba7ba

Please sign in to comment.