-
Notifications
You must be signed in to change notification settings - Fork 5
/
mongo-store.js
118 lines (106 loc) · 4.11 KB
/
mongo-store.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
deathmatch = (window.deathmatch || {});
deathmatch.store = (function() {
function lpad(n,d) { return String(Math.pow(10,n) + d).substring(1); }
/* url (string*), body (string), handler (string->), errorHandler (number->), headers ({:string}) */
function request( options ) {
var xhr = window.ActiveXObject ? new ActiveXObject("Microsoft.XMLHTTP") : new XMLHttpRequest();
xhr.onreadystatechange = function() {
if ( xhr.readyState < 4 ) return;
if ((!xhr.status && (location.protocol == "file:")) || (xhr.status >= 200 && xhr.status < 300)
|| xhr.status == 304 || xhr.status == 1223) {
if (options.handler) options.handler(xhr.responseText, xhr);
} else if (options.errorHandler) options.errorHandler(xhr.status)
};
xhr.open(options.method || 'GET', options.url, true );
if (options.headers) for (var k in options.headers) xhr.setRequestHeader(k, options.headers[k]);
if (("POST" === options.method) && (( ! options.headers ) || ( ! options.headers['Content-type'] )))
xhr.setRequestHeader('Content-type','application/x-www-form-urlencoded');
xhr.send( options.body || null );
}
function loadSimulation(name, handler /* err, simulation, generation */ ) {
var simulation, generation, complete = false;
request({url:'/simulations/'+encodeURIComponent(name),
handler: function(o) { done( simulation = JSON.parse(o) ); },
errorHandler: errorHandler('simulation') });
request({url:'/latest-generation/'+encodeURIComponent(name),
handler: function(o) { done( generation = JSON.parse(o) ); },
errorHandler: errorHandler("generation") });
function done() {
if (simulation && generation) {
simulation.index = generation.index;
handler(null, simulation, generation);
complete = true;
}
}
function errorHandler(type) {
return function(status) {
if ( ! complete )
handler(type+" not found");
complete = true;
}
}
}
function loadGeneration(name, generationIndex, handler /* err, simulation, generation */ ) {
var simulation, generation, complete = false;
request({url:'/simulations/'+encodeURIComponent(name),
handler: function(o) { done( simulation = JSON.parse(o) ); },
errorHandler: errorHandler('simulation') });
request({url:'/generations/'+createKey(name, generationIndex),
handler: function(o) { done( generation = JSON.parse(o) ); },
errorHandler: errorHandler("generation") });
function done() {
if (simulation && generation) {
simulation.index = generation.index;
handler(null, simulation, generation);
complete = true;
}
}
function errorHandler(type) {
return function(status) {
if ( ! complete )
handler(type+" not found");
complete = true;
}
}
}
function aggregateGenerations( query, handler ) {
request({
url:'/generations/aggregate',
headers: {'Content-type':'application/json'},
method: 'POST',
body: JSON.stringify(query),
handler: function(response) { handler( JSON.parse(response) ) },
errorHandler: handler
})
}
function createKey( simulationName, generationIndex ) {
return encodeURIComponent(simulationName)+'-'+lpad(7,generationIndex)
}
function saveGeneration( simulation, generation ) {
var key = createKey( simulation.name, simulation.index );
generation.simulation = simulation.name;
generation.index = simulation.index;
request({
method:'PUT',
url:'/generations/'+key,
headers: { 'Content-type':'application/json' },
body: JSON.stringify(generation)
})
}
function saveSimulation( simulation ) {
var key = encodeURIComponent(simulation.name);
request({
method:'PUT',
url:'/simulations/'+key,
headers: { 'Content-type':'application/json' },
body: JSON.stringify(simulation)
})
}
return {
loadSimulation : loadSimulation,
loadGeneration : loadGeneration,
saveSimulation : saveSimulation,
saveGeneration : saveGeneration,
aggregateGenerations : aggregateGenerations,
}
})();