Skip to content

Commit

Permalink
Merge branch 'dev-master' into dev-3.x
Browse files Browse the repository at this point in the history
  • Loading branch information
tripp committed Sep 22, 2014
2 parents 61fe90c + 6c88ea0 commit 517363d
Show file tree
Hide file tree
Showing 11 changed files with 120 additions and 24 deletions.
2 changes: 1 addition & 1 deletion build/jsonp-url/jsonp-url-coverage.js

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions build/jsonp-url/jsonp-url-debug.js
Expand Up @@ -67,6 +67,7 @@ Y.mix(JSONPRequest.prototype, {
})
.replace(/\[(\d+)\]/g,
function (x, $1) {
/*jslint bitwise: true */
keys[i] = parseInt($1, 10) | 0;
return '.@' + (i++);
})
Expand Down
1 change: 1 addition & 0 deletions build/jsonp-url/jsonp-url.js
Expand Up @@ -67,6 +67,7 @@ Y.mix(JSONPRequest.prototype, {
})
.replace(/\[(\d+)\]/g,
function (x, $1) {
/*jslint bitwise: true */
keys[i] = parseInt($1, 10) | 0;
return '.@' + (i++);
})
Expand Down
4 changes: 2 additions & 2 deletions build/jsonp/jsonp-coverage.js

Large diffs are not rendered by default.

