diff options
author | Marcel Klehr <mklehr@gmx.net> | 2013-10-13 21:20:19 +0200 |
---|---|---|
committer | Marcel Klehr <mklehr@gmx.net> | 2013-10-13 21:20:19 +0200 |
commit | 200f2507172d50084cd8cfe6aae6b25a3045fb09 (patch) | |
tree | c2e9457fa95ddea96cff6f0f49ecb8ddf5383692 /src/node | |
parent | 680d9a28115167b47dc390effce147e78e3b66c6 (diff) | |
download | etherpad-lite-200f2507172d50084cd8cfe6aae6b25a3045fb09.zip |
You don't need the padList if you want to alter it
Don't query the db for caching a list of pads if you only want to alter the list
Diffstat (limited to 'src/node')
-rw-r--r-- | src/node/db/PadManager.js | 69 |
1 files changed, 37 insertions, 32 deletions
diff --git a/src/node/db/PadManager.js b/src/node/db/PadManager.js index 00772608..868137f9 100644 --- a/src/node/db/PadManager.js +++ b/src/node/db/PadManager.js @@ -24,7 +24,9 @@ var Pad = require("../db/Pad").Pad; var db = require("./DB").db; /** - * An Object containing all known Pads. Provides "get" and "set" functions, + * A cache of all loaded Pads. + * + * Provides "get" and "set" functions, * which should be used instead of indexing with brackets. These prepend a * colon to the key, to avoid conflicting with built-in Object methods or with * these functions themselves. @@ -37,44 +39,55 @@ var globalPads = { set: function (name, value) { this[':'+name] = value; - - if(padList.list.length == 0){ // If we haven't populated the padList.list yet - padList.init(); - } - - padList.addPad(name); }, - remove: function (name) { delete this[':'+name]; } + remove: function (name) { + delete this[':'+name]; + } }; +/** + * A cache of the list of all pads. + * + * Updated without db access as new pads are created/old ones removed. + */ var padList = { list: [], sorted : false, - init: function() + initiated: false, + init: function(cb) { db.findKeys("pad:*", "*:*:*", function(err, dbData) { - if(ERR(err)) return; + if(ERR(err, cb)) return; if(dbData != null){ dbData.forEach(function(val){ padList.addPad(val.replace(/pad:/,""),false); }); + padList.initiated = true + cb && cb() } }); return this; }, + load: function(cb) { + if(this.initiated) cb && cb() + else this.init(cb) + }, /** * Returns all pads in alphabetical order as array. */ - getPads: function(){ - if(!this.sorted){ - this.list=this.list.sort(); - this.sorted=true; - } - return this.list; + getPads: function(cb){ + this.load(function() { + if(!padList.sorted){ + padList.list = padList.list.sort(); + padList.sorted = true; + } + cb && cb(padList.list); + }) }, addPad: function(name) { + if(!this.initiated) return; if(this.list.indexOf(name) == -1){ this.list.push(name); this.sorted=false; @@ -82,7 +95,8 @@ var padList = { }, removePad: function(name) { - var index=this.list.indexOf(name); + if(!this.initiated) return; + var index = this.list.indexOf(name); if(index>-1){ this.list.splice(index,1); this.sorted=false; @@ -161,16 +175,11 @@ exports.getPad = function(id, text, callback) } } -exports.listAllPads = function(callback) +exports.listAllPads = function(cb) { - if(padList.list.length == 0){ // If we haven't populated the padList.list yet - padList.init(); - } - if(callback != null){ - callback(null,{padIDs: padList.getPads()}); - }else{ - return {padIDs: padList.getPads()}; - } + padList.getPads(function(list) { + cb && cb(null, {padIDs: list}); + }); } //checks if a pad exists @@ -231,17 +240,13 @@ exports.isValidPadId = function(padId) * Removes the pad from database and unloads it. */ exports.removePad = function(padId){ - if(padList.list.length == 0){ // If we haven't populated the padList.list yet - padList.init(); - } db.remove("pad:"+padId); exports.unloadPad(padId); padList.removePad(padId); } -//removes a pad from the array +//removes a pad from the cache exports.unloadPad = function(padId) { - if(globalPads.get(padId)) - globalPads.remove(padId); + globalPads.remove(padId); } |