Skip to content

Commit

Permalink
get_db()
Browse files Browse the repository at this point in the history
  • Loading branch information
rooklift committed Nov 18, 2021
1 parent 6d76096 commit dabc1b4
Showing 1 changed file with 20 additions and 15 deletions.
35 changes: 20 additions & 15 deletions src/75_looker.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,23 +79,32 @@ let looker_props = {
}
},

lookup: function(db_name, board) {

// When repeatedly called with the same params, this should
// return the same object (unless it changes of course).
get_db: function(db_name) { // Creates it if needed.

if (typeof db_name !== "string" || !this.all_dbs[db_name]) {
if (typeof db_name !== "string") {
return null;
}

let ret = this.all_dbs[db_name][board.fen()];

if (!ret) {
return null;
if (!this.all_dbs[db_name]) {
this.all_dbs[db_name] = Object.create(null);
}

return ret;
return this.all_dbs[db_name];
},

lookup: function(db_name, board) {

// When repeatedly called with the same params, this should
// return the same object (unless it changes of course).

let db = this.get_db(db_name);
if (db) { // Remember get_db() can return null.
let ret = db[board.fen()];
if (ret) {
return ret;
}
}
return null; // I guess we tend to like null over undefined. (Bad habit?)
},

// --------------------------------------------------------------------------------------------
Expand Down Expand Up @@ -132,11 +141,7 @@ let looker_props = {

// Get the correct DB, creating it if needed...

let db = this.all_dbs["chessdbcn"];
if (!db) {
db = Object.create(null);
this.all_dbs["chessdbcn"] = db;
}
let db = this.get_db("chessdbcn");

// Create or recreate the info object. Recreation ensures that the infobox drawer can
// tell that it's a new object if it changes (and a redraw is needed).
Expand Down

0 comments on commit dabc1b4

Please sign in to comment.