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

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

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

Loading…
Cancel
Save