no more omit

This commit is contained in:
Bel LaPointe
2021-03-12 07:35:02 -06:00
parent 21ab31b30d
commit bfa51bce7b
4 changed files with 121 additions and 4 deletions

107
src/client/js.js Normal file
View File

@@ -0,0 +1,107 @@
class DB {
constructor() {
if (typeof(localStorage) === "undefined") {
throw new Error('Hmmm... no "localStorage" support');
}
}
get(key) {
b = localStorage.getItem(key);
if (b) {
b = JSON.parse(b);
} else {
b = null;
}
return b
}
set(key, value) {
b = JSON.stringify(value);
localStorage.setItem(key, b);
}
del(key) {
localStorage.removeItem(key);
}
}
class Requests {
put(remote, body, callback) {
this.http("put", remote, callback, body);
}
post(remote, body, callback) {
this.http("post", remote, callback, body);
}
get(remote, callback) {
this.http("get", remote, callback, null);
}
http(method, remote, callback, body) {
var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == XMLHttpRequest.DONE) {
callback(xmlhttp.responseText, xmlhttp.status);
}
};
xmlhttp.open(method, remote, true);
if (typeof body == "undefined") {
body = null;
}
xmlhttp.send(body);
}
}
class Games {
constructor() {
this.db = new DB();
this.requests = new Requests();
}
list(cb) {
this.requests.get("/api/games", (text) => {
var list = JSON.parse(text);
cb(list);
});
}
get(id, cb) {
this.requests.get("/api/games/"+id, (text, status) => {
if (status != 200) {
throw new Error("bad status getting game: "+status+": "+text);
}
var game = JSON.parse(text);
cb(game);
});
}
create(id, cb) {
this.requests.post("/api/games/"+id, null, (text, status) => {
if (status != 200) {
throw new Error("bad status creating game: "+status+": "+text);
}
var game = JSON.parse(text);
cb(game);
});
}
update(id, game, cb) {
this.requests.put("/api/games/"+id, JSON.stringify(game), (text, status) => {
if (status != 200) {
throw new Error("bad status updating game: "+status+": "+text);
}
var game2 = JSON.parse(text);
cb(game2);
});
}
}
games = new Games()
games.create("id", console.log);
games.list((ids) => {
console.log(ids);
ids.forEach((id) => {
games.get(id, console.log);
});
});