You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
49 lines
1.5 KiB
JavaScript
49 lines
1.5 KiB
JavaScript
var Level = require("level");
|
|
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));
|
|
});
|
|
};
|
|
|
|
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; } }));
|
|
}).nThen(function (waitFor) {
|
|
db.put(channelName+'=>'+index, content, waitFor(function (e) { if (e) { throw e; } }));
|
|
}).nThen(cb);
|
|
};
|
|
|
|
var getMessages = function (db, channelName, msgHandler) {
|
|
var index;
|
|
nThen(function (waitFor) {
|
|
getIndex(db, channelName, waitFor(function (i) { index = i; }));
|
|
}).nThen(function (waitFor) {
|
|
var again = function (i) {
|
|
db.get(channelName + '=>' + i, waitFor(function (e, out) {
|
|
if (e) { throw e; }
|
|
msgHandler(out);
|
|
if (i < index) { again(i+1); }
|
|
}));
|
|
};
|
|
again(0);
|
|
});
|
|
};
|
|
|
|
module.exports.create = function (conf, cb) {
|
|
var db = Level(conf.levelPath || './test.level.db');
|
|
cb({
|
|
message: function (channelName, content, cb) {
|
|
insert(db, channelName, content, cb);
|
|
},
|
|
getMessages: function (channelName, msgHandler) {
|
|
getMessages(db, channelName, msgHandler);
|
|
}
|
|
});
|
|
};
|