Skip to content

Commit c490091

Browse files
committed
it is impossible to cancel JSONP polling request - comprehend that
1 parent a7769b3 commit c490091

File tree

2 files changed

+24
-2
lines changed

2 files changed

+24
-2
lines changed

lib/trans-jsonp-polling.js

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,16 +68,36 @@ JsonPTransport.prototype.doCleanup = function() {
6868
var jsonPReceiverWrapper = function(url, constructReceiver, user_callback) {
6969
var id = 'a' + utils.random_string(6);
7070
var url_id = url + '?c=' + escape(WPrefix + '.' + id);
71+
72+
// Unfortunately it is not possible to abort loading of the
73+
// script. We need to keep track of frake close frames.
74+
var aborting = 0;
75+
7176
// Callback will be called exactly once.
7277
var callback = function(frame) {
73-
delete _window[WPrefix][id];
74-
user_callback(frame);
78+
switch(aborting) {
79+
case 0:
80+
// Normal behaviour - delete hook _and_ emit message.
81+
delete _window[WPrefix][id];
82+
user_callback(frame);
83+
break;
84+
case 1:
85+
// Fake close frame - emit but don't delete hook.
86+
user_callback(frame);
87+
aborting = 2;
88+
break;
89+
case 2:
90+
// Got frame after connection was closed, delete hook, don't emit.
91+
delete _window[WPrefix][id];
92+
break;
93+
}
7594
};
7695

7796
var close_script = constructReceiver(url_id, callback);
7897
_window[WPrefix][id] = close_script;
7998
var stop = function() {
8099
if (_window[WPrefix][id]) {
100+
aborting = 1;
81101
_window[WPrefix][id](utils.closeFrame(1000, "JSONP user aborted read"));
82102
}
83103
};

lib/trans-jsonp-receiver.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ var jsonPGenericReceiver = function(url, callback) {
2121
}
2222
if (script) {
2323
clearTimeout(tref);
24+
// Unfortunately, you can't really abort script loading of
25+
// the script.
2426
script.parentNode.removeChild(script);
2527
script.onreadystatechange = script.onerror =
2628
script.onload = script.onclick = null;

0 commit comments

Comments
 (0)