655fd6bf8c69838d14c3032fe0d948066a9d0f88
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
= {
41 dbOperation((err
, db
) => {
44 .transaction("compgames", "readwrite")
45 .objectStore("compgames");
46 objectStore
.add(game
);
50 // obj: move and/or fen
51 update: function(gameId
, obj
) {
52 dbOperation((err
, db
) => {
54 .transaction("compgames", "readwrite")
55 .objectStore("compgames");
56 objectStore
.get(gameId
).onsuccess = function(event
) {
57 // Ignoring error silently: shouldn't happen now. TODO?
58 if (event
.target
.result
) {
59 const game
= event
.target
.result
;
60 Object
.keys(obj
).forEach(k
=> {
61 if (k
== "move") game
.moves
.push(obj
[k
]);
62 else game
[k
] = obj
[k
];
64 objectStore
.put(game
); //save updated data
70 // Retrieve any game from its identifier (variant name)
71 // NOTE: need callback because result is obtained asynchronously
72 get: function(gameId
, callback
) {
73 dbOperation((err
, db
) => {
75 .transaction("compgames", "readonly")
76 .objectStore("compgames");
77 objectStore
.get(gameId
).onsuccess = function(event
) {
78 callback(event
.target
.result
);
83 // Delete a game in indexedDB
84 remove: function(gameId
) {
85 dbOperation((err
, db
) => {
87 db
.transaction("compgames", "readwrite")
88 .objectStore("compgames")