5dd5826b33
1. The API supports batching -- updated the code to use that. Retained string as the parameter (instead of changing it to array) so that existing clients won't be affected. 2. Make `doFetchSubCount` a batched command by default through an idle timer. This way, none of the clients need to collect IDs -- it's all done behind the scenes. 3. Added minimum of 5 minutes between each sub-count fetch for a claim ID.
68 lines
2.1 KiB
JavaScript
68 lines
2.1 KiB
JavaScript
// @flow
|
|
import { Lbryio } from 'lbryinc';
|
|
import * as ACTIONS from 'constants/action_types';
|
|
|
|
const FETCH_SUB_COUNT_MIN_INTERVAL_MS = 5 * 60 * 1000;
|
|
const FETCH_SUB_COUNT_IDLE_FIRE_MS = 100;
|
|
|
|
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 } });
|
|
})
|
|
.catch((error) => {
|
|
dispatch({ type: ACTIONS.FETCH_VIEW_COUNT_FAILED, data: error });
|
|
});
|
|
};
|
|
|
|
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;
|
|
}
|
|
|
|
dispatch({ type: ACTIONS.FETCH_SUB_COUNT_STARTED });
|
|
|
|
return Lbryio.call('subscription', 'sub_count', { claim_id: claimIds.join(',') })
|
|
.then((result: Array<number>) => {
|
|
const subCounts = result;
|
|
dispatch({
|
|
type: ACTIONS.FETCH_SUB_COUNT_COMPLETED,
|
|
data: { claimIdCsv, subCounts, fetchDate: now },
|
|
});
|
|
})
|
|
.catch((error) => {
|
|
dispatch({ type: ACTIONS.FETCH_SUB_COUNT_FAILED, data: error });
|
|
});
|
|
};
|
|
|
|
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);
|
|
};
|