From bbe4a5f756ed0150064cad098d36ce964d0b54b1 Mon Sep 17 00:00:00 2001 From: Ray Bellis Date: Mon, 28 Jan 2019 16:20:30 +0000 Subject: [PATCH] db/PadManager.js: more conversion to Promises/async --- src/node/db/PadManager.js | 61 +++++++++++++++++++-------------------- 1 file changed, 29 insertions(+), 32 deletions(-) diff --git a/src/node/db/PadManager.js b/src/node/db/PadManager.js index e478636b..858ce5b4 100644 --- a/src/node/db/PadManager.js +++ b/src/node/db/PadManager.js @@ -50,46 +50,43 @@ var globalPads = { * * Updated without db access as new pads are created/old ones removed. */ -var padList = { +let padList = { list: [], sorted : false, initiated: false, - init: thenify(function(cb) { - db.findKeys("pad:*", "*:*:*", function(err, dbData) { - if (ERR(err, cb)) return; + init: async function() { + let dbData = await db.findKeys("pad:*", "*:*:*"); - if (dbData != null) { - padList.initiated = true - dbData.forEach(function(val) { - padList.addPad(val.replace(/pad:/,""),false); - }); + if (dbData != null) { + this.initiated = true; - cb && cb(); + for (let val of dbData) { + this.addPad(val.replace(/pad:/,""), false); } - }); + } return this; - }), - load: thenify(function(cb) { - if (this.initiated) { - cb && cb(); - } else { - this.init(cb); + }, + load: async function() { + if (!this.initiated) { + return this.init(); } - }), + + return this; + }, /** * Returns all pads in alphabetical order as array. */ - getPads: thenify(function(cb) { - this.load(function() { - if (!padList.sorted) { - padList.list = padList.list.sort(); - padList.sorted = true; - } + getPads: async function() { + await this.load(); - cb && cb(padList.list); - }) - }), + if (!this.sorted) { + this.list.sort(); + this.sorted = true; + } + + return this.list; + }, addPad: function(name) { if (!this.initiated) return; @@ -171,12 +168,12 @@ exports.getPad = thenify(function(id, text, callback) }); }); -exports.listAllPads = thenify(function(cb) +exports.listAllPads = async function() { - padList.getPads(function(list) { - cb && cb(null, {padIDs: list}); - }); -}); + let padIDs = await padList.getPads(); + + return { padIDs }; +} // checks if a pad exists exports.doesPadExist = thenify(function(padId, callback)