1 // (Comp)Game object: {
2 // // Static informations:
3 // vname: string (this is the ID)
6 // // Game (dynamic) state:
8 // moves: array of Move objects,
11 import { store
} from "@/store";
13 function dbOperation(callback
) {
15 let DBOpenRequest
= window
.indexedDB
.open("vchess_comp", 4);
17 DBOpenRequest
.onerror = function(event
) {
19 "Database error: stop private browsing, or update your browser"]);
20 callback("error", null);
23 DBOpenRequest
.onsuccess = function(event
) {
24 db
= DBOpenRequest
.result
;
29 DBOpenRequest
.onupgradeneeded = function(event
) {
30 let db
= event
.target
.result
;
31 let upgradeTransaction
= event
.target
.transaction
;
32 if (!db
.objectStoreNames
.contains("compgames"))
33 db
.createObjectStore("compgames", { keyPath: "vname" });
35 upgradeTransaction
.objectStore("compgames");
39 export const CompgameStorage
= {
42 dbOperation((err
, db
) => {
45 .transaction("compgames", "readwrite")
46 .objectStore("compgames");
47 objectStore
.add(game
);
51 // obj: move and/or fen
52 update: function(gameId
, obj
) {
53 dbOperation((err
, db
) => {
55 .transaction("compgames", "readwrite")
56 .objectStore("compgames");
57 objectStore
.get(gameId
).onsuccess = function(event
) {
58 // Ignoring error silently: shouldn't happen now. TODO?
59 if (!!event
.target
.result
) {
60 const game
= event
.target
.result
;
61 Object
.keys(obj
).forEach(k
=> {
62 if (k
== "move") game
.moves
.push(obj
[k
]);
63 else game
[k
] = obj
[k
];
65 objectStore
.put(game
); //save updated data
71 // Retrieve any game from its identifier (variant name)
72 // NOTE: need callback because result is obtained asynchronously
73 get: function(gameId
, callback
) {
74 dbOperation((err
, db
) => {
76 .transaction("compgames", "readonly")
77 .objectStore("compgames");
78 objectStore
.get(gameId
).onsuccess = function(event
) {
79 callback(event
.target
.result
);
84 // Delete a game in indexedDB
85 remove: function(gameId
) {
86 dbOperation((err
, db
) => {
88 db
.transaction("compgames", "readwrite")
89 .objectStore("compgames")