Skip to content
This repository has been archived by the owner on Nov 17, 2023. It is now read-only.

Commit

Permalink
perf(wallet): prevent multiple concurrent page loads
Browse files Browse the repository at this point in the history
  • Loading branch information
mrfelton committed Oct 10, 2020
1 parent cecad88 commit e20f2db
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 4 deletions.
1 change: 1 addition & 0 deletions renderer/reducers/activity/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export const FETCH_ACTIVITY_HISTORY_FAILURE = 'FETCH_ACTIVITY_HISTORY_FAILURE'
export const SET_HAS_NEXT_PAGE = 'SET_HAS_NEXT_PAGE'
export const ERROR_DETAILS_DIALOG_ID = 'ERROR_DETAILS_DIALOG_ID'
export const SET_ERROR_DIALOG_DETAILS = 'SET_ERROR_DIALOG_DETAILS'
export const SET_PAGE_LOADING = 'SET_PAGE_LOADING'

export const defaultFilter = new Set([
'SENT_ACTIVITY',
Expand Down
36 changes: 32 additions & 4 deletions renderer/reducers/activity/reducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import { fetchChannels } from 'reducers/channels'
import { fetchInfo, infoSelectors } from 'reducers/info'
import { showError, showNotification } from 'reducers/notification'
import { createActivityPaginator, getItemType } from './utils'
import { hasNextPage } from './selectors'
import { hasNextPage, isPageLoading } from './selectors'
import messages from './messages'
import * as constants from './constants'

Expand All @@ -28,6 +28,7 @@ const {
ADD_FILTER,
SET_HAS_NEXT_PAGE,
SET_ERROR_DIALOG_DETAILS,
SET_PAGE_LOADING,
} = constants

// ------------------------------------
Expand All @@ -44,6 +45,7 @@ const {
* @property {boolean} isActivityLoading Boolean indicating if activity is loading.
* @property {Error|null} activityLoadingError Error from loading activity.
* @property {boolean} hasNextPage Boolean indicating if there are more activity pages to fetch.
* @property {boolean} isPageLoading Boolean indicating if the activity page is loading.
*/

/** @type {State} */
Expand All @@ -65,6 +67,7 @@ const initialState = {
isActivityLoading: false,
activityLoadingError: null,
hasNextPage: true,
isPageLoading: false,
}

// ------------------------------------
Expand Down Expand Up @@ -93,6 +96,19 @@ export const getPaginator = () => {
// Actions
// ------------------------------------

/**
* setPageLoading - Set boolean indicating if page is in the process of loading.
*
* @param {boolean} isPageLoading Boolean indicating if page is loading
* @returns {object} Action
*/
export const setPageLoading = isPageLoading => {
return {
type: SET_PAGE_LOADING,
isPageLoading,
}
}

/**
* setErorDialogDetails - Set the error dialog details.
*
Expand Down Expand Up @@ -240,12 +256,19 @@ export const resetPaginator = () => () => {
* @returns {(dispatch:Function, getState:Function) => Promise<void>} Thunk
*/
export const loadPage = (reload = false) => async (dispatch, getState) => {
const state = getState()

if (isPageLoading(state)) {
return
}
dispatch(setPageLoading(true))

await dispatch(fetchInfo())
const config = settingsSelectors.currentConfig(getState())
const blockHeight = infoSelectors.blockHeight(getState())
const config = settingsSelectors.currentConfig(state)
const blockHeight = infoSelectors.blockHeight(state)
const thisPaginator = getPaginator()

if (reload || hasNextPage(getState())) {
if (reload || hasNextPage(state)) {
const { pageSize } = config.activity
const { items, hasNextPage: paginatorHasNextPage } = await thisPaginator(pageSize, blockHeight)

Expand All @@ -260,6 +283,8 @@ export const loadPage = (reload = false) => async (dispatch, getState) => {
payments && dispatch(receivePayments(payments))
transactions && dispatch(receiveTransactions(transactions))
}

dispatch(setPageLoading(false))
}

/**
Expand Down Expand Up @@ -355,6 +380,9 @@ const ACTION_HANDLERS = {
[SET_ERROR_DIALOG_DETAILS]: (state, { details }) => {
state.errorDialogDetails = details
},
[SET_PAGE_LOADING]: (state, { isPageLoading }) => {
state.isPageLoading = isPageLoading
},
}

export default createReducer(initialState, ACTION_HANDLERS)
8 changes: 8 additions & 0 deletions renderer/reducers/activity/selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,14 @@ export const filter = state => state.activity.filter
*/
export const hasNextPage = state => state.activity.hasNextPage

/**
* isPageLoading - Activity page loading state.
*
* @param {State} state Redux state
* @returns {boolean} Boolean indicating if the activty pager is loading items.
*/
export const isPageLoading = state => state.activity.isPageLoading

/**
* filters - List of activity filters.
*
Expand Down

0 comments on commit e20f2db

Please sign in to comment.