Skip to content

Commit

Permalink
Fix interrupted streams handling
Browse files Browse the repository at this point in the history
The embedded webkit instance seems (at least in linux) to trigger the
"onreadystatechange" in the middle of handling data leading to the streaming to
stop because of JSON Parse errors (as described in  Issue #473).

This patch introduces a new variable to store the broken response to prepend it
with the next incoming data forming a proper JSON string.

The stream continues to run, and updates, broken replies just tend to lag a
second due to having to wait for coming data.

This is not a perfect fix but rather a workaround to keep streaming running
(especially important due to API call limits coming).
  • Loading branch information
tante authored and kuromabo committed Apr 10, 2013
1 parent d34680c commit 2b20e97
Showing 1 changed file with 20 additions and 3 deletions.
23 changes: 20 additions & 3 deletions data/js/lib.twitter.js
Expand Up @@ -813,6 +813,11 @@ function TwitterClient() {
'with': 'followings'
};

// since some browsers trigger readystatechange rather randomly we need
// to reassemble broken stream responses
// the var is used to store the intermediate response
var interrupted_response = ''

var signed_params = self.oauth.form_signed_params(
sign_url, self.oauth.access_token, 'GET', params, false);
url = url + '?' + signed_params;
Expand Down Expand Up @@ -865,6 +870,12 @@ function TwitterClient() {
if (callback) {
// @TODO the procedure to process tweets can be simpler.
// because all json objects are complete.

// prepend the unprocessed data from the
// interrupted_response
newText = interrupted_response + newText;
interrupted_response = ''

var lines = newText.split(/[\n\r]/g);
for (var i = 0; i < lines.length; i += 1) {
var line = lines[i].split(/({[^\0]+})/gm);
Expand All @@ -873,8 +884,14 @@ function TwitterClient() {
try {
ret = JSON.parse(line[j]);
} catch(e) {
hotot_log('Streams XHR', e.message + '\n' + line);
return;
// The log is disabled to not trigger
// irritations with users
// hotot_log('Streams XHR', e.message + '\n' + line);
// store the interrupted response for later
// processing
interrupted_response = newText;
// do not quit streaming
//return;
}
try {
callback(ret);
Expand Down Expand Up @@ -921,4 +938,4 @@ function TwitterClient() {
};
};

lib.twitter.Client = TwitterClient;
lib.twitter.Client = TwitterClient;

0 comments on commit 2b20e97

Please sign in to comment.