chaindb: async loading.
This commit is contained in:
parent
c5bc356bc9
commit
34a44fe391
@ -97,15 +97,27 @@ Chain.prototype._init = function _init() {
|
||||
|
||||
utils.nextTick(function() {
|
||||
var count = self.db.count();
|
||||
var i, entry;
|
||||
var entry;
|
||||
var i = 1;
|
||||
|
||||
for (i = 1; i < count; i++)
|
||||
self._saveEntry(self.db.get(i));
|
||||
function done() {
|
||||
self.loading = false;
|
||||
self.emit('load');
|
||||
|
||||
self.loading = false;
|
||||
self.emit('load');
|
||||
utils.debug('Chain successfully loaded.');
|
||||
}
|
||||
|
||||
utils.debug('Chain successfully loaded.');
|
||||
(function next() {
|
||||
if (i >= count)
|
||||
return done();
|
||||
self.db.getAsync(i, function(err, entry) {
|
||||
if (err)
|
||||
throw err;
|
||||
self._saveEntry(entry);
|
||||
i += 1;
|
||||
next();
|
||||
});
|
||||
})();
|
||||
});
|
||||
};
|
||||
|
||||
@ -996,7 +1008,7 @@ ChainDB.prototype._read = function _read(size, offset) {
|
||||
}
|
||||
};
|
||||
|
||||
ChainDB.prototype._readAsync = function _read(size, offset) {
|
||||
ChainDB.prototype._readAsync = function _read(size, offset, callback) {
|
||||
var self = this;
|
||||
var data = new Buffer(size);
|
||||
var index = 0;
|
||||
|
||||
@ -414,6 +414,9 @@ Pool.prototype._addLoader = function _addLoader() {
|
||||
};
|
||||
|
||||
Pool.prototype.startSync = function startSync() {
|
||||
if (this.loading)
|
||||
return this.once('load', this.startSync.bind(this));
|
||||
|
||||
this.syncing = true;
|
||||
|
||||
this._startInterval();
|
||||
@ -436,6 +439,9 @@ Pool.prototype.stopSync = function stopSync() {
|
||||
|
||||
this.syncing = false;
|
||||
|
||||
if (this.loading)
|
||||
return;
|
||||
|
||||
this._stopInterval();
|
||||
this._stopTimer();
|
||||
};
|
||||
@ -1052,6 +1058,9 @@ Pool.prototype.addWallet = function addWallet(w, defaultTs) {
|
||||
var self = this;
|
||||
var e;
|
||||
|
||||
if (this.loading)
|
||||
return this.once('load', this.addWallet.bind(this, w, defaultTs));
|
||||
|
||||
if (this.wallets.indexOf(w) !== -1)
|
||||
return false;
|
||||
|
||||
@ -1089,6 +1098,7 @@ Pool.prototype.addWallet = function addWallet(w, defaultTs) {
|
||||
|
||||
Pool.prototype.removeWallet = function removeWallet(w) {
|
||||
var i = this.wallets.indexOf(w);
|
||||
assert(!this.loading);
|
||||
if (i == -1)
|
||||
return;
|
||||
this.wallets.splice(i, 1);
|
||||
@ -1127,6 +1137,8 @@ Pool.prototype.searchWallet = function(w) {
|
||||
var self = this;
|
||||
var ts;
|
||||
|
||||
assert(!this.loading);
|
||||
|
||||
if (this.options.fullNode)
|
||||
return;
|
||||
|
||||
@ -1167,6 +1179,8 @@ Pool.prototype.search = function search(id, range, e) {
|
||||
var self = this;
|
||||
var hashes, pending, listener, timeout, done, total, cb;
|
||||
|
||||
assert(!this.loading);
|
||||
|
||||
if (this.options.fullNode)
|
||||
return;
|
||||
|
||||
|
||||
Loading…
Reference in New Issue
Block a user