doFetchActiveLivestreams: add interval and options checking
- Added a default minimum of 5 minutes between fetches. Clients can bypass this through `forceFetch` if needed. - We'll need to support different 'orderBy', so adding an "options check" when determining if we just made the same fetch.
This commit is contained in:
parent
7515ed2704
commit
64effb4679
4 changed files with 45 additions and 19 deletions
2
flow-typed/livestream.js
vendored
2
flow-typed/livestream.js
vendored
|
@ -26,6 +26,8 @@ declare type LivestreamState = {
|
||||||
viewersById: {},
|
viewersById: {},
|
||||||
fetchingActiveLivestreams: boolean,
|
fetchingActiveLivestreams: boolean,
|
||||||
activeLivestreams: ?LivestreamInfo,
|
activeLivestreams: ?LivestreamInfo,
|
||||||
|
activeLivestreamsLastFetchedDate: number,
|
||||||
|
activeLivestreamsLastFetchedOptions: {},
|
||||||
}
|
}
|
||||||
|
|
||||||
declare type LivestreamInfo = {
|
declare type LivestreamInfo = {
|
||||||
|
|
|
@ -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';
|
||||||
|
|
|
@ -33,19 +33,34 @@ 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 = (
|
||||||
type: ACTIONS.FETCH_ACTIVE_LIVESTREAMS_STARTED,
|
orderBy: Array<string> = ['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.activeLivestreamsLastFetchedDate;
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
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;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -64,11 +79,11 @@ export const doFetchActiveLivestreams = () => {
|
||||||
// live. The UI usually just wants to report the latest claim, so we
|
// live. The UI usually just wants to report the latest claim, so we
|
||||||
// query that store it in `latestClaimUri`.
|
// query that store it in `latestClaimUri`.
|
||||||
doClaimSearch({
|
doClaimSearch({
|
||||||
page_size: 50,
|
page_size: nextOptions.page_size,
|
||||||
has_no_source: true,
|
has_no_source: true,
|
||||||
channel_ids: Object.keys(activeLivestreams),
|
channel_ids: Object.keys(activeLivestreams),
|
||||||
claim_type: ['stream'],
|
claim_type: ['stream'],
|
||||||
order_by: ['release_time'], // **
|
order_by: nextOptions.order_by, // **
|
||||||
limit_claims_per_channel: 1, // **
|
limit_claims_per_channel: 1, // **
|
||||||
no_totals: true,
|
no_totals: true,
|
||||||
})
|
})
|
||||||
|
@ -88,19 +103,19 @@ export const doFetchActiveLivestreams = () => {
|
||||||
|
|
||||||
dispatch({
|
dispatch({
|
||||||
type: ACTIONS.FETCH_ACTIVE_LIVESTREAMS_COMPLETED,
|
type: ACTIONS.FETCH_ACTIVE_LIVESTREAMS_COMPLETED,
|
||||||
data: activeLivestreams,
|
data: {
|
||||||
|
activeLivestreams,
|
||||||
|
activeLivestreamsLastFetchedDate: now,
|
||||||
|
activeLivestreamsLastFetchedOptions: nextOptions,
|
||||||
|
},
|
||||||
});
|
});
|
||||||
})
|
})
|
||||||
.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,
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
|
@ -7,6 +7,8 @@ const defaultState: LivestreamState = {
|
||||||
viewersById: {},
|
viewersById: {},
|
||||||
fetchingActiveLivestreams: false,
|
fetchingActiveLivestreams: false,
|
||||||
activeLivestreams: null,
|
activeLivestreams: null,
|
||||||
|
activeLivestreamsLastFetchedDate: 0,
|
||||||
|
activeLivestreamsLastFetchedOptions: {},
|
||||||
};
|
};
|
||||||
|
|
||||||
export default handleActions(
|
export default handleActions(
|
||||||
|
@ -45,8 +47,14 @@ 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, activeLivestreamsLastFetchedDate, activeLivestreamsLastFetchedOptions } = action.data;
|
||||||
return { ...state, fetchingActiveLivestreams: false, activeLivestreams };
|
return {
|
||||||
|
...state,
|
||||||
|
fetchingActiveLivestreams: false,
|
||||||
|
activeLivestreams,
|
||||||
|
activeLivestreamsLastFetchedDate,
|
||||||
|
activeLivestreamsLastFetchedOptions,
|
||||||
|
};
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
defaultState
|
defaultState
|
||||||
|
|
Loading…
Add table
Reference in a new issue