parent
86b6336ea6
commit
0d65bbda3c
@ -0,0 +1,147 @@ |
||||
import { reducer, initialState } from '../../../js/pages/address/internal_transactions' |
||||
|
||||
describe('RECEIVED_NEW_INTERNAL_TRANSACTION_BATCH', () => { |
||||
test('with new internal transaction', () => { |
||||
const state = Object.assign({}, initialState, { |
||||
items: ['test 1'] |
||||
}) |
||||
const action = { |
||||
type: 'RECEIVED_NEW_INTERNAL_TRANSACTION_BATCH', |
||||
msgs: [{ internalTransactionHtml: 'test 2' }] |
||||
} |
||||
const output = reducer(state, action) |
||||
|
||||
expect(output.items).toEqual(['test 2', 'test 1']) |
||||
}) |
||||
|
||||
test('with batch of new internal transactions', () => { |
||||
const state = Object.assign({}, initialState, { |
||||
items: ['test 1'] |
||||
}) |
||||
const action = { |
||||
type: 'RECEIVED_NEW_INTERNAL_TRANSACTION_BATCH', |
||||
msgs: [ |
||||
{ internalTransactionHtml: 'test 2' }, |
||||
{ internalTransactionHtml: 'test 3' }, |
||||
{ internalTransactionHtml: 'test 4' }, |
||||
{ internalTransactionHtml: 'test 5' }, |
||||
{ internalTransactionHtml: 'test 6' }, |
||||
{ internalTransactionHtml: 'test 7' }, |
||||
{ internalTransactionHtml: 'test 8' }, |
||||
{ internalTransactionHtml: 'test 9' }, |
||||
{ internalTransactionHtml: 'test 10' }, |
||||
{ internalTransactionHtml: 'test 11' }, |
||||
{ internalTransactionHtml: 'test 12' }, |
||||
{ internalTransactionHtml: 'test 13' } |
||||
] |
||||
} |
||||
const output = reducer(state, action) |
||||
|
||||
expect(output.items).toEqual(['test 1']) |
||||
expect(output.internalTransactionsBatch).toEqual([ |
||||
'test 13', |
||||
'test 12', |
||||
'test 11', |
||||
'test 10', |
||||
'test 9', |
||||
'test 8', |
||||
'test 7', |
||||
'test 6', |
||||
'test 5', |
||||
'test 4', |
||||
'test 3', |
||||
'test 2', |
||||
]) |
||||
}) |
||||
|
||||
test('after batch of new internal transactions', () => { |
||||
const state = Object.assign({}, initialState, { |
||||
internalTransactionsBatch: ['test 1'] |
||||
}) |
||||
const action = { |
||||
type: 'RECEIVED_NEW_INTERNAL_TRANSACTION_BATCH', |
||||
msgs: [ |
||||
{ internalTransactionHtml: 'test 2' } |
||||
] |
||||
} |
||||
const output = reducer(state, action) |
||||
|
||||
expect(output.internalTransactionsBatch).toEqual(['test 2', 'test 1']) |
||||
}) |
||||
|
||||
test('when channel has been disconnected', () => { |
||||
const state = Object.assign({}, initialState, { |
||||
channelDisconnected: true, |
||||
items: ['test 1'] |
||||
}) |
||||
const action = { |
||||
type: 'RECEIVED_NEW_INTERNAL_TRANSACTION_BATCH', |
||||
msgs: [{ internalTransactionHtml: 'test 2' }] |
||||
} |
||||
const output = reducer(state, action) |
||||
|
||||
expect(output.items).toEqual(['test 1']) |
||||
}) |
||||
|
||||
test('beyond page one', () => { |
||||
const state = Object.assign({}, initialState, { |
||||
beyondPageOne: true, |
||||
items: ['test 1'] |
||||
}) |
||||
const action = { |
||||
type: 'RECEIVED_NEW_INTERNAL_TRANSACTION_BATCH', |
||||
msgs: [{ internalTransactionHtml: 'test 2' }] |
||||
} |
||||
const output = reducer(state, action) |
||||
|
||||
expect(output.items).toEqual(['test 1']) |
||||
}) |
||||
|
||||
test('with filtered "to" internal transaction', () => { |
||||
const state = Object.assign({}, initialState, { |
||||
filter: 'to', |
||||
addressHash: '0x00', |
||||
items: [] |
||||
}) |
||||
const action = { |
||||
type: 'RECEIVED_NEW_INTERNAL_TRANSACTION_BATCH', |
||||
msgs: [{ |
||||
fromAddressHash: '0x00', |
||||
toAddressHash: '0x01', |
||||
internalTransactionHtml: 'test 2' |
||||
}, |
||||
{ |
||||
fromAddressHash: '0x01', |
||||
toAddressHash: '0x00', |
||||
internalTransactionHtml: 'test 3' |
||||
}] |
||||
} |
||||
const output = reducer(state, action) |
||||
|
||||
expect(output.items).toEqual(['test 3']) |
||||
}) |
||||
|
||||
test('with filtered "from" internal transaction', () => { |
||||
const state = Object.assign({}, initialState, { |
||||
filter: 'from', |
||||
addressHash: '0x00', |
||||
items: [] |
||||
}) |
||||
const action = { |
||||
type: 'RECEIVED_NEW_INTERNAL_TRANSACTION_BATCH', |
||||
msgs: [{ |
||||
fromAddressHash: '0x00', |
||||
toAddressHash: '0x01', |
||||
internalTransactionHtml: 'test 2' |
||||
}, |
||||
{ |
||||
fromAddressHash: '0x01', |
||||
toAddressHash: '0x00', |
||||
internalTransactionHtml: 'test 3' |
||||
}] |
||||
} |
||||
const output = reducer(state, action) |
||||
|
||||
expect(output.items).toEqual(['test 2']) |
||||
}) |
||||
}) |
@ -0,0 +1,96 @@ |
||||
import $ from 'jquery' |
||||
import _ from 'lodash' |
||||
import humps from 'humps' |
||||
import numeral from 'numeral' |
||||
import socket from '../../socket' |
||||
import { batchChannel } from '../../lib/utils' |
||||
import { connectElements } from '../../lib/redux_helpers.js' |
||||
import { createAsyncLoadStore } from '../../lib/async_listing_load' |
||||
|
||||
const BATCH_THRESHOLD = 10 |
||||
|
||||
export const initialState = { |
||||
channelDisconnected: false, |
||||
addressHash: null, |
||||
filter: null, |
||||
internalTransactionsBatch: [] |
||||
} |
||||
|
||||
export function reducer (state, action) { |
||||
switch (action.type) { |
||||
case 'PAGE_LOAD': |
||||
case 'ELEMENTS_LOAD': { |
||||
return Object.assign({}, state, _.omit(action, 'type')) |
||||
} |
||||
case 'CHANNEL_DISCONNECTED': { |
||||
if (state.beyondPageOne) return state |
||||
|
||||
return Object.assign({}, state, { |
||||
channelDisconnected: true, |
||||
internalTransactionsBatch: [] |
||||
}) |
||||
} |
||||
case 'RECEIVED_NEW_INTERNAL_TRANSACTION_BATCH': { |
||||
if (state.channelDisconnected || state.beyondPageOne) return state |
||||
|
||||
const incomingInternalTransactions = action.msgs |
||||
.filter(({toAddressHash, fromAddressHash}) => ( |
||||
!state.filter || |
||||
(state.filter === 'to' && toAddressHash === state.addressHash) || |
||||
(state.filter === 'from' && fromAddressHash === state.addressHash) |
||||
)).map(msg => msg.internalTransactionHtml) |
||||
|
||||
if (!state.internalTransactionsBatch.length && incomingInternalTransactions.length < BATCH_THRESHOLD) { |
||||
return Object.assign({}, state, { |
||||
items: [ |
||||
...incomingInternalTransactions.reverse(), |
||||
...state.items |
||||
] |
||||
}) |
||||
} else { |
||||
return Object.assign({}, state, { |
||||
internalTransactionsBatch: [ |
||||
...incomingInternalTransactions.reverse(), |
||||
...state.internalTransactionsBatch |
||||
] |
||||
}) |
||||
} |
||||
} |
||||
default: |
||||
return state |
||||
} |
||||
} |
||||
|
||||
const elements = { |
||||
'[data-selector="channel-disconnected-message"]': { |
||||
render ($el, state) { |
||||
if (state.channelDisconnected) $el.show() |
||||
} |
||||
}, |
||||
'[data-selector="channel-batching-count"]': { |
||||
render ($el, state, oldState) { |
||||
const $channelBatching = $('[data-selector="channel-batching-message"]') |
||||
if (!state.internalTransactionsBatch.length) return $channelBatching.hide() |
||||
$channelBatching.show() |
||||
$el[0].innerHTML = numeral(state.internalTransactionsBatch.length).format() |
||||
} |
||||
} |
||||
} |
||||
|
||||
if ($('[data-page="address-internal-transactions"]').length) { |
||||
const store = createAsyncLoadStore(reducer, initialState, 'dataset.key') |
||||
const addressHash = $('[data-page="address-details"]')[0].dataset.pageAddressHash |
||||
|
||||
store.dispatch({type: 'PAGE_LOAD', addressHash}) |
||||
connectElements({ store, elements }) |
||||
|
||||
const addressChannel = socket.channel(`addresses:${addressHash}`, {}) |
||||
addressChannel.join() |
||||
addressChannel.onError(() => store.dispatch({ |
||||
type: 'CHANNEL_DISCONNECTED' |
||||
})) |
||||
addressChannel.on('internal_transaction', batchChannel((msgs) => store.dispatch({ |
||||
type: 'RECEIVED_NEW_INTERNAL_TRANSACTION_BATCH', |
||||
msgs: humps.camelizeKeys(msgs) |
||||
}))) |
||||
} |
Loading…
Reference in new issue