Skip to content

Commit

Permalink
doFetchActiveLivestreams: add interval check
Browse files Browse the repository at this point in the history
- Added a default minimum of 5 minutes between fetches. Clients can bypass this through `forceFetch` if needed.
  • Loading branch information
infinite-persistence committed Sep 20, 2021
1 parent f6578e3 commit 4cb83fc
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 17 deletions.
1 change: 1 addition & 0 deletions flow-typed/livestream.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ declare type LivestreamState = {
viewersById: {},
fetchingActiveLivestreams: boolean,
activeLivestreams: ?LivestreamInfo,
lastFetchedActiveLivestreams: number,
}

declare type LivestreamInfo = {
Expand Down
1 change: 1 addition & 0 deletions ui/constants/action_types.js
Original file line number Diff line number Diff line change
Expand Up @@ -354,4 +354,5 @@ export const FETCH_NO_SOURCE_CLAIMS_FAILED = 'FETCH_NO_SOURCE_CLAIMS_FAILED';
export const VIEWERS_RECEIVED = 'VIEWERS_RECEIVED';
export const FETCH_ACTIVE_LIVESTREAMS_STARTED = 'FETCH_ACTIVE_LIVESTREAMS_STARTED';
export const FETCH_ACTIVE_LIVESTREAMS_FAILED = 'FETCH_ACTIVE_LIVESTREAMS_FAILED';
export const FETCH_ACTIVE_LIVESTREAMS_SKIPPED = 'FETCH_ACTIVE_LIVESTREAMS_SKIPPED';
export const FETCH_ACTIVE_LIVESTREAMS_COMPLETED = 'FETCH_ACTIVE_LIVESTREAMS_COMPLETED';
36 changes: 21 additions & 15 deletions ui/redux/actions/livestream.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,19 +33,26 @@ export const doFetchNoSourceClaims = (channelId: string) => async (dispatch: Dis
}
};

export const doFetchActiveLivestreams = () => {
return async (dispatch: Dispatch) => {
dispatch({
type: ACTIONS.FETCH_ACTIVE_LIVESTREAMS_STARTED,
});
const FETCH_ACTIVE_LIVESTREAMS_MIN_INTERVAL_MS = 5 * 60 * 1000;

export const doFetchActiveLivestreams = (forceFetch: boolean) => {
return async (dispatch: Dispatch, getState: GetState) => {
const state = getState();
const now = Date.now();
const timeDelta = now - state.livestream.lastFetchedActiveLivestreams;

if (!forceFetch && timeDelta < FETCH_ACTIVE_LIVESTREAMS_MIN_INTERVAL_MS) {
dispatch({ type: ACTIONS.FETCH_ACTIVE_LIVESTREAMS_SKIPPED });
return;
}

dispatch({ type: ACTIONS.FETCH_ACTIVE_LIVESTREAMS_STARTED });

fetch(LIVESTREAM_LIVE_API)
.then((res) => res.json())
.then((res) => {
if (!res.data) {
dispatch({
type: ACTIONS.FETCH_ACTIVE_LIVESTREAMS_FAILED,
});
dispatch({ type: ACTIONS.FETCH_ACTIVE_LIVESTREAMS_FAILED });
return;
}

Expand Down Expand Up @@ -88,19 +95,18 @@ export const doFetchActiveLivestreams = () => {

dispatch({
type: ACTIONS.FETCH_ACTIVE_LIVESTREAMS_COMPLETED,
data: activeLivestreams,
data: {
activeLivestreams,
lastFetchedActiveLivestreams: now,
},
});
})
.catch(() => {
dispatch({
type: ACTIONS.FETCH_ACTIVE_LIVESTREAMS_FAILED,
});
dispatch({ type: ACTIONS.FETCH_ACTIVE_LIVESTREAMS_FAILED });
});
})
.catch((err) => {
dispatch({
type: ACTIONS.FETCH_ACTIVE_LIVESTREAMS_FAILED,
});
dispatch({ type: ACTIONS.FETCH_ACTIVE_LIVESTREAMS_FAILED });
});
};
};
10 changes: 8 additions & 2 deletions ui/redux/reducers/livestream.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const defaultState: LivestreamState = {
viewersById: {},
fetchingActiveLivestreams: false,
activeLivestreams: null,
lastFetchedActiveLivestreams: 0,
};

export default handleActions(
Expand Down Expand Up @@ -45,8 +46,13 @@ export default handleActions(
return { ...state, fetchingActiveLivestreams: false };
},
[ACTIONS.FETCH_ACTIVE_LIVESTREAMS_COMPLETED]: (state: LivestreamState, action: any) => {
const activeLivestreams: LivestreamInfo = action.data;
return { ...state, fetchingActiveLivestreams: false, activeLivestreams };
const { activeLivestreams, lastFetchedActiveLivestreams } = action.data;
return {
...state,
fetchingActiveLivestreams: false,
activeLivestreams,
lastFetchedActiveLivestreams,
};
},
},
defaultState
Expand Down

0 comments on commit 4cb83fc

Please sign in to comment.