You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
120 lines
4.3 KiB
120 lines
4.3 KiB
6 years ago
|
import $ from 'jquery'
|
||
|
import humps from 'humps'
|
||
6 years ago
|
import numeral from 'numeral'
|
||
|
import 'numeral/locales'
|
||
6 years ago
|
import router from '../router'
|
||
|
import socket from '../socket'
|
||
|
import { updateAllAges } from '../lib/from_now'
|
||
6 years ago
|
import { batchChannel, initRedux } from '../utils'
|
||
|
|
||
|
const BATCH_THRESHOLD = 10
|
||
6 years ago
|
|
||
|
export const initialState = {
|
||
6 years ago
|
addressCount: null,
|
||
6 years ago
|
averageBlockTime: null,
|
||
6 years ago
|
batchCountAccumulator: 0,
|
||
6 years ago
|
newBlock: null,
|
||
6 years ago
|
newTransactions: [],
|
||
|
transactionCount: null
|
||
6 years ago
|
}
|
||
|
|
||
|
export function reducer (state = initialState, action) {
|
||
|
switch (action.type) {
|
||
6 years ago
|
case 'PAGE_LOAD': {
|
||
|
return Object.assign({}, state, {
|
||
|
transactionCount: numeral(action.transactionCount).value()
|
||
|
})
|
||
|
}
|
||
6 years ago
|
case 'RECEIVED_NEW_ADDRESS_COUNT': {
|
||
|
return Object.assign({}, state, {
|
||
|
addressCount: action.msg.count
|
||
|
})
|
||
|
}
|
||
6 years ago
|
case 'RECEIVED_NEW_BLOCK': {
|
||
|
return Object.assign({}, state, {
|
||
6 years ago
|
averageBlockTime: action.msg.averageBlockTime,
|
||
6 years ago
|
newBlock: action.msg.chainBlockHtml
|
||
6 years ago
|
})
|
||
|
}
|
||
6 years ago
|
case 'RECEIVED_NEW_TRANSACTION_BATCH': {
|
||
|
if (!state.batchCountAccumulator && action.msgs.length < BATCH_THRESHOLD) {
|
||
|
return Object.assign({}, state, {
|
||
|
newTransactions: [
|
||
|
...state.newTransactions,
|
||
6 years ago
|
...action.msgs.map(({transactionHtml}) => transactionHtml)
|
||
6 years ago
|
],
|
||
|
transactionCount: state.transactionCount + action.msgs.length
|
||
6 years ago
|
})
|
||
|
} else {
|
||
|
return Object.assign({}, state, {
|
||
6 years ago
|
batchCountAccumulator: state.batchCountAccumulator + action.msgs.length,
|
||
|
transactionCount: state.transactionCount + action.msgs.length
|
||
6 years ago
|
})
|
||
|
}
|
||
|
}
|
||
6 years ago
|
default:
|
||
|
return state
|
||
|
}
|
||
|
}
|
||
|
|
||
6 years ago
|
router.when('', { exactPathMatch: true }).then(({ locale }) => initRedux(reducer, {
|
||
6 years ago
|
main (store) {
|
||
6 years ago
|
numeral.locale(locale)
|
||
6 years ago
|
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`)
|
||
6 years ago
|
store.dispatch({
|
||
|
type: 'PAGE_LOAD',
|
||
|
transactionCount: $('[data-selector="transaction-count"]').text()
|
||
|
})
|
||
6 years ago
|
blocksChannel.join()
|
||
6 years ago
|
blocksChannel.on('new_block', msg => store.dispatch({ type: 'RECEIVED_NEW_BLOCK', msg: humps.camelizeKeys(msg) }))
|
||
6 years ago
|
|
||
|
const transactionsChannel = socket.channel(`transactions:new_transaction`)
|
||
|
transactionsChannel.join()
|
||
|
transactionsChannel.on('new_transaction', batchChannel((msgs) =>
|
||
6 years ago
|
store.dispatch({ type: 'RECEIVED_NEW_TRANSACTION_BATCH', msgs: humps.camelizeKeys(msgs) }))
|
||
6 years ago
|
)
|
||
6 years ago
|
},
|
||
|
render (state, oldState) {
|
||
6 years ago
|
const $addressCount = $('[data-selector="address-count"]')
|
||
6 years ago
|
const $averageBlockTime = $('[data-selector="average-block-time"]')
|
||
6 years ago
|
const $blockList = $('[data-selector="chain-block-list"]')
|
||
6 years ago
|
const $channelBatching = $('[data-selector="channel-batching-message"]')
|
||
|
const $channelBatchingCount = $('[data-selector="channel-batching-count"]')
|
||
6 years ago
|
const $transactionsList = $('[data-selector="transactions-list"]')
|
||
6 years ago
|
const $transactionCount = $('[data-selector="transaction-count"]')
|
||
6 years ago
|
|
||
6 years ago
|
if (oldState.addressCount !== state.addressCount) {
|
||
6 years ago
|
$addressCount.empty().append(state.addressCount)
|
||
6 years ago
|
}
|
||
6 years ago
|
if (oldState.averageBlockTime !== state.averageBlockTime) {
|
||
6 years ago
|
$averageBlockTime.empty().append(state.averageBlockTime)
|
||
6 years ago
|
}
|
||
6 years ago
|
if (oldState.newBlock !== state.newBlock) {
|
||
|
$blockList.children().last().remove()
|
||
|
$blockList.prepend(state.newBlock)
|
||
|
updateAllAges()
|
||
|
}
|
||
6 years ago
|
if (oldState.transactionCount !== state.transactionCount) $transactionCount.empty().append(numeral(state.transactionCount).format())
|
||
6 years ago
|
if (state.batchCountAccumulator) {
|
||
|
$channelBatching.show()
|
||
|
$channelBatchingCount[0].innerHTML = numeral(state.batchCountAccumulator).format()
|
||
|
} else {
|
||
|
$channelBatching.hide()
|
||
|
}
|
||
6 years ago
|
if (oldState.newTransactions !== state.newTransactions) {
|
||
|
const newTransactionsToInsert = state.newTransactions.slice(oldState.newTransactions.length)
|
||
|
$transactionsList
|
||
|
.children()
|
||
|
.slice($transactionsList.children().length - newTransactionsToInsert.length, $transactionsList.children().length)
|
||
|
.remove()
|
||
|
$transactionsList.prepend(newTransactionsToInsert.reverse().join(''))
|
||
|
|
||
|
updateAllAges()
|
||
|
}
|
||
6 years ago
|
}
|
||
|
}))
|