diff --git a/flow-typed/livestream.js b/flow-typed/livestream.js index 29743f06b..9bee62676 100644 --- a/flow-typed/livestream.js +++ b/flow-typed/livestream.js @@ -26,7 +26,8 @@ declare type LivestreamState = { viewersById: {}, fetchingActiveLivestreams: boolean, activeLivestreams: ?LivestreamInfo, - lastFetchedActiveLivestreams: number, + activeLivestreamsLastFetchedDate: number, + activeLivestreamsLastFetchedOptions: {}, } declare type LivestreamInfo = { diff --git a/ui/redux/actions/livestream.js b/ui/redux/actions/livestream.js index 1674a0f2d..3f94b5a4c 100644 --- a/ui/redux/actions/livestream.js +++ b/ui/redux/actions/livestream.js @@ -35,13 +35,21 @@ export const doFetchNoSourceClaims = (channelId: string) => async (dispatch: Dis const FETCH_ACTIVE_LIVESTREAMS_MIN_INTERVAL_MS = 5 * 60 * 1000; -export const doFetchActiveLivestreams = (forceFetch: boolean) => { +export const doFetchActiveLivestreams = ( + orderBy: Array = ['release_time'], + pageSize: number = 50, + forceFetch: boolean = false +) => { return async (dispatch: Dispatch, getState: GetState) => { const state = getState(); const now = Date.now(); - const timeDelta = now - state.livestream.lastFetchedActiveLivestreams; + const timeDelta = now - state.livestream.activeLivestreamsLastFetchedDate; - if (!forceFetch && timeDelta < FETCH_ACTIVE_LIVESTREAMS_MIN_INTERVAL_MS) { + const prevOptions = state.livestream.activeLivestreamsLastFetchedOptions; + const nextOptions = { page_size: pageSize, order_by: orderBy }; + const sameOptions = JSON.stringify(prevOptions) === JSON.stringify(nextOptions); + + if (!forceFetch && sameOptions && timeDelta < FETCH_ACTIVE_LIVESTREAMS_MIN_INTERVAL_MS) { dispatch({ type: ACTIONS.FETCH_ACTIVE_LIVESTREAMS_SKIPPED }); return; } @@ -71,11 +79,11 @@ export const doFetchActiveLivestreams = (forceFetch: boolean) => { // live. The UI usually just wants to report the latest claim, so we // query that store it in `latestClaimUri`. doClaimSearch({ - page_size: 50, + page_size: nextOptions.page_size, has_no_source: true, channel_ids: Object.keys(activeLivestreams), claim_type: ['stream'], - order_by: ['release_time'], // ** + order_by: nextOptions.order_by, // ** limit_claims_per_channel: 1, // ** no_totals: true, }) @@ -97,7 +105,8 @@ export const doFetchActiveLivestreams = (forceFetch: boolean) => { type: ACTIONS.FETCH_ACTIVE_LIVESTREAMS_COMPLETED, data: { activeLivestreams, - lastFetchedActiveLivestreams: now, + activeLivestreamsLastFetchedDate: now, + activeLivestreamsLastFetchedOptions: nextOptions, }, }); }) diff --git a/ui/redux/reducers/livestream.js b/ui/redux/reducers/livestream.js index fe89b618b..7a5d8af84 100644 --- a/ui/redux/reducers/livestream.js +++ b/ui/redux/reducers/livestream.js @@ -7,7 +7,8 @@ const defaultState: LivestreamState = { viewersById: {}, fetchingActiveLivestreams: false, activeLivestreams: null, - lastFetchedActiveLivestreams: 0, + activeLivestreamsLastFetchedDate: 0, + activeLivestreamsLastFetchedOptions: {}, }; export default handleActions( @@ -46,12 +47,13 @@ export default handleActions( return { ...state, fetchingActiveLivestreams: false }; }, [ACTIONS.FETCH_ACTIVE_LIVESTREAMS_COMPLETED]: (state: LivestreamState, action: any) => { - const { activeLivestreams, lastFetchedActiveLivestreams } = action.data; + const { activeLivestreams, activeLivestreamsLastFetchedDate, activeLivestreamsLastFetchedOptions } = action.data; return { ...state, fetchingActiveLivestreams: false, activeLivestreams, - lastFetchedActiveLivestreams, + activeLivestreamsLastFetchedDate, + activeLivestreamsLastFetchedOptions, }; }, },