Skip to content

Commit 726bdb7

Browse files
optimization: processing of new position data
* processing large amount of new position will not hang the browser
1 parent 07a3f06 commit 726bdb7

File tree

1 file changed

+73
-61
lines changed

1 file changed

+73
-61
lines changed

js/tracker.js

Lines changed: 73 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,6 @@ var Z_SHADOW = 1000000;
3737
var Z_CAR = 1000001;
3838
var Z_PAYLOAD = 1000002;
3939

40-
var bootstrapped = false;
41-
var zoom_timer;
42-
4340
// localStorage vars
4441
var ls_receivers = false;
4542
var ls_pred = false;
@@ -1578,77 +1575,92 @@ var status = "";
15781575
var bs_idx = 0;
15791576

15801577
function update(response) {
1581-
if (response == null || !response.positions) {
1582-
return;
1583-
}
1584-
1585-
var updated_position = false;
1586-
for (var i = 0; i < response.positions.position.length; i++) {
1587-
var position = response.positions.position[i];
1588-
if (!position.picture) {
1589-
addPosition(position);
1590-
got_positions = true;
1591-
updated_position = true;
1578+
if (response == null
1579+
|| !response.positions
1580+
|| !response.positions.position
1581+
|| !response.positions.position.length) {
1582+
return;
15921583
}
1593-
}
1594-
1595-
if (response.positions.position.length > 0) {
1596-
var position = response.positions.position[response.positions.position.length-1];
1597-
position_id = position.position_id;
1598-
}
15991584

1600-
if (updated_position) {
1601-
// create a dummy response object for postions
1602-
var lastPositions = { positions: { position: [] } };
1603-
var lastPPointer = lastPositions.positions.position;
1604-
1605-
for (var i = 0, ii = vehicle_names.length; i < ii; i++) {
1606-
if(!bootstrapped) {
1607-
setTimeout(function() {
1608-
var idx = bs_idx;
1609-
bs_idx += 1;
1610-
updatePolyline(idx);
1611-
updateVehicleInfo(idx, vehicles[idx].curr_position);
1585+
// create a dummy response object for postions
1586+
var lastPositions = { positions: { position: [] } };
1587+
var ctx_init = {
1588+
positions: response.positions.position,
1589+
lastPositions: lastPositions,
1590+
lastPPointer: lastPositions.positions.position,
1591+
idx: 0,
1592+
max: response.positions.position.length,
1593+
last_vehicle: null,
1594+
step: function(ctx) {
1595+
var draw_idx = -1;
1596+
1597+
var i = ctx.idx;
1598+
var max = i + 5000;
1599+
max = (max >= ctx.max) ? ctx.max : max;
1600+
1601+
for (; i < max ; i++) {
1602+
var row = ctx.positions[i];
1603+
1604+
if(row.position_id > position_id) { position_id = row.position_id; }
1605+
1606+
if (!row.picture) {
1607+
if(ctx.last_vehicle == null) ctx.last_vehicle = row.vehicle;
1608+
1609+
addPosition(row);
1610+
got_positions = true;
1611+
1612+
if(ctx.last_vehicle != row.vehicle) {
1613+
draw_idx = vehicle_names.indexOf(ctx.last_vehicle);
1614+
ctx.last_vehicle = row.vehicle;
1615+
}
1616+
}
1617+
}
16121618

1613-
if(listScroll) listScroll.refresh();
1614-
}, 400*i);
1615-
} else if(vehicles[i].updated) {
1616-
updatePolyline(i);
1619+
ctx.idx = max;
16171620

1618-
if(follow_vehicle == i) {
1619-
panTo(follow_vehicle);
1621+
if(ctx.idx < ctx.max) {
1622+
setTimeout(function() { ctx.step(ctx); }, 4);
1623+
} else {
1624+
ctx.idx = 0;
1625+
ctx.max = vehicle_names.length;
1626+
setTimeout(function() { ctx.draw(ctx); }, 16);
16201627
}
1628+
},
1629+
draw: function(ctx) {
1630+
if(vehicles[ctx.idx].updated) {
1631+
updatePolyline(ctx.idx);
1632+
updateVehicleInfo(ctx.idx, vehicles[ctx.idx].curr_position);
16211633

1622-
updateVehicleInfo(i, vehicles[i].curr_position);
1623-
1624-
// remember last position for each vehicle
1625-
lastPPointer.push(vehicles[i].curr_position);
1626-
}
1627-
}
1634+
// remember last position for each vehicle
1635+
ctx.lastPPointer.push(vehicles[ctx.idx].curr_position);
16281636

1629-
bootstrapped = true;
1637+
if(listScroll) listScroll.refresh();
1638+
if(zoomed_in && follow_vehicle == ctx.idx) panTo(follow_vehicle);
1639+
}
16301640

1631-
// update graph is current vehicles is followed
1632-
if(follow_vehicle != -1 && vehicles[follow_vehicle].graph_data_updated) updateGraph(follow_vehicle, false);
1641+
ctx.idx++;
16331642

1634-
// store in localStorage
1635-
offline.set('positions', lastPositions);
1636-
}
1643+
if(ctx.idx < ctx.max) {
1644+
setTimeout(function() { ctx.draw(ctx); }, 16);
1645+
} else {
1646+
setTimeout(function() { ctx.end(ctx); }, 16);
1647+
}
1648+
},
1649+
end: function(ctx) {
1650+
// update graph is current vehicles is followed
1651+
if(follow_vehicle != -1 && vehicles[follow_vehicle].graph_data_updated) updateGraph(follow_vehicle, false);
16371652

1638-
if (got_positions && !zoomed_in) {
1639-
if(vehicles.length == 0) return;
1653+
// store in localStorage
1654+
offline.set('positions', ctx.lastPositions);
16401655

1641-
zoom_timer = setInterval(function() {
1642-
if(bootstrapped && bs_idx == vehicle_names.length) {
1643-
zoom_on_payload();
1644-
clearInterval(zoom_timer);
1656+
if (got_positions && !zoomed_in && vehicles.length) {
1657+
zoom_on_payload();
1658+
zoomed_in = true;
1659+
}
16451660
}
1646-
},100);
1647-
1648-
zoomed_in = true;
1649-
}
1661+
}
16501662

1651-
if(listScroll) listScroll.refresh();
1663+
ctx_init.step(ctx_init);
16521664
}
16531665

16541666
function zoom_on_payload() {

0 commit comments

Comments
 (0)