db/PadManager.js: more conversion to Promises/async

This commit is contained in:
Ray Bellis 2019-01-28 16:20:30 +00:00
parent 8108964472
commit bbe4a5f756

View file

@ -50,46 +50,43 @@ var globalPads = {
* *
* Updated without db access as new pads are created/old ones removed. * Updated without db access as new pads are created/old ones removed.
*/ */
var padList = { let padList = {
list: [], list: [],
sorted : false, sorted : false,
initiated: false, initiated: false,
init: thenify(function(cb) { init: async function() {
db.findKeys("pad:*", "*:*:*", function(err, dbData) { let dbData = await db.findKeys("pad:*", "*:*:*");
if (ERR(err, cb)) return;
if (dbData != null) { if (dbData != null) {
padList.initiated = true this.initiated = true;
dbData.forEach(function(val) {
padList.addPad(val.replace(/pad:/,""),false);
});
cb && cb(); for (let val of dbData) {
this.addPad(val.replace(/pad:/,""), false);
} }
}); }
return this; return this;
}), },
load: thenify(function(cb) { load: async function() {
if (this.initiated) { if (!this.initiated) {
cb && cb(); return this.init();
} else {
this.init(cb);
} }
}),
return this;
},
/** /**
* Returns all pads in alphabetical order as array. * Returns all pads in alphabetical order as array.
*/ */
getPads: thenify(function(cb) { getPads: async function() {
this.load(function() { await this.load();
if (!padList.sorted) {
padList.list = padList.list.sort();
padList.sorted = true;
}
cb && cb(padList.list); if (!this.sorted) {
}) this.list.sort();
}), this.sorted = true;
}
return this.list;
},
addPad: function(name) { addPad: function(name) {
if (!this.initiated) return; 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) { let padIDs = await padList.getPads();
cb && cb(null, {padIDs: list});
}); return { padIDs };
}); }
// checks if a pad exists // checks if a pad exists
exports.doesPadExist = thenify(function(padId, callback) exports.doesPadExist = thenify(function(padId, callback)