From 1e43456b0e1667c3aeb0a8774e90ea99532b8046 Mon Sep 17 00:00:00 2001 From: Stamates Date: Mon, 6 Aug 2018 17:27:42 -0400 Subject: [PATCH] Wireup live update of address count --- apps/explorer_web/assets/js/pages/chain.js | 16 ++++++- .../explorer_web/channels/address_channel.ex | 48 +++++++++++-------- .../explorer_web/lib/explorer_web/notifier.ex | 2 + 3 files changed, 45 insertions(+), 21 deletions(-) diff --git a/apps/explorer_web/assets/js/pages/chain.js b/apps/explorer_web/assets/js/pages/chain.js index aea629f7de..62c3ebc44e 100644 --- a/apps/explorer_web/assets/js/pages/chain.js +++ b/apps/explorer_web/assets/js/pages/chain.js @@ -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) } diff --git a/apps/explorer_web/lib/explorer_web/channels/address_channel.ex b/apps/explorer_web/lib/explorer_web/channels/address_channel.ex index b949f54f6e..8a4806e8a2 100644 --- a/apps/explorer_web/lib/explorer_web/channels/address_channel.ex +++ b/apps/explorer_web/lib/explorer_web/channels/address_channel.ex @@ -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( - "balance_update", - %{address: address, exchange_rate: exchange_rate}, - socket - ) do + 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("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 diff --git a/apps/explorer_web/lib/explorer_web/notifier.ex b/apps/explorer_web/lib/explorer_web/notifier.ex index 7f2f129680..037f014aef 100644 --- a/apps/explorer_web/lib/explorer_web/notifier.ex +++ b/apps/explorer_web/lib/explorer_web/notifier.ex @@ -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)