doFetchActiveLivestreams: add interval check

- Added a default minimum of 5 minutes between fetches. Clients can bypass this through `forceFetch` if needed.
This commit is contained in:
infinite-persistence 2021-09-20 10:30:23 +08:00
parent dcb44e647d
commit 8340783e04
No known key found for this signature in database
GPG key ID: B9C3252EDC3D0AA0
4 changed files with 31 additions and 17 deletions

View file

@ -26,6 +26,7 @@ declare type LivestreamState = {
viewersById: {}, viewersById: {},
fetchingActiveLivestreams: boolean, fetchingActiveLivestreams: boolean,
activeLivestreams: ?LivestreamInfo, activeLivestreams: ?LivestreamInfo,
lastFetchedActiveLivestreams: number,
} }
declare type LivestreamInfo = { declare type LivestreamInfo = {

View file

@ -354,4 +354,5 @@ export const FETCH_NO_SOURCE_CLAIMS_FAILED = 'FETCH_NO_SOURCE_CLAIMS_FAILED';
export const VIEWERS_RECEIVED = 'VIEWERS_RECEIVED'; export const VIEWERS_RECEIVED = 'VIEWERS_RECEIVED';
export const FETCH_ACTIVE_LIVESTREAMS_STARTED = 'FETCH_ACTIVE_LIVESTREAMS_STARTED'; 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_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'; export const FETCH_ACTIVE_LIVESTREAMS_COMPLETED = 'FETCH_ACTIVE_LIVESTREAMS_COMPLETED';

View file

@ -33,19 +33,26 @@ export const doFetchNoSourceClaims = (channelId: string) => async (dispatch: Dis
} }
}; };
export const doFetchActiveLivestreams = () => { const FETCH_ACTIVE_LIVESTREAMS_MIN_INTERVAL_MS = 5 * 60 * 1000;
return async (dispatch: Dispatch) => {
dispatch({ export const doFetchActiveLivestreams = (forceFetch: boolean) => {
type: ACTIONS.FETCH_ACTIVE_LIVESTREAMS_STARTED, 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) fetch(LIVESTREAM_LIVE_API)
.then((res) => res.json()) .then((res) => res.json())
.then((res) => { .then((res) => {
if (!res.data) { if (!res.data) {
dispatch({ dispatch({ type: ACTIONS.FETCH_ACTIVE_LIVESTREAMS_FAILED });
type: ACTIONS.FETCH_ACTIVE_LIVESTREAMS_FAILED,
});
return; return;
} }
@ -88,19 +95,18 @@ export const doFetchActiveLivestreams = () => {
dispatch({ dispatch({
type: ACTIONS.FETCH_ACTIVE_LIVESTREAMS_COMPLETED, type: ACTIONS.FETCH_ACTIVE_LIVESTREAMS_COMPLETED,
data: activeLivestreams, data: {
activeLivestreams,
lastFetchedActiveLivestreams: now,
},
}); });
}) })
.catch(() => { .catch(() => {
dispatch({ dispatch({ type: ACTIONS.FETCH_ACTIVE_LIVESTREAMS_FAILED });
type: ACTIONS.FETCH_ACTIVE_LIVESTREAMS_FAILED,
});
}); });
}) })
.catch((err) => { .catch((err) => {
dispatch({ dispatch({ type: ACTIONS.FETCH_ACTIVE_LIVESTREAMS_FAILED });
type: ACTIONS.FETCH_ACTIVE_LIVESTREAMS_FAILED,
});
}); });
}; };
}; };

View file

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