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.
133 lines
4.7 KiB
133 lines
4.7 KiB
7 years ago
|
import $ from 'jquery'
|
||
6 years ago
|
import humps from 'humps'
|
||
7 years ago
|
import numeral from 'numeral'
|
||
|
import 'numeral/locales'
|
||
|
import socket from '../socket'
|
||
|
import router from '../router'
|
||
6 years ago
|
import { updateAllAges } from '../lib/from_now'
|
||
|
import { batchChannel, initRedux } from '../utils'
|
||
|
|
||
|
const BATCH_THRESHOLD = 10
|
||
7 years ago
|
|
||
6 years ago
|
export const initialState = {
|
||
6 years ago
|
batchCountAccumulator: 0,
|
||
6 years ago
|
beyondPageOne: null,
|
||
6 years ago
|
blockNumber: null,
|
||
6 years ago
|
channelDisconnected: false,
|
||
6 years ago
|
confirmations: null,
|
||
6 years ago
|
newTransactions: [],
|
||
|
transactionCount: null
|
||
6 years ago
|
}
|
||
7 years ago
|
|
||
|
export function reducer (state = initialState, action) {
|
||
|
switch (action.type) {
|
||
6 years ago
|
case 'PAGE_LOAD': {
|
||
7 years ago
|
return Object.assign({}, state, {
|
||
6 years ago
|
beyondPageOne: !!action.index,
|
||
6 years ago
|
blockNumber: parseInt(action.blockNumber, 10),
|
||
|
transactionCount: numeral(action.transactionCount).value()
|
||
7 years ago
|
})
|
||
|
}
|
||
6 years ago
|
case 'CHANNEL_DISCONNECTED': {
|
||
|
if (state.beyondPageOne) return state
|
||
|
|
||
|
return Object.assign({}, state, {
|
||
|
channelDisconnected: true,
|
||
|
batchCountAccumulator: 0
|
||
|
})
|
||
|
}
|
||
6 years ago
|
case 'RECEIVED_NEW_BLOCK': {
|
||
6 years ago
|
if ((action.msg.blockNumber - state.blockNumber) > state.confirmations) {
|
||
6 years ago
|
return Object.assign({}, state, {
|
||
6 years ago
|
confirmations: action.msg.blockNumber - state.blockNumber
|
||
6 years ago
|
})
|
||
|
} else return state
|
||
|
}
|
||
6 years ago
|
case 'RECEIVED_NEW_TRANSACTION_BATCH': {
|
||
6 years ago
|
if (state.channelDisconnected || state.beyondPageOne) return state
|
||
|
|
||
6 years ago
|
if (!state.batchCountAccumulator && action.msgs.length < BATCH_THRESHOLD) {
|
||
|
return Object.assign({}, state, {
|
||
|
newTransactions: [
|
||
|
...state.newTransactions,
|
||
|
...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
|
})
|
||
|
}
|
||
|
}
|
||
7 years ago
|
default:
|
||
|
return state
|
||
|
}
|
||
|
}
|
||
|
|
||
6 years ago
|
router.when('/transactions/:transactionHash').then(({ locale }) => initRedux(reducer, {
|
||
7 years ago
|
main (store) {
|
||
6 years ago
|
const blocksChannel = socket.channel(`blocks:new_block`, {})
|
||
6 years ago
|
const $transactionBlockNumber = $('[data-selector="block-number"]')
|
||
7 years ago
|
numeral.locale(locale)
|
||
6 years ago
|
store.dispatch({
|
||
|
type: 'PAGE_LOAD',
|
||
|
blockNumber: $transactionBlockNumber.text()
|
||
|
})
|
||
6 years ago
|
blocksChannel.join()
|
||
|
blocksChannel.on('new_block', (msg) => store.dispatch({ type: 'RECEIVED_NEW_BLOCK', msg: humps.camelizeKeys(msg) }))
|
||
7 years ago
|
},
|
||
|
render (state, oldState) {
|
||
6 years ago
|
const $blockConfirmations = $('[data-selector="block-confirmations"]')
|
||
6 years ago
|
|
||
7 years ago
|
if (oldState.confirmations !== state.confirmations) {
|
||
7 years ago
|
$blockConfirmations.empty().append(numeral(state.confirmations).format())
|
||
7 years ago
|
}
|
||
|
}
|
||
|
}))
|
||
6 years ago
|
|
||
6 years ago
|
router.when('/transactions', { exactPathMatch: true }).then((params) => initRedux(reducer, {
|
||
6 years ago
|
main (store) {
|
||
6 years ago
|
const { locale, index } = params
|
||
6 years ago
|
const transactionsChannel = socket.channel(`transactions:new_transaction`)
|
||
6 years ago
|
numeral.locale(locale)
|
||
6 years ago
|
store.dispatch({
|
||
|
type: 'PAGE_LOAD',
|
||
|
transactionCount: $('[data-selector="transaction-count"]').text(),
|
||
|
index
|
||
|
})
|
||
6 years ago
|
transactionsChannel.join()
|
||
6 years ago
|
transactionsChannel.onError(() => store.dispatch({ type: 'CHANNEL_DISCONNECTED' }))
|
||
6 years ago
|
transactionsChannel.on('new_transaction', batchChannel((msgs) =>
|
||
|
store.dispatch({ type: 'RECEIVED_NEW_TRANSACTION_BATCH', msgs: humps.camelizeKeys(msgs) }))
|
||
|
)
|
||
|
},
|
||
|
render (state, oldState) {
|
||
|
const $channelBatching = $('[data-selector="channel-batching-message"]')
|
||
|
const $channelBatchingCount = $('[data-selector="channel-batching-count"]')
|
||
6 years ago
|
const $channelDisconnected = $('[data-selector="channel-disconnected-message"]')
|
||
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 (state.channelDisconnected) $channelDisconnected.show()
|
||
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()
|
||
|
}
|
||
|
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()
|
||
|
}
|
||
|
}
|
||
|
}))
|