Wireup live update of address count

pull/511/head
Stamates 6 years ago
parent 5c50e08fd1
commit 1e43456b0e
  1. 16
      apps/explorer_web/assets/js/pages/chain.js
  2. 46
      apps/explorer_web/lib/explorer_web/channels/address_channel.ex
  3. 2
      apps/explorer_web/lib/explorer_web/notifier.ex

@ -10,6 +10,7 @@ import { batchChannel, initRedux } from '../utils'
const BATCH_THRESHOLD = 10
export const initialState = {
addressCount: null,
averageBlockTime: null,
batchCountAccumulator: 0,
newBlock: null,
@ -24,6 +25,11 @@ export function reducer (state = initialState, action) {
transactionCount: numeral(action.transactionCount).value()
})
}
case 'RECEIVED_NEW_ADDRESS_COUNT': {
return Object.assign({}, state, {
addressCount: action.msg.count
})
}
case 'RECEIVED_NEW_BLOCK': {
return Object.assign({}, state, {
averageBlockTime: action.msg.averageBlockTime,
@ -53,8 +59,12 @@ export function reducer (state = initialState, action) {
router.when('', { exactPathMatch: true }).then(({ locale }) => initRedux(reducer, {
main (store) {
const blocksChannel = socket.channel(`blocks:new_block`)
numeral.locale(locale)
const addressesChannel = socket.channel(`addresses:new_address`)
addressesChannel.join()
addressesChannel.on('count', msg => store.dispatch({ type: 'RECEIVED_NEW_ADDRESS_COUNT', msg: humps.camelizeKeys(msg) }))
const blocksChannel = socket.channel(`blocks:new_block`)
store.dispatch({
type: 'PAGE_LOAD',
transactionCount: $('[data-selector="transaction-count"]').text()
@ -69,6 +79,7 @@ router.when('', { exactPathMatch: true }).then(({ locale }) => initRedux(reducer
)
},
render (state, oldState) {
const $addressCount = $('[data-selector="address-count"]')
const $averageBlockTime = $('[data-selector="average-block-time"]')
const $blockList = $('[data-selector="chain-block-list"]')
const $channelBatching = $('[data-selector="channel-batching-message"]')
@ -76,6 +87,9 @@ router.when('', { exactPathMatch: true }).then(({ locale }) => initRedux(reducer
const $transactionsList = $('[data-selector="transactions-list"]')
const $transactionCount = $('[data-selector="transaction-count"]')
if (oldState.addressCount !== state.addressCount) {
$addressCount.empty().append(state.addressCount)
}
if (oldState.averageBlockTime !== state.averageBlockTime) {
$averageBlockTime.empty().append(state.averageBlockTime)
}

@ -7,50 +7,58 @@ defmodule ExplorerWeb.AddressChannel do
alias ExplorerWeb.{AddressTransactionView, AddressView}
alias Phoenix.View
intercept(["balance_update", "transaction"])
intercept(["balance_update", "count", "transaction"])
def join("addresses:" <> _address_hash, _params, socket) do
{:ok, %{}, socket}
end
def handle_out("transaction", %{address: address, transaction: transaction}, socket) do
def handle_out(
"balance_update",
%{address: address, exchange_rate: exchange_rate},
socket
) do
Gettext.put_locale(ExplorerWeb.Gettext, socket.assigns.locale)
rendered =
View.render_to_string(
AddressTransactionView,
"_transaction.html",
AddressView,
"_balance_card.html",
locale: socket.assigns.locale,
address: address,
transaction: transaction
exchange_rate: exchange_rate
)
push(socket, "transaction", %{
to_address_hash: to_string(transaction.to_address_hash),
from_address_hash: to_string(transaction.from_address_hash),
transaction_html: rendered
})
push(socket, "balance", %{balance: rendered})
{:noreply, socket}
end
def handle_out("count", %{count: count}, socket) do
Gettext.put_locale(ExplorerWeb.Gettext, socket.assigns.locale)
push(socket, "count", %{count: Cldr.Number.to_string!(count, format: "#,###")})
{:noreply, socket}
end
def handle_out(
"balance_update",
%{address: address, exchange_rate: exchange_rate},
socket
) do
def handle_out("transaction", %{address: address, transaction: transaction}, socket) do
Gettext.put_locale(ExplorerWeb.Gettext, socket.assigns.locale)
rendered =
View.render_to_string(
AddressView,
"_balance_card.html",
AddressTransactionView,
"_transaction.html",
locale: socket.assigns.locale,
address: address,
exchange_rate: exchange_rate
transaction: transaction
)
push(socket, "balance", %{balance: rendered})
push(socket, "transaction", %{
to_address_hash: to_string(transaction.to_address_hash),
from_address_hash: to_string(transaction.from_address_hash),
transaction_html: rendered
})
{:noreply, socket}
end
end

@ -9,6 +9,8 @@ defmodule ExplorerWeb.Notifier do
alias ExplorerWeb.Endpoint
def handle_event({:chain_event, :addresses, addresses}) do
Endpoint.broadcast("addresses:new_address", "count", %{count: Chain.address_estimated_count()})
addresses
|> Stream.reject(fn %Address{fetched_balance: fetched_balance} -> is_nil(fetched_balance) end)
|> Enum.each(&broadcast_balance/1)

Loading…
Cancel
Save