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

View File

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

View File

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

View File

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