32 changes: 25 additions & 7 deletions build/jsonp/jsonp-debug.js
@@ -1,5 +1,6 @@
YUI.add('jsonp', function (Y, NAME) {

/*jslint maxlen: 350 */
var isFunction = Y.Lang.isFunction;

/**
Expand Down Expand Up @@ -84,6 +85,8 @@ JSONPRequest.prototype = {
*/
this._timeouts = {};

this._failures = {};

// Accept a function, an object, or nothing
callback = (isFunction(callback)) ?
{ on: { success: callback } } :
Expand Down Expand Up @@ -143,6 +146,9 @@ JSONPRequest.prototype = {
if (self._timeouts[proxy] === undefined) {
self._timeouts[proxy] = 0;
}
if (self._failures[proxy] === undefined) {
self._failures[proxy] = 0;
}
self._requests[proxy]++;

Y.log('sending ' + proxy);
Expand All @@ -155,7 +161,7 @@ JSONPRequest.prototype = {
return self;
}

function wrap(fn, isTimeout) {
function wrap(fn, isTimeout, isFailure) {
return (isFunction(fn)) ?
function (data) {
var execute = true,
Expand All @@ -168,18 +174,30 @@ JSONPRequest.prototype = {
if (isTimeout) {
++self._timeouts[proxy];
--self._requests[proxy];
Y.log(proxy + ' timed out - timeouts(' + self._timeouts[proxy] + ') requests(' + self._requests[proxy] + ')');
Y.log(proxy + ' timed out - timeouts(' + self._timeouts[proxy] + ') failures(' + self._failures[proxy] + ') requests(' + self._requests[proxy] + ')');
} else if (isFailure) {
++self._failures[proxy];
if (self._timeouts[proxy] > 0) {
--self._timeouts[proxy];
} else {
--self._requests[proxy];
}
Y.log(proxy + ' failure - timeouts(' + self._timeouts[proxy] + ') failures(' + self._failures[proxy] + ') requests(' + self._requests[proxy] + ')');
} else {
if (!self._requests[proxy]) {
execute = false;
counter = '_timeouts';
if (self._timeouts[proxy] > 0) {
counter = '_timeouts';
} else if (self._failures[proxy] > 0) {
counter = '_failures';
}
}
--self[counter][proxy];
Y.log(proxy + ' response received - timeouts(' + self._timeouts[proxy] + ') requests(' + self._requests[proxy] + ')');
Y.log(proxy + ' response received - timeouts(' + self._timeouts[proxy] + ') failures(' + self._failures[proxy] + ') requests(' + self._requests[proxy] + ')');
}
//}

if (!self._requests[proxy] && !self._timeouts[proxy]) {
if (!self._requests[proxy] && !self._timeouts[proxy] && !self._failures[proxy]) {
Y.log('deleting ' + proxy);
delete YUI.Env.JSONP[proxy];
}
Expand All @@ -199,8 +217,8 @@ JSONPRequest.prototype = {
// be made non-blocking by just calling execute() on the transaction.
// https://github.com/yui/yui3/pull/393#issuecomment-11961608
Y.Get.js(url, {
onFailure : wrap(config.on.failure),
onTimeout : wrap(config.on.timeout, true),
onFailure : wrap(config.on.failure, false, true),
onTimeout : wrap(config.on.timeout, true, false),
timeout : config.timeout,
charset : config.charset,
attributes: config.attributes,
Expand Down
2 changes: 1 addition & 1 deletion build/jsonp/jsonp-min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

27 changes: 22 additions & 5 deletions build/jsonp/jsonp.js
@@ -1,5 +1,6 @@
YUI.add('jsonp', function (Y, NAME) {

/*jslint maxlen: 350 */
var isFunction = Y.Lang.isFunction;

/**
Expand Down Expand Up @@ -84,6 +85,8 @@ JSONPRequest.prototype = {
*/
this._timeouts = {};

this._failures = {};

// Accept a function, an object, or nothing
callback = (isFunction(callback)) ?
{ on: { success: callback } } :
Expand Down Expand Up @@ -143,6 +146,9 @@ JSONPRequest.prototype = {
if (self._timeouts[proxy] === undefined) {
self._timeouts[proxy] = 0;
}
if (self._failures[proxy] === undefined) {
self._failures[proxy] = 0;
}
self._requests[proxy]++;


Expand All @@ -153,7 +159,7 @@ JSONPRequest.prototype = {
return self;
}

function wrap(fn, isTimeout) {
function wrap(fn, isTimeout, isFailure) {
return (isFunction(fn)) ?
function (data) {
var execute = true,
Expand All @@ -166,16 +172,27 @@ JSONPRequest.prototype = {
if (isTimeout) {
++self._timeouts[proxy];
--self._requests[proxy];
} else if (isFailure) {
++self._failures[proxy];
if (self._timeouts[proxy] > 0) {
--self._timeouts[proxy];
} else {
--self._requests[proxy];
}
} else {
if (!self._requests[proxy]) {
execute = false;
counter = '_timeouts';
if (self._timeouts[proxy] > 0) {
counter = '_timeouts';
} else if (self._failures[proxy] > 0) {
counter = '_failures';
}
}
--self[counter][proxy];
}
//}

if (!self._requests[proxy] && !self._timeouts[proxy]) {
if (!self._requests[proxy] && !self._timeouts[proxy] && !self._failures[proxy]) {
delete YUI.Env.JSONP[proxy];
}

Expand All @@ -194,8 +211,8 @@ JSONPRequest.prototype = {
// be made non-blocking by just calling execute() on the transaction.
// https://github.com/yui/yui3/pull/393#issuecomment-11961608
Y.Get.js(url, {
onFailure : wrap(config.on.failure),
onTimeout : wrap(config.on.timeout, true),
onFailure : wrap(config.on.failure, false, true),
onTimeout : wrap(config.on.timeout, true, false),
timeout : config.timeout,
charset : config.charset,
attributes: config.attributes,
Expand Down
2 changes: 1 addition & 1 deletion src/jsonp/HISTORY.md
Expand Up @@ -4,7 +4,7 @@ JSONP Change History
@VERSION@
------

* No changes.
* Resolve "Uncaught TypeError: undefined is not a function" error when both *timeout* and *failure* are defined. Fixes #1817 [stanleyhlng]

3.17.2
------
Expand Down
1 change: 1 addition & 0 deletions src/jsonp/js/jsonp-url.js
Expand Up @@ -65,6 +65,7 @@ Y.mix(JSONPRequest.prototype, {
})
.replace(/\[(\d+)\]/g,
function (x, $1) {
/*jslint bitwise: true */
keys[i] = parseInt($1, 10) | 0;
return '.@' + (i++);
})
Expand Down
32 changes: 25 additions & 7 deletions src/jsonp/js/jsonp.js
@@ -1,3 +1,4 @@
/*jslint maxlen: 350 */
var isFunction = Y.Lang.isFunction;

/**
Expand Down Expand Up @@ -82,6 +83,8 @@ JSONPRequest.prototype = {
*/
this._timeouts = {};

this._failures = {};

// Accept a function, an object, or nothing
callback = (isFunction(callback)) ?
{ on: { success: callback } } :
Expand Down Expand Up @@ -141,6 +144,9 @@ JSONPRequest.prototype = {
if (self._timeouts[proxy] === undefined) {
self._timeouts[proxy] = 0;
}
if (self._failures[proxy] === undefined) {
self._failures[proxy] = 0;
}
self._requests[proxy]++;

Y.log('sending ' + proxy);
Expand All @@ -153,7 +159,7 @@ JSONPRequest.prototype = {
return self;
}

function wrap(fn, isTimeout) {
function wrap(fn, isTimeout, isFailure) {
return (isFunction(fn)) ?
function (data) {
var execute = true,
Expand All @@ -166,18 +172,30 @@ JSONPRequest.prototype = {
if (isTimeout) {
++self._timeouts[proxy];
--self._requests[proxy];
Y.log(proxy + ' timed out - timeouts(' + self._timeouts[proxy] + ') requests(' + self._requests[proxy] + ')');
Y.log(proxy + ' timed out - timeouts(' + self._timeouts[proxy] + ') failures(' + self._failures[proxy] + ') requests(' + self._requests[proxy] + ')');
} else if (isFailure) {
++self._failures[proxy];
if (self._timeouts[proxy] > 0) {
--self._timeouts[proxy];
} else {
--self._requests[proxy];
}
Y.log(proxy + ' failure - timeouts(' + self._timeouts[proxy] + ') failures(' + self._failures[proxy] + ') requests(' + self._requests[proxy] + ')');
} else {
if (!self._requests[proxy]) {
execute = false;
counter = '_timeouts';
if (self._timeouts[proxy] > 0) {
counter = '_timeouts';
} else if (self._failures[proxy] > 0) {
counter = '_failures';
}
}
--self[counter][proxy];
Y.log(proxy + ' response received - timeouts(' + self._timeouts[proxy] + ') requests(' + self._requests[proxy] + ')');
Y.log(proxy + ' response received - timeouts(' + self._timeouts[proxy] + ') failures(' + self._failures[proxy] + ') requests(' + self._requests[proxy] + ')');
}
//}

if (!self._requests[proxy] && !self._timeouts[proxy]) {
if (!self._requests[proxy] && !self._timeouts[proxy] && !self._failures[proxy]) {
Y.log('deleting ' + proxy);
delete YUI.Env.JSONP[proxy];
}
Expand All @@ -197,8 +215,8 @@ JSONPRequest.prototype = {
// be made non-blocking by just calling execute() on the transaction.
// https://github.com/yui/yui3/pull/393#issuecomment-11961608
Y.Get.js(url, {
onFailure : wrap(config.on.failure),
onTimeout : wrap(config.on.timeout, true),
onFailure : wrap(config.on.failure, false, true),
onTimeout : wrap(config.on.timeout, true, false),
timeout : config.timeout,
charset : config.charset,
attributes: config.attributes,
Expand Down
40 changes: 40 additions & 0 deletions src/jsonp/tests/unit/assets/jsonp-tests.js
Expand Up @@ -310,6 +310,46 @@ suite.add(new Y.Test.Case({
}
}));

suite.add(new Y.Test.Case({
name : "Should not have TypeError when both `timeout` and `failure` handlers are set.",

"Having timeout and failure handles should not cause a TypeError": function () {
var error = false,
windowOnError = window.onerror,
jsonp;

window.onerror = function(e) {
error = e;
};

jsonp = new Y.JSONPRequest('echo/jsonp?wait=2&callback={callback}', {
timeout: 1,
on: {
success: function (data) {
//success logic
},
timeout: function () {
//timeout logic
},
failure: function() {
//failure logic
}
}
});

jsonp.send();

this.wait(function() {
window.onerror = windowOnError;
if(error) {
Y.Assert.fail(error);
} else {
Y.Assert.isTrue(true);
}
}, 3000);
}
}));

Y.Test.Runner.add(suite);

}, '@VERSION@' ,{requires:['jsonp', 'test'] });

0 comments on commit 517363d

Please sign in to comment.