refactor: db.

This commit is contained in:
Christopher Jeffrey 2016-09-20 22:26:47 -07:00
parent d78151d3d3
commit b616d75128
No known key found for this signature in database
GPG Key ID: 8962AB9DE6666BBD
4 changed files with 24 additions and 40 deletions

View File

@ -153,11 +153,8 @@ LowlevelUp.prototype.get = function get(key, options) {
assert(this.loaded, 'Cannot use database before it is loaded.');
if (!options)
options = {};
return new Promise(function(resolve, reject) {
self.binding.get(key, options, function(err, result) {
self.binding.get(key, options || {}, function(err, result) {
if (err) {
if (isNotFound(err))
return resolve();
@ -216,7 +213,7 @@ LowlevelUp.prototype.batch = function batch(ops, options) {
return new Batch(this);
return new Promise(function(resolve, reject) {
self.binding.batch(ops, options, P(resolve, reject));
self.binding.batch(ops, options || {}, P(resolve, reject));
});
};
@ -451,11 +448,7 @@ Batch.prototype.del = function del(key) {
Batch.prototype.write = function write() {
var self = this;
return new Promise(function(resolve, reject) {
self.batch.write(function(err) {
if (err)
return reject(err);
resolve();
});
self.batch.write(P(resolve, reject));
});
};
@ -481,11 +474,7 @@ Iterator.prototype.next = function() {
}
if (key === undefined && value === undefined) {
self.iter.end(function(err) {
if (err)
return reject(err);
resolve();
});
self.iter.end(P(resolve, reject));
return;
}
@ -501,11 +490,7 @@ Iterator.prototype.seek = function seek(key) {
Iterator.prototype.end = function end() {
var self = this;
return new Promise(function(resolve, reject) {
self.iter.end(function(err) {
if (err)
return reject(err);
resolve();
});
self.iter.end(P(resolve, reject));
});
};

View File

@ -1519,6 +1519,7 @@ Pool.prototype.watchAddress = function watchAddress(address) {
Pool.prototype.getData = function getData(peer, type, hash) {
return spawn(function *() {
var self = this;
var item, exists;
if (!this.loaded)
@ -1534,10 +1535,10 @@ Pool.prototype.getData = function getData(peer, type, hash) {
if (type === this.txType) {
if (peer.queueTX.length === 0) {
utils.nextTick(function() {
this.logger.debug(
self.logger.debug(
'Requesting %d/%d txs from peer with getdata (%s).',
peer.queueTX.length,
this.activeTX,
self.activeTX,
peer.hostname);
peer.getData(peer.queueTX);

View File

@ -194,6 +194,7 @@ Fullnode.prototype._init = function _init() {
});
this.chain.on('connect', function(entry, block) {
return;
self.walletdb.addBlock(entry, block.txs).catch(onError);
if (self.chain.synced)

View File

@ -2,42 +2,39 @@
// See: https://github.com/yoursnetwork/asink
function spawn(genF, self) {
function spawn(generator, self) {
return new Promise(function(resolve, reject) {
var gen = genF.call(self);
var gen = generator.call(self);
function step(nextF) {
function step(value, rejection) {
var next;
try {
next = nextF();
if (rejection)
next = gen.throw(value);
else
next = gen.next(value);
} catch (e) {
// finished with failure, reject the promise
reject(e);
return;
}
if (next.done) {
// finished with success, resolve the promise
resolve(next.value);
return;
}
// not finished, chain off the yielded promise and `step` again
Promise.resolve(next.value).then(function(v) {
step(function() {
return gen.next(v);
});
}, function (e) {
step(function() {
return gen.throw(e);
});
if (!(next.value instanceof Promise)) {
step(next.value);
return;
}
next.value.then(step, function(e) {
step(e, true);
});
}
step(function() {
return gen.next(undefined);
});
step(undefined);
});
}