mirror of
https://github.com/kremalicious/metamask-extension.git
synced 2024-12-23 09:52:26 +01:00
Add more documentation to computed balances controller
This commit is contained in:
parent
1ef6528921
commit
9c7eafc86f
@ -2,8 +2,16 @@ const ObservableStore = require('obs-store')
|
|||||||
const extend = require('xtend')
|
const extend = require('xtend')
|
||||||
const BalanceController = require('./balance')
|
const BalanceController = require('./balance')
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Background controller responsible for syncing
|
||||||
|
* and computing ETH balances for all accounts
|
||||||
|
*/
|
||||||
class ComputedbalancesController {
|
class ComputedbalancesController {
|
||||||
|
/**
|
||||||
|
* Creates a new controller instance
|
||||||
|
*
|
||||||
|
* @param {Object} [opts] Controller configuration parameters
|
||||||
|
*/
|
||||||
constructor (opts = {}) {
|
constructor (opts = {}) {
|
||||||
const { accountTracker, txController, blockTracker } = opts
|
const { accountTracker, txController, blockTracker } = opts
|
||||||
this.accountTracker = accountTracker
|
this.accountTracker = accountTracker
|
||||||
@ -19,6 +27,9 @@ class ComputedbalancesController {
|
|||||||
this._initBalanceUpdating()
|
this._initBalanceUpdating()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Updates balances associated with each internal address
|
||||||
|
*/
|
||||||
updateAllBalances () {
|
updateAllBalances () {
|
||||||
Object.keys(this.balances).forEach((balance) => {
|
Object.keys(this.balances).forEach((balance) => {
|
||||||
const address = balance.address
|
const address = balance.address
|
||||||
@ -26,12 +37,23 @@ class ComputedbalancesController {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Initializes internal address tracking
|
||||||
|
*
|
||||||
|
* @private
|
||||||
|
*/
|
||||||
_initBalanceUpdating () {
|
_initBalanceUpdating () {
|
||||||
const store = this.accountTracker.store.getState()
|
const store = this.accountTracker.store.getState()
|
||||||
this.syncAllAccountsFromStore(store)
|
this.syncAllAccountsFromStore(store)
|
||||||
this.accountTracker.store.subscribe(this.syncAllAccountsFromStore.bind(this))
|
this.accountTracker.store.subscribe(this.syncAllAccountsFromStore.bind(this))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Uses current account state to sync and track all
|
||||||
|
* addresses associated with the current account
|
||||||
|
*
|
||||||
|
* @param {Object} store Account tracking state
|
||||||
|
*/
|
||||||
syncAllAccountsFromStore (store) {
|
syncAllAccountsFromStore (store) {
|
||||||
const upstream = Object.keys(store.accounts)
|
const upstream = Object.keys(store.accounts)
|
||||||
const balances = Object.keys(this.balances)
|
const balances = Object.keys(this.balances)
|
||||||
@ -50,6 +72,13 @@ class ComputedbalancesController {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Conditionally establishes a new subscription
|
||||||
|
* to track an address associated with the current
|
||||||
|
* account
|
||||||
|
*
|
||||||
|
* @param {string} address Address to conditionally subscribe to
|
||||||
|
*/
|
||||||
trackAddressIfNotAlready (address) {
|
trackAddressIfNotAlready (address) {
|
||||||
const state = this.store.getState()
|
const state = this.store.getState()
|
||||||
if (!(address in state.computedBalances)) {
|
if (!(address in state.computedBalances)) {
|
||||||
@ -57,6 +86,12 @@ class ComputedbalancesController {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Establishes a new subscription to track an
|
||||||
|
* address associated with the current account
|
||||||
|
*
|
||||||
|
* @param {string} address Address to conditionally subscribe to
|
||||||
|
*/
|
||||||
trackAddress (address) {
|
trackAddress (address) {
|
||||||
const updater = new BalanceController({
|
const updater = new BalanceController({
|
||||||
address,
|
address,
|
||||||
|
@ -1,6 +1,10 @@
|
|||||||
module.exports = createProviderMiddleware
|
module.exports = createProviderMiddleware
|
||||||
|
|
||||||
// forward requests to provider
|
/**
|
||||||
|
* Forwards an HTTP request to the current Web3 provider
|
||||||
|
*
|
||||||
|
* @param {{ provider: Object }} config Configuration containing current Web3 provider
|
||||||
|
*/
|
||||||
function createProviderMiddleware ({ provider }) {
|
function createProviderMiddleware ({ provider }) {
|
||||||
return (req, res, next, end) => {
|
return (req, res, next, end) => {
|
||||||
provider.sendAsync(req, (err, _res) => {
|
provider.sendAsync(req, (err, _res) => {
|
||||||
|
@ -6,6 +6,13 @@ module.exports = PortDuplexStream
|
|||||||
|
|
||||||
inherits(PortDuplexStream, Duplex)
|
inherits(PortDuplexStream, Duplex)
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates a stream that's both readable and writable.
|
||||||
|
* The stream supports arbitrary JavaScript objects.
|
||||||
|
*
|
||||||
|
* @class
|
||||||
|
* @param {Object} port Remote Port object
|
||||||
|
*/
|
||||||
function PortDuplexStream (port) {
|
function PortDuplexStream (port) {
|
||||||
Duplex.call(this, {
|
Duplex.call(this, {
|
||||||
objectMode: true,
|
objectMode: true,
|
||||||
@ -15,8 +22,13 @@ function PortDuplexStream (port) {
|
|||||||
port.onDisconnect.addListener(this._onDisconnect.bind(this))
|
port.onDisconnect.addListener(this._onDisconnect.bind(this))
|
||||||
}
|
}
|
||||||
|
|
||||||
// private
|
/**
|
||||||
|
* Callback triggered when a message is received from
|
||||||
|
* the remote Port associated with this Stream.
|
||||||
|
*
|
||||||
|
* @private
|
||||||
|
* @param {Object} msg - Payload from the onMessage listener of Port
|
||||||
|
*/
|
||||||
PortDuplexStream.prototype._onMessage = function (msg) {
|
PortDuplexStream.prototype._onMessage = function (msg) {
|
||||||
if (Buffer.isBuffer(msg)) {
|
if (Buffer.isBuffer(msg)) {
|
||||||
delete msg._isBuffer
|
delete msg._isBuffer
|
||||||
@ -27,14 +39,31 @@ PortDuplexStream.prototype._onMessage = function (msg) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Callback triggered when the remote Port
|
||||||
|
* associated with this Stream disconnects.
|
||||||
|
*
|
||||||
|
* @private
|
||||||
|
*/
|
||||||
PortDuplexStream.prototype._onDisconnect = function () {
|
PortDuplexStream.prototype._onDisconnect = function () {
|
||||||
this.destroy()
|
this.destroy()
|
||||||
}
|
}
|
||||||
|
|
||||||
// stream plumbing
|
/**
|
||||||
|
* Explicitly sets read operations to a no-op
|
||||||
|
*/
|
||||||
PortDuplexStream.prototype._read = noop
|
PortDuplexStream.prototype._read = noop
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Called internally when data should be written to
|
||||||
|
* this writable stream.
|
||||||
|
*
|
||||||
|
* @private
|
||||||
|
* @param {*} msg Arbitrary JavaScript object to write
|
||||||
|
* @param {string} encoding Encoding to use when writing payload
|
||||||
|
* @param {Function} cb Called when writing is complete or an error occurs
|
||||||
|
*/
|
||||||
PortDuplexStream.prototype._write = function (msg, encoding, cb) {
|
PortDuplexStream.prototype._write = function (msg, encoding, cb) {
|
||||||
try {
|
try {
|
||||||
if (Buffer.isBuffer(msg)) {
|
if (Buffer.isBuffer(msg)) {
|
||||||
|
@ -1,6 +1,9 @@
|
|||||||
|
|
||||||
module.exports = setupMetamaskMeshMetrics
|
module.exports = setupMetamaskMeshMetrics
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Injects an iframe into the curerent document for testing
|
||||||
|
*/
|
||||||
function setupMetamaskMeshMetrics() {
|
function setupMetamaskMeshMetrics() {
|
||||||
const testingContainer = document.createElement('iframe')
|
const testingContainer = document.createElement('iframe')
|
||||||
testingContainer.src = 'https://metamask.github.io/mesh-testing/'
|
testingContainer.src = 'https://metamask.github.io/mesh-testing/'
|
||||||
|
Loading…
x
Reference in New Issue
Block a user