Skip to content
This repository has been archived by the owner on Apr 15, 2019. It is now read-only.

Commit

Permalink
Remove constants
Browse files Browse the repository at this point in the history
  • Loading branch information
Gina Contrino committed Nov 2, 2017
1 parent 5517c01 commit 6abe551
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 26 deletions.
5 changes: 0 additions & 5 deletions src/constants/api.js

This file was deleted.

5 changes: 2 additions & 3 deletions src/store/middlewares/account.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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) {
Expand Down Expand Up @@ -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);
}

Expand Down
10 changes: 4 additions & 6 deletions src/store/middlewares/account.test.js
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -35,15 +33,15 @@ describe('Account middleware', () => {
const newBlockCreated = {
type: actionTypes.newBlockCreated,
data: {
interval: SYNC_ACTIVE_INTERVAL,
windowIsFocused: true,
block: transactions,
},
};

const inactiveNewBlockCreated = {
type: actionTypes.newBlockCreated,
data: {
interval: SYNC_INACTIVE_INTERVAL,
windowIsFocused: false,
block: transactions,
},
};
Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand Down
9 changes: 4 additions & 5 deletions src/store/middlewares/socket.js
Original file line number Diff line number Diff line change
@@ -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', () => {
Expand Down
12 changes: 5 additions & 7 deletions src/store/middlewares/socket.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 },
});
});

Expand Down Expand Up @@ -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 },
});
});
});
Expand Down

0 comments on commit 6abe551

Please sign in to comment.