import React, { useRef } from 'react'; import { doPolling } from '../../../../'; import { fetchTodo } from '../service/todo'; let count = 0; export const RegularPolling = () => { const pollingRef = useRef>(); const shouldStop = useRef(false); const poll = () => { pollingRef.current?.abort(); pollingRef.current = doPolling(({ signal }) => fetchTodo(1, signal), { until: props => { console.log(`until`, props); return count++ === 5; }, breakIf: props => { console.log(`breakIf`, props); return shouldStop.current; }, onStart: () => { console.log(`onStart`, 'onStart'); }, onFinish: props => { console.log(`onFinish`, props); count = 0; }, onNext: props => { console.log(`onNext`, props); }, onComplete: props => { console.log(`onComplete`, props); }, onError: props => { console.log(`onError`, props); }, onBreak(props) { console.log(`onBreak`, props); shouldStop.current = false; }, onIntervalError(props) { console.log(`onIntervalError`, props); }, onTooManyAttempts(props) { console.log(`onTooManyAttempts`, props); }, onTooManyErrors(props) { console.log(`onTooManyErrors`, props); }, interval: 1000, }); pollingRef.current.init().then(props => { console.log(`init`, props); }); }; const abortPolling = () => { pollingRef.current?.abort().then(props => { console.log(`abort`, props); }); shouldStop.current = false; }; const stopPolling = () => { shouldStop.current = true; }; return ( <> ); };