-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathterraformer-geostore-memory.js
78 lines (68 loc) · 2.06 KB
/
terraformer-geostore-memory.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
66
67
68
69
70
71
72
73
74
75
76
77
78
(function (root, factory) {
// Node.
if(typeof module === 'object' && typeof module.exports === 'object') {
exports = module.exports = factory();
}
// Browser Global.
if(typeof root.navigator === "object") {
if (!root.Terraformer){
throw new Error("Terraformer.GeoStore.Memory requires the core Terraformer library. https://github.com/esri/Terraformer");
}
if (!root.Terraformer.GeoStore){
throw new Error("Terraformer.GeoStore.Memory requires the Terraformer GeoStore library. https://github.com/esri/terraformer-geostore");
}
root.Terraformer.GeoStore.Memory = factory(root.Terraformer).Memory;
}
}(this, function() {
var exports = { };
// These methods get called in context of the geostore
function Memory(){
this.data = {};
}
// store the data at id returns true if stored successfully
Memory.prototype.add = function(geojson, callback){
if(geojson.type === "FeatureCollection"){
for (var i = 0; i < geojson.features.length; i++) {
this.data[geojson.features[i].id] = geojson.features[i];
}
} else {
this.data[geojson.id] = geojson;
}
if (callback) {
callback(null, geojson);
}
};
// remove the data from the index and data with id returns true if removed successfully.
Memory.prototype.remove = function(id, callback){
delete this.data[id];
if (callback) {
callback(null, id);
}
};
// return the data stored at id
Memory.prototype.get = function(id, callback){
if (callback) {
callback(null, this.data[id]);
}
};
Memory.prototype.update = function(geojson, callback){
this.data[geojson.id] = geojson;
if (callback) {
callback(null, geojson);
}
};
Memory.prototype.serialize = function(callback){
var data = JSON.stringify(this.data);
if (callback) {
callback(null, data);
}
};
Memory.prototype.deserialize = function(serializedStore, callback){
this.data = JSON.parse(serializedStore);
if (callback) {
callback();
}
};
exports.Memory = Memory;
return exports;
}));