From 13360c335b9af0341905f5c0d31b67bb7dc6254b Mon Sep 17 00:00:00 2001 From: Caleb James DeLisle Date: Thu, 11 Feb 2016 16:55:26 +0100 Subject: [PATCH] I'm an idiot - killer off-by-one ate the storage --- storage/lvl.js | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/storage/lvl.js b/storage/lvl.js index 18c604d93..14a091b2b 100644 --- a/storage/lvl.js +++ b/storage/lvl.js @@ -3,19 +3,27 @@ var nThen = require('nthen'); var getIndex = function(db, cName, cb) { db.get(cName+'=>index', function(e, out){ - if (e && !e.notFound) { throw e; } - cb(parseInt(out || 0)); + if (e) { + if (e.notFound) { + cb(-1); + } else { + throw e; + } + return; + } + cb(parseInt(out)); }); }; var insert = function (db, channelName, content, cb) { var index; nThen(function (waitFor) { - getIndex(db, channelName, waitFor(function (i) { index = i; })); - }).nThen(function (waitFor) { - db.put(channelName+'=>index', ''+index, waitFor(function (e) { if (e) { throw e; } })); + getIndex(db, channelName, waitFor(function (i) { index = i+1; })); }).nThen(function (waitFor) { db.put(channelName+'=>'+index, content, waitFor(function (e) { if (e) { throw e; } })); + }).nThen(function (waitFor) { + db.put(channelName+'=>index', ''+index, waitFor(function (e) { if (e) { throw e; } })); + console.log(index); }).nThen(cb); };