From 6abe551fb3cd49f7e45d10badfdc97966cdd466d Mon Sep 17 00:00:00 2001 From: Gina Contrino Date: Wed, 1 Nov 2017 16:37:01 +0100 Subject: [PATCH] Remove constants --- src/constants/api.js | 5 ----- src/store/middlewares/account.js | 5 ++--- src/store/middlewares/account.test.js | 10 ++++------ src/store/middlewares/socket.js | 9 ++++----- src/store/middlewares/socket.test.js | 12 +++++------- 5 files changed, 15 insertions(+), 26 deletions(-) delete mode 100644 src/constants/api.js diff --git a/src/constants/api.js b/src/constants/api.js deleted file mode 100644 index f11dc7ef4..000000000 --- a/src/constants/api.js +++ /dev/null @@ -1,5 +0,0 @@ -/** - * The interval of syncTick event - */ -export const SYNC_ACTIVE_INTERVAL = 10000; -export const SYNC_INACTIVE_INTERVAL = 120000; diff --git a/src/store/middlewares/account.js b/src/store/middlewares/account.js index 5a4e32675..5bf0c8a04 100644 --- a/src/store/middlewares/account.js +++ b/src/store/middlewares/account.js @@ -7,7 +7,6 @@ import actionTypes from '../../constants/actions'; import { fetchAndUpdateForgedBlocks } from '../../actions/forging'; import { getDelegate } from '../../utils/api/delegate'; import transactionTypes from '../../constants/transactionTypes'; -import { SYNC_ACTIVE_INTERVAL, SYNC_INACTIVE_INTERVAL } from '../../constants/api'; const updateTransactions = (store, peers, account) => { const maxBlockSize = 25; @@ -28,7 +27,7 @@ const updateAccountData = (store, action) => { getAccount(peers.data, account.address).then((result) => { if (result.balance !== account.balance) { - if (action.data.interval === SYNC_INACTIVE_INTERVAL) { + if (!action.data.windowIsFocused) { updateTransactions(store, peers, account); } if (account.isDelegate) { @@ -95,7 +94,7 @@ const checkTransactionsAndUpdateAccount = (store, action) => { const state = store.getState(); const { peers, account } = state; - if (action.data.interval === SYNC_ACTIVE_INTERVAL && hasRecentTransactions(state)) { + if (action.data.windowIsFocused && hasRecentTransactions(state)) { updateTransactions(store, peers, account); } diff --git a/src/store/middlewares/account.test.js b/src/store/middlewares/account.test.js index 493df40ee..7cb85dc4d 100644 --- a/src/store/middlewares/account.test.js +++ b/src/store/middlewares/account.test.js @@ -1,7 +1,5 @@ import { expect } from 'chai'; import { spy, stub } from 'sinon'; - -import { SYNC_ACTIVE_INTERVAL, SYNC_INACTIVE_INTERVAL } from '../../constants/api'; import { accountUpdated } from '../../actions/account'; import { activePeerUpdate } from '../../actions/peers'; import * as votingActions from '../../actions/voting'; @@ -35,7 +33,7 @@ describe('Account middleware', () => { const newBlockCreated = { type: actionTypes.newBlockCreated, data: { - interval: SYNC_ACTIVE_INTERVAL, + windowIsFocused: true, block: transactions, }, }; @@ -43,7 +41,7 @@ describe('Account middleware', () => { const inactiveNewBlockCreated = { type: actionTypes.newBlockCreated, data: { - interval: SYNC_INACTIVE_INTERVAL, + windowIsFocused: false, block: transactions, }, }; @@ -112,7 +110,7 @@ describe('Account middleware', () => { expect(stubTransactions).to.have.been.calledWith(); }); - it(`should call transactions API methods on ${actionTypes.newBlockCreated} action if account.balance changes and action.data.interval is SYNC_INACTIVE_INTERVAL`, () => { + it(`should call transactions API methods on ${actionTypes.newBlockCreated} action if account.balance changes and the window is in blur`, () => { stubGetAccount.resolves({ balance: 10e8 }); middleware(store)(next)(inactiveNewBlockCreated); @@ -121,7 +119,7 @@ describe('Account middleware', () => { expect(stubTransactions).to.have.been.calledWith(); }); - it(`should call transactions API methods on ${actionTypes.newBlockCreated} action if action.data.interval is SYNC_ACTIVE_INTERVAL and there are recent transactions`, () => { + it(`should call transactions API methods on ${actionTypes.newBlockCreated} action if the window is in focus and there are recent transactions`, () => { stubGetAccount.resolves({ balance: 0 }); middleware(store)(next)(newBlockCreated); diff --git a/src/store/middlewares/socket.js b/src/store/middlewares/socket.js index e85dd099a..73641ee6b 100644 --- a/src/store/middlewares/socket.js +++ b/src/store/middlewares/socket.js @@ -1,23 +1,22 @@ import io from '../../utils/socketShim'; import actionTypes from '../../constants/actions'; import { activePeerUpdate } from '../../actions/peers'; -import { SYNC_ACTIVE_INTERVAL, SYNC_INACTIVE_INTERVAL } from '../../constants/api'; let connection; let forcedClosing = false; const socketSetup = (store) => { - let interval = SYNC_ACTIVE_INTERVAL; + let windowIsFocused = true; const { ipc } = window; if (ipc) { - ipc.on('blur', () => { interval = SYNC_INACTIVE_INTERVAL; }); - ipc.on('focus', () => { interval = SYNC_ACTIVE_INTERVAL; }); + ipc.on('blur', () => { windowIsFocused = false; }); + ipc.on('focus', () => { windowIsFocused = true; }); } connection = io.connect(`ws://${store.getState().peers.data.currentPeer}:${store.getState().peers.data.port}`); connection.on('blocks/change', (block) => { store.dispatch({ type: actionTypes.newBlockCreated, - data: { block, interval }, + data: { block, windowIsFocused }, }); }); connection.on('disconnect', () => { diff --git a/src/store/middlewares/socket.test.js b/src/store/middlewares/socket.test.js index 44e644f25..56ea3634a 100644 --- a/src/store/middlewares/socket.test.js +++ b/src/store/middlewares/socket.test.js @@ -3,8 +3,6 @@ import { spy, stub } from 'sinon'; import io from './../../utils/socketShim'; import middleware from './socket'; import actionTypes from '../../constants/actions'; -import { SYNC_ACTIVE_INTERVAL, SYNC_INACTIVE_INTERVAL } from '../../constants/api'; - describe('Socket middleware', () => { let store; @@ -49,7 +47,7 @@ describe('Socket middleware', () => { expect(store.dispatch).to.have.been.calledWith({ type: actionTypes.newBlockCreated, - data: { block: transactions, interval: SYNC_ACTIVE_INTERVAL }, + data: { block: transactions, windowIsFocused: true }, }); }); @@ -107,25 +105,25 @@ describe('Socket middleware', () => { expect(window.ipc.on).to.have.been.calledWith('focus'); }); - it('should set window.ipc to set the interval to SYNC_INACTIVE_INTERVAL on blur', () => { + it('should register window focus changes', () => { middleware(store)(next)({ type: actionTypes.accountLoggedIn }); ipcCallbacks.blur(); socketCallbacks['blocks/change'](transactions); expect(store.dispatch).to.have.been.calledWith({ type: actionTypes.newBlockCreated, - data: { block: transactions, interval: SYNC_INACTIVE_INTERVAL }, + data: { block: transactions, windowIsFocused: false }, }); }); - it('should set window.ipc to set the interval to SYNC_ACTIVE_INTERVAL on focus', () => { + it('should register window focus changes', () => { middleware(store)(next)({ type: actionTypes.accountLoggedIn }); ipcCallbacks.focus(); socketCallbacks['blocks/change'](transactions); expect(store.dispatch).to.have.been.calledWith({ type: actionTypes.newBlockCreated, - data: { block: transactions, interval: SYNC_ACTIVE_INTERVAL }, + data: { block: transactions, windowIsFocused: true }, }); }); });