From: Jakub Narebski <jnareb@gmail.com>
To: git@vger.kernel.org
Cc: John 'Warthog9' Hawley <warthog9@kernel.org>,
Petr Baudis <pasky@suse.cz>, Fredrik Kuivinen <frekui@gmail.com>,
Giuseppe Bilotta <giuseppe.bilotta@gmail.com>,
Luben Tuikov <ltuikov@yahoo.com>,
Martin Koegler <mkoegler@auto.tuwien.ac.at>,
Jakub Narebski <jnareb@gmail.com>
Subject: [PATCH 3/3] gitweb.js: use setTimeout rather than setInterval in blame_incremental.js
Date: Fri, 27 May 2011 15:50:01 +0200 [thread overview]
Message-ID: <1306504201-18014-4-git-send-email-jnareb@gmail.com> (raw)
In-Reply-To: <1306504201-18014-1-git-send-email-jnareb@gmail.com>
If there is a possibility that your logic could take longer to execute
than the interval time, it is recommended that you recursively call a
named function using window.setTimeout rather than window.setInterval.
Therefore instead of using setInterval as an alternate way of invoking
handleResponse (because some web browsers call onreadystatechange only
once per each distinct state, and not for each server flush), use
setTimeout and reset it from handleResponse. As a bonus this allows
us to get rid of timer if it turns out that web browser calls
onreadystatechange on each server flush.
While at it get rid of `xhr' global variable, creating it instead as
local variable in startBlame and passing it as parameter, and of
`pollTimer' global variable, passing it as member of xhr object
(xhr.pollTimer).
Signed-off-by: Jakub Narebski <jnareb@gmail.com>
---
This patch introduces three separate features, and probably should be
split into separate patches.
gitweb/static/js/blame_incremental.js | 55 ++++++++++++++++++++++++---------
1 files changed, 40 insertions(+), 15 deletions(-)
diff --git a/gitweb/static/js/blame_incremental.js b/gitweb/static/js/blame_incremental.js
index 27955ec..91354a0 100644
--- a/gitweb/static/js/blame_incremental.js
+++ b/gitweb/static/js/blame_incremental.js
@@ -29,7 +29,6 @@
/* ............................................................ */
/* utility/helper functions (and variables) */
-var xhr; // XMLHttpRequest object
var projectUrl; // partial query + separator ('?' or ';')
// 'commits' is an associative map. It maps SHA1s to Commit objects.
@@ -431,8 +430,6 @@ var endRe = /^END ?([^ ]*) ?(.*)/;
var curCommit = new Commit();
var curGroup = {};
-var pollTimer = null;
-
/**
* Parse output from 'git blame --incremental [...]', received via
* XMLHttpRequest from server (blamedataUrl), and call handleLine
@@ -533,26 +530,34 @@ function processData(unprocessed, nextReadPos) {
* Handle XMLHttpRequest errors
*
* @param {XMLHttpRequest} xhr: XMLHttpRequest object
+ * @param {Number} [xhr.pollTimer] ID of the timeout to clear
*
- * @globals pollTimer, commits
+ * @globals commits
*/
function handleError(xhr) {
errorInfo('Server error: ' +
xhr.status + ' - ' + (xhr.statusText || 'Error contacting server'));
- clearInterval(pollTimer);
+ if (typeof xhr.pollTimer === "number") {
+ clearTimeout(xhr.pollTimer);
+ delete xhr.pollTimer;
+ }
commits = {}; // free memory
}
/**
* Called after XMLHttpRequest finishes (loads)
*
- * @param {XMLHttpRequest} xhr: XMLHttpRequest object (unused)
+ * @param {XMLHttpRequest} xhr: XMLHttpRequest object
+ * @param {Number} [xhr.pollTimer] ID of the timeout to clear
*
- * @globals pollTimer, commits
+ * @globals commits
*/
function responseLoaded(xhr) {
- clearInterval(pollTimer);
+ if (typeof xhr.pollTimer === "number") {
+ clearTimeout(xhr.pollTimer);
+ delete xhr.pollTimer;
+ }
fixColorsAndGroups();
writeTimeInterval();
@@ -563,9 +568,13 @@ function responseLoaded(xhr) {
* handler for XMLHttpRequest onreadystatechange event
* @see startBlame
*
- * @globals xhr
+ * @param {XMLHttpRequest} xhr: XMLHttpRequest object
+ * @param {Number} xhr.prevDataLength: previous value of xhr.responseText.length
+ * @param {Number} xhr.nextReadPos: start of unread part of xhr.responseText
+ * @param {Number} [xhr.pollTimer] ID of the timeout (to reset or cancel)
+ * @param {Boolean} fromTimer: if handler was called from timer
*/
-function handleResponse() {
+function handleResponse(xhr, fromTimer) {
/*
* xhr.readyState
@@ -614,6 +623,19 @@ function handleResponse() {
// did we finish work?
if (xhr.readyState === 4) {
responseLoaded(xhr);
+ return;
+ }
+
+ // if we get from timer, we have to restart it
+ // otherwise onreadystatechange gives us partial response, timer not needed
+ if (fromTimer) {
+ setTimeout(function () {
+ handleResponse(xhr, true);
+ }, 1000);
+
+ } else if (typeof xhr.pollTimer === "number") {
+ clearTimeout(xhr.pollTimer);
+ delete xhr.pollTimer;
}
}
@@ -629,11 +651,11 @@ function handleResponse() {
* Called from 'blame_incremental' view after loading table with
* file contents, a base for blame view.
*
- * @globals xhr, t0, projectUrl, div_progress_bar, totalLines, pollTimer
+ * @globals t0, projectUrl, div_progress_bar, totalLines
*/
function startBlame(blamedataUrl, bUrl) {
- xhr = createRequestObject();
+ var xhr = createRequestObject();
if (!xhr) {
errorInfo('ERROR: XMLHttpRequest not supported');
return;
@@ -652,8 +674,9 @@ function startBlame(blamedataUrl, bUrl) {
xhr.prevDataLength = -1; // used to detect if we have new data
xhr.nextReadPos = 0; // where unread part of response starts
- xhr.onreadystatechange = handleResponse;
- //xhr.onreadystatechange = function () { handleResponse(xhr); };
+ xhr.onreadystatechange = function () {
+ handleResponse(xhr, false);
+ };
xhr.open('GET', blamedataUrl);
xhr.setRequestHeader('Accept', 'text/plain');
@@ -661,7 +684,9 @@ function startBlame(blamedataUrl, bUrl) {
// not all browsers call onreadystatechange event on each server flush
// poll response using timer every second to handle this issue
- pollTimer = setInterval(xhr.onreadystatechange, 1000);
+ xhr.pollTimer = setTimeout(function () {
+ handleResponse(xhr, true);
+ }, 1000);
}
/* end of blame_incremental.js */
--
1.7.5
next prev parent reply other threads:[~2011-05-27 13:50 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
2011-05-27 13:49 [PATCH 0/3] gitweb: Improving blame_incremental.js Jakub Narebski
2011-05-27 13:49 ` [PATCH 1/3] gitweb.js: No need for inProgress in blame_incremental.js Jakub Narebski
2011-05-27 13:50 ` [PATCH 2/3] gitweb.js: No need for loop in blame_incremental's handleResponse() Jakub Narebski
2011-05-27 13:50 ` Jakub Narebski [this message]
2011-05-27 14:04 ` [PATCH 0/3] gitweb: Improving blame_incremental.js Jakub Narebski
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=1306504201-18014-4-git-send-email-jnareb@gmail.com \
--to=jnareb@gmail.com \
--cc=frekui@gmail.com \
--cc=git@vger.kernel.org \
--cc=giuseppe.bilotta@gmail.com \
--cc=ltuikov@yahoo.com \
--cc=mkoegler@auto.tuwien.ac.at \
--cc=pasky@suse.cz \
--cc=warthog9@kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).