Updating tileJson provider to use webworkers to process tiles when they are available. This is probably still not as efficient as it could be as we are not recycling workers.

This commit is contained in:
Stuart Lynn 2015-10-27 15:41:38 -04:00
parent 1bdaed7bcb
commit ccc871a2c3

View File

@ -48,10 +48,39 @@
* Index: Array index to the properties * Index: Array index to the properties
* } * }
*/ */
proccessTile: function(rows, coord, zoom) { createProccessTileWorker:function(){
var workerFunction = "var proccessTile ="+ this.proccessTileSerial.toString()
var wrapper = "; self.onmessage = function(e){var data = JSON.parse(e.data); JSON.stringify(self.postMessage(proccessTile(data.rows,data.coord, data.zoom, data.options)))}"
var script = workerFunction + wrapper;
var blob = new Blob([script], {type: "text/javascript"})
var worker = new Worker(window.URL.createObjectURL(blob))
return worker
},
proccessTile:function(rows,coord,zoom,callback){
if(typeof(Worker) === "undefined"){
callback(this.proccessTileSerial(rows,coord,zoom, this.options))
}
else{
var worker = this.createProccessTileWorker()
worker.onmessage = function(e){
callback(e.data)
worker.terminate()
}
var workerSafeOptions= {
x : new this.options.coordinates_data_type(rows.length),
y : new this.options.coordinates_data_type(rows.length),
cumulative: this.options.cumulative,
valueDataType: this.options.valueDataType,
resolution: this.options.resolution,
}
worker.postMessage(JSON.stringify({rows: rows, coord: {x:coord.x,y:coord.y}, zoom:zoom, options: workerSafeOptions}))
}
},
proccessTileSerial: function(rows, coord, zoom,options) {
var r; var r;
var x = new this.options.coordinates_data_type(rows.length); var x = options.x || new options.coordinates_data_type(rows.length);
var y = new this.options.coordinates_data_type(rows.length); var y = options.y || new options.coordinates_data_type(rows.length);
// count number of dates // count number of dates
var dates = 0; var dates = 0;
@ -64,16 +93,16 @@
} }
} }
if(this.options.cumulative) { if(options.cumulative) {
dates = (1 + maxDateSlots) * rows.length; dates = (1 + maxDateSlots) * rows.length;
} }
var type = this.options.cumulative ? Uint32Array: Uint8ClampedArray; var type = options.cumulative ? Uint32Array: Uint8ClampedArray;
// reserve memory for all the dates // reserve memory for all the dates
var timeIndex = new Int32Array(maxDateSlots + 1); //index-size var timeIndex = new Int32Array(maxDateSlots + 1); //index-size
var timeCount = new Int32Array(maxDateSlots + 1); var timeCount = new Int32Array(maxDateSlots + 1);
var renderData = new (this.options.valueDataType || type)(dates); var renderData = new (options.valueDataType || type)(dates);
var renderDataPos = new Uint32Array(dates); var renderDataPos = new Uint32Array(dates);
var rowsPerSlot = {}; var rowsPerSlot = {};
@ -81,15 +110,15 @@
// precache pixel positions // precache pixel positions
for (var r = 0; r < rows.length; ++r) { for (var r = 0; r < rows.length; ++r) {
var row = rows[r]; var row = rows[r];
x[r] = row.x__uint8 * this.options.resolution; x[r] = row.x__uint8 * options.resolution;
y[r] = row.y__uint8 * this.options.resolution; y[r] = row.y__uint8 * options.resolution;
var dates = row.dates__uint16; var dates = row.dates__uint16;
var vals = row.vals__uint8; var vals = row.vals__uint8;
if (!this.options.cumulative) { if (!options.cumulative) {
for (var j = 0, len = dates.length; j < len; ++j) { for (var j = 0, len = dates.length; j < len; ++j) {
var rr = rowsPerSlot[dates[j]] || (rowsPerSlot[dates[j]] = []); var rr = rowsPerSlot[dates[j]] || (rowsPerSlot[dates[j]] = []);
if(this.options.cumulative) { if(options.cumulative) {
vals[j] += prev_val; vals[j] += prev_val;
} }
prev_val = vals[j]; prev_val = vals[j];
@ -257,7 +286,7 @@
torque.net.get( url , function (data) { torque.net.get( url , function (data) {
if (data && data.responseText) { if (data && data.responseText) {
var rows = JSON.parse(data.responseText); var rows = JSON.parse(data.responseText);
callback(self.proccessTile(rows, coord, zoom)); self.proccessTile(rows, coord, zoom,callback.bind(self));
} else { } else {
callback(null); callback(null);
} }
@ -314,6 +343,7 @@
_fetchMap: function(callback) { _fetchMap: function(callback) {
var self = this; var self = this;
torque.net.get(this.options.tileJSON, function (data) { torque.net.get(this.options.tileJSON, function (data) {
data = JSON.parse(data.response); data = JSON.parse(data.response);
if (data) { if (data) {