From dabc1b4f54c77310f6c7687e81f8899ca1298597 Mon Sep 17 00:00:00 2001 From: rooklift <16438795+rooklift@users.noreply.github.com> Date: Thu, 18 Nov 2021 15:22:22 +0000 Subject: [PATCH] get_db() --- src/75_looker.js | 35 ++++++++++++++++++++--------------- 1 file changed, 20 insertions(+), 15 deletions(-) diff --git a/src/75_looker.js b/src/75_looker.js index acf43290..48c11581 100644 --- a/src/75_looker.js +++ b/src/75_looker.js @@ -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?) }, // -------------------------------------------------------------------------------------------- @@ -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).