2021-10-17 10:36:14 +02:00
|
|
|
// @flow
|
|
|
|
import { Lbryio } from 'lbryinc';
|
|
|
|
import * as ACTIONS from 'constants/action_types';
|
|
|
|
|
2021-10-28 06:50:17 +02:00
|
|
|
const FETCH_SUB_COUNT_MIN_INTERVAL_MS = 5 * 60 * 1000;
|
|
|
|
const FETCH_SUB_COUNT_IDLE_FIRE_MS = 100;
|
|
|
|
|
2021-10-17 10:36:14 +02:00
|
|
|
export const doFetchViewCount = (claimIdCsv: string) => (dispatch: Dispatch) => {
|
|
|
|
dispatch({ type: ACTIONS.FETCH_VIEW_COUNT_STARTED });
|
|
|
|
|
|
|
|
return Lbryio.call('file', 'view_count', { claim_id: claimIdCsv })
|
|
|
|
.then((result: Array<number>) => {
|
|
|
|
const viewCounts = result;
|
|
|
|
dispatch({ type: ACTIONS.FETCH_VIEW_COUNT_COMPLETED, data: { claimIdCsv, viewCounts } });
|
|
|
|
})
|
2021-10-28 06:50:17 +02:00
|
|
|
.catch((error) => {
|
2021-10-17 10:36:14 +02:00
|
|
|
dispatch({ type: ACTIONS.FETCH_VIEW_COUNT_FAILED, data: error });
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2021-10-28 06:50:17 +02:00
|
|
|
const executeFetchSubCount = (claimIdCsv: string) => (dispatch: Dispatch, getState: GetState) => {
|
|
|
|
const state = getState();
|
|
|
|
const subCountLastFetchedById = state.stats.subCountLastFetchedById;
|
|
|
|
const now = Date.now();
|
|
|
|
|
|
|
|
const claimIds = claimIdCsv.split(',').filter((id) => {
|
|
|
|
const prev = subCountLastFetchedById[id];
|
|
|
|
return !prev || now - prev > FETCH_SUB_COUNT_MIN_INTERVAL_MS;
|
|
|
|
});
|
|
|
|
|
|
|
|
if (claimIds.length === 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-10-17 10:36:14 +02:00
|
|
|
dispatch({ type: ACTIONS.FETCH_SUB_COUNT_STARTED });
|
|
|
|
|
2021-10-28 06:50:17 +02:00
|
|
|
return Lbryio.call('subscription', 'sub_count', { claim_id: claimIds.join(',') })
|
2021-10-17 10:36:14 +02:00
|
|
|
.then((result: Array<number>) => {
|
2021-10-28 06:50:17 +02:00
|
|
|
const subCounts = result;
|
2021-10-17 10:36:14 +02:00
|
|
|
dispatch({
|
|
|
|
type: ACTIONS.FETCH_SUB_COUNT_COMPLETED,
|
2022-03-01 15:25:52 +01:00
|
|
|
data: { claimIds, subCounts, fetchDate: now },
|
2021-10-17 10:36:14 +02:00
|
|
|
});
|
|
|
|
})
|
2021-10-28 06:50:17 +02:00
|
|
|
.catch((error) => {
|
2021-10-17 10:36:14 +02:00
|
|
|
dispatch({ type: ACTIONS.FETCH_SUB_COUNT_FAILED, data: error });
|
|
|
|
});
|
|
|
|
};
|
2021-10-28 06:50:17 +02:00
|
|
|
|
|
|
|
let fetchSubCountTimer;
|
|
|
|
let fetchSubCountQueue = '';
|
|
|
|
|
|
|
|
export const doFetchSubCount = (claimIdCsv: string) => (dispatch: Dispatch) => {
|
|
|
|
if (fetchSubCountTimer) {
|
|
|
|
clearTimeout(fetchSubCountTimer);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (fetchSubCountQueue && !fetchSubCountQueue.endsWith(',')) {
|
|
|
|
fetchSubCountQueue += ',';
|
|
|
|
}
|
|
|
|
|
|
|
|
fetchSubCountQueue += claimIdCsv;
|
|
|
|
|
|
|
|
fetchSubCountTimer = setTimeout(() => {
|
|
|
|
dispatch(executeFetchSubCount(fetchSubCountQueue));
|
|
|
|
fetchSubCountQueue = '';
|
|
|
|
}, FETCH_SUB_COUNT_IDLE_FIRE_MS);
|
|
|
|
};
|