forked from glenrobertson/leaflet-tilelayer-geojson
-
Notifications
You must be signed in to change notification settings - Fork 5
/
CommunistWorker.js
65 lines (57 loc) · 1.93 KB
/
CommunistWorker.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
L.CommunistWorker = L.AbstractWorker.extend({
statics: {
// number of web workers, not using web workers when falsy
NUM_WORKERS: 2
},
initialize: function (workerFunc) {
this.workerFunc = workerFunc;
},
onAdd: function (map) {
this._workers = L.CommunistWorker.createWorkers(this.workerFunc);
},
onRemove: function (map) {
if (this._workers) {
// TODO do not close when other layers are still using the static instance
//this._workers.close();
}
},
process: function(tile, callback) {
if (this._workers){
tile._worker = this._workers.data(tile.datum).then(function(parsed) {
if (tile._worker) {
tile._worker = null;
tile.parsed = parsed;
tile.datum = null;
callback(null, tile);
} else {
// tile has been unloaded, don't continue with adding
//console.log('worker aborted ' + tile.key);
}
});
} else {
callback(null, tile);
}
},
abort: function(tile) {
if (tile._worker) {
// TODO abort worker, would need to recreate after close
//tile._worker.close();
tile._worker = null;
}
}
});
L.communistWorker = function (workerFunc) {
return new L.CommunistWorker(workerFunc);
};
L.extend(L.CommunistWorker, {
createWorkers: function(workerFunc) {
if ( L.CommunistWorker.NUM_WORKERS && typeof Worker === "function" && typeof communist === "function"
&& !("workers" in L.CommunistWorker)) {
L.CommunistWorker.workers = communist({
//data : L.TileLayer.Vector.parseData
data : workerFunc
}, L.CommunistWorker.NUM_WORKERS);
}
return L.CommunistWorker.workers;
}
});