Skip to content

Commit

Permalink
Avoid race condition using temporary array.
Browse files Browse the repository at this point in the history
Previously, we'd be missing values between the beforechange event and subsequent
change event. Also, using a temporary array is safer for concurrent requests.
  • Loading branch information
mbostock committed Apr 5, 2012
1 parent d80467e commit e03b80b
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 5 deletions.
5 changes: 3 additions & 2 deletions cubism.js
Expand Up @@ -30,12 +30,13 @@ function cubism_source(context, request) {
function beforechange(start, stop) {
var steps = Math.min(size, Math.round((start - start0) / step));
if (!steps) return; // already fetched this window; ignore it!
values.splice(0, steps);
var temp = values.slice(steps);
steps = Math.min(size, steps + cubism_sourceOverlap);
start0 = start;
request(expression, new Date(stop - steps * step), stop, step, function(error, data) {
if (error) return console.warn(error);
for (var j = 0, i = size - steps, m = data.length; j < m; ++j) values[j + i] = data[j];
for (var j = 0, i = size - steps, m = data.length; j < m; ++j) temp[j + i] = data[j];
values = temp;
event.change.call(metric, start, stop);
});
}
Expand Down

0 comments on commit e03b80b

Please sign in to comment.