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
) {
18 alert(store
.state
.tr
["Database error: stop private browsing, or update your browser"]);
19 callback("error", null);
22 DBOpenRequest
.onsuccess = function(event
) {
23 db
= DBOpenRequest
.result
;
28 DBOpenRequest
.onupgradeneeded = function(event
) {
29 let db
= event
.target
.result
;
30 db
.createObjectStore("compgames", { keyPath: "vname" });
34 export const CompgameStorage
= {
36 dbOperation((err
,db
) => {
39 .transaction("compgames", "readwrite")
40 .objectStore("compgames");
41 objectStore
.add(game
);
45 // obj: move and/or fen
46 update: function(gameId
, obj
) {
47 dbOperation((err
,db
) => {
49 .transaction("compgames", "readwrite")
50 .objectStore("compgames");
51 objectStore
.get(gameId
).onsuccess = function(event
) {
52 // Ignoring error silently: shouldn't happen now. TODO?
53 if (event
.target
.result
) {
54 const game
= event
.target
.result
;
55 Object
.keys(obj
).forEach(k
=> {
56 if (k
== "move") game
.moves
.push(obj
[k
]);
57 else game
[k
] = obj
[k
];
59 objectStore
.put(game
); //save updated data
65 // Retrieve any game from its identifier (variant name)
66 // NOTE: need callback because result is obtained asynchronously
67 get: function(gameId
, callback
) {
68 dbOperation((err
,db
) => {
70 .transaction("compgames", "readonly")
71 .objectStore("compgames");
72 objectStore
.get(gameId
).onsuccess = function(event
) {
73 callback(event
.target
.result
);
78 // Delete a game in indexedDB
79 remove: function(gameId
) {
80 dbOperation((err
,db
) => {
82 db
.transaction("compgames", "readwrite")
83 .objectStore("compgames")