From 30bee6504ed8498917c53dbca234ce9916c05f9e Mon Sep 17 00:00:00 2001 From: ansuz Date: Mon, 26 Oct 2015 22:25:14 -0400 Subject: [PATCH] amnesia.js : put the 'db' and closures inside the the module's 'create' method, in case we ever want to call it twice, for some reason --- storage/amnesia.js | 54 +++++++++++++++++++++------------------------- 1 file changed, 25 insertions(+), 29 deletions(-) diff --git a/storage/amnesia.js b/storage/amnesia.js index 68a79c6e7..a25281c81 100644 --- a/storage/amnesia.js +++ b/storage/amnesia.js @@ -1,5 +1,3 @@ -console.log("Loading amnesiadb. This is a horrible idea in production, as data *will not* persist\n"); - /* As the log statement says, this module does nothing to persist your data across sessions. If your process crashes for any reason, all pads will die. @@ -14,34 +12,32 @@ console.log("Loading amnesiadb. This is a horrible idea in production, as data * Enjoy! */ -var db=[], - index=0; - -var insert = function(channelName, content, cb){ - var val = { - id:index++, - chan: channelName, - msg: content, - time: new Date().getTime(), - }; - db.push(val); - cb(); -}; - -var getMessages = function(channelName, cb){ - db.sort(function(a,b){ - return a.id - b.id; - }); - db.filter(function(val){ - return val.chan == channelName; - }).forEach(function(doc){ - cb(doc.msg); - }); -}; - module.exports.create = function(conf, cb){ + console.log("Loading amnesiadb. This is a horrible idea in production,"+ + "as data *will not* persist\n"); + + var db=[], + index=0; cb({ - message: insert, - getMessages: getMessages, + message: function(channelName, content, cb){ + var val = { + id:index++, + chan: channelName, + msg: content, + time: new Date().getTime(), + }; + db.push(val); + cb(); + }, + getMessages: function(channelName, cb){ + db.sort(function(a,b){ + return a.id - b.id; + }); + db.filter(function(val){ + return val.chan == channelName; + }).forEach(function(doc){ + cb(doc.msg); + }); + }, }); };