Skip to content

Commit

Permalink
add sync flag and related events
Browse files Browse the repository at this point in the history
  • Loading branch information
fanatid committed Nov 28, 2014
1 parent e6ae263 commit a0f1aa4
Show file tree
Hide file tree
Showing 4 changed files with 123 additions and 7 deletions.
34 changes: 34 additions & 0 deletions src/Wallet.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,14 @@ var verify = require('./verify')
* @param {AssetDefinition} assetdef
*/

/**
* @event Wallet#syncStart
*/

/**
* @event Wallet#syncStop
*/

/**
* @event Wallet#initialize
*/
Expand Down Expand Up @@ -96,6 +104,8 @@ function Wallet(opts) {
var self = this
events.EventEmitter.call(self)

self._syncCountValue = 0

verify.boolean(opts.testnet)
verify.string(opts.network)
opts.networkOpts = _.extend({ testnet: opts.testnet }, opts.networkOpts)
Expand Down Expand Up @@ -155,6 +165,11 @@ function Wallet(opts) {

self.aManager.on('newAsset', function(assetdef) { self.emit('newAsset', assetdef) })
self.coinManager.on('touchAsset', function(assetdef) { self.emit('touchAsset', assetdef) })

self.txDb.on('syncStart', function () { self._syncCount += 1 })
self.coinManager.on('syncStart', function () { self._syncCount += 1 })
self.txDb.on('syncStop', function () { self._syncCount -= 1 })
self.coinManager.on('syncStop', function () { self._syncCount -= 1 })
}

inherits(Wallet, events.EventEmitter)
Expand All @@ -171,6 +186,25 @@ Wallet.prototype.getTxDb = function() { return this.txDb }
Wallet.prototype.getCoinManager = function() { return this.coinManager }
Wallet.prototype.getCoinQuery = function() { return new coin.CoinQuery(this) }

Object.defineProperty(Wallet.prototype, '_syncCount', {
get: function () {
return this._syncCountValue
},
set: function (newValue) {
if (this._syncCountValue === 0 && newValue > 0) { this.emit('syncStart') }
if (this._syncCountValue > 0 && newValue === 0) { this.emit('syncStop') }

this._syncCountValue = newValue
}
})

/**
* @return {boolean}
*/
Wallet.prototype.isSyncing = function () {
return this._syncCount > 0
}

/**
* @return {boolean}
*/
Expand Down
57 changes: 52 additions & 5 deletions src/coin/CoinManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,14 @@ function getWalletAllAddresses(wallet) {
* @param {AssetDefinition} asset
*/

/**
* @event CoinManager#syncStart
*/

/**
* @event CoinManager#syncStop
*/

/**
* @class CoinManager
* @extends events.EventEmitter
Expand All @@ -56,6 +64,8 @@ function CoinManager(wallet, storage) {
self._wallet = wallet
self._storage = storage

self._syncCountValue = 0

self._wallet.getTxDb().on('addTx', self._addTx.bind(self))
self._wallet.getTxDb().on('revertTx', self._revertTx.bind(self))

Expand Down Expand Up @@ -101,6 +111,8 @@ CoinManager.prototype._addTx = function(tx) {

var self = this

self._syncCount += 1

var txId = tx.getId()
var allAddresses = getWalletAllAddresses(self._wallet)

Expand All @@ -109,7 +121,7 @@ CoinManager.prototype._addTx = function(tx) {
self._storage.markCoinAsSpent(txId, input.index)
})

var promises = tx.outs.map(function(output, index) {
var promises = tx.outs.map(function (output, index) {
if (self._storage.isCoinExists(txId, index))
return

Expand Down Expand Up @@ -144,7 +156,13 @@ CoinManager.prototype._addTx = function(tx) {
})
})

Q.all(promises).catch(function(error) { self.emit('error', error) })
Q.all(promises).finally(function () {
self._syncCount -= 1

}).catch(function (error) {
self.emit('error', error)

})
}

/**
Expand All @@ -155,6 +173,8 @@ CoinManager.prototype._revertTx = function(tx) {

var self = this

self._syncCount += 1

var txId = tx.getId()
var allAddresses = getWalletAllAddresses(self._wallet)

Expand All @@ -163,13 +183,13 @@ CoinManager.prototype._revertTx = function(tx) {
self._storage.markCoinAsUnspent(txId, input.index)
})

tx.outs.map(function(output, index) {
var promises = tx.outs.map(function (output, index) {
var coinRecord = self._storage.removeCoin(txId, index)
if (coinRecord === null)
return

var coin = self._record2Coin(coinRecord)
Q.ninvoke(coin, 'getMainColorValue').then(function(colorValue) {
return Q.ninvoke(coin, 'getMainColorValue').then(function (colorValue) {
var colorDesc = colorValue.getColorDefinition().getDesc()
var assetdef = self._wallet.getAssetDefinitionManager().getByDesc(colorDesc)
self.emit('touchAsset', assetdef)
Expand All @@ -179,8 +199,35 @@ CoinManager.prototype._revertTx = function(tx) {
self.emit('touchAddress', address)
})

}).catch(function(error) { self.emit('error', error) })
})
})

Q.all(promises).finally(function () {
self._syncCount -= 1

}).catch(function (error) {
self.emit('error', error)

})
}

Object.defineProperty(CoinManager.prototype, '_syncCount', {
get: function () {
return this._syncCountValue
},
set: function (newValue) {
if (this._syncCountValue === 0 && newValue > 0) { this.emit('syncStart') }
if (this._syncCountValue > 0 && newValue === 0) { this.emit('syncStop') }

this._syncCountValue = newValue
}
})

/**
* @return {boolean}
*/
CoinManager.prototype.isSyncing = function () {
return this._syncCount > 0
}

/**
Expand Down
38 changes: 36 additions & 2 deletions src/tx/TxDb.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,14 @@ function getCurrentTimestamp() {
* @param {Transaction} tx
*/

/**
* @event TxDb#syncStart
*/

/**
* @event TxDb#syncStop
*/

/**
* @class TxDb
* @extends events.EventEmitter
Expand All @@ -63,6 +71,8 @@ function TxDb(wallet, txStorage) {
self._addTxSync = {}
self._addTxQueue = {}

self._syncCountValue = 0

self._txStorage.getAllTxIds().forEach(function(txId) {
var record = self._txStorage.get(txId)
if (record !== null && record.status === txStatus.dispatch)
Expand Down Expand Up @@ -141,8 +151,13 @@ TxDb.prototype._addTx = function(txId, data, cb) {

var self = this

self._syncCount += 1

var deferred = Q.defer()
deferred.promise.done(function() { cb(null) }, function(error) { cb(error) })
deferred.promise.finally(function() {
self._syncCount -= 1

}).done(function () { cb(null) }, function (error) { cb(error) })

if (_.isUndefined(self._addTxQueue[txId]))
self._addTxQueue[txId] = []
Expand Down Expand Up @@ -230,6 +245,25 @@ TxDb.prototype._addTx = function(txId, data, cb) {
}).done()
}

Object.defineProperty(TxDb.prototype, '_syncCount', {
get: function () {
return this._syncCountValue
},
set: function (newValue) {
if (this._syncCountValue === 0 && newValue > 0) { this.emit('syncStart') }
if (this._syncCountValue > 0 && newValue === 0) { this.emit('syncStop') }

this._syncCountValue = newValue
}
})

/**
* @return {boolean}
*/
TxDb.prototype.isSyncing = function () {
return this._syncCount > 0
}

/**
* @param {Transaction} tx
* @param {TxDb~errorCallback} cb
Expand Down Expand Up @@ -303,7 +337,7 @@ TxDb.prototype.historySync = function(address, entries, cb) {
tAddresses: [address]
}

// Todo:
// @todo
// How add tx if they was marked as invalid because was aborted in revert case
// and now re-created with equals params and have same txId... ?
// Example:
Expand Down
1 change: 1 addition & 0 deletions src/tx/TxFetcher.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ TxFetcher.prototype.historySync = function(address, cb) {
verify.function(cb)

var self = this

Q.ninvoke(self._blockchain, 'getHistory', address).then(function(entries) {
return Q.ninvoke(self._txdb, 'historySync', address, entries)

Expand Down

0 comments on commit a0f1aa4

Please sign in to comment.