UNPKG

@grafana/ui

Version:
56 lines (53 loc) 2.08 kB
import * as React from 'react'; import { useRef, useEffect } from 'react'; import { Subject, of, NEVER, interval } from 'rxjs'; import { switchMap, tap } from 'rxjs/operators'; import { stringToMs } from '@grafana/data'; import { RefreshPicker } from '../RefreshPicker/RefreshPicker.mjs'; "use strict"; const SetInterval = React.memo(({ func, loading, interval: intervalStr }) => { const propsSubjectRef = useRef(null); const subscriptionRef = useRef(null); const prevPropsRef = useRef({ func, loading, interval: intervalStr }); useEffect(() => { propsSubjectRef.current = new Subject(); subscriptionRef.current = propsSubjectRef.current.pipe( // switchMap creates a new observables based on the input stream, // which becomes part of the propsSubject stream switchMap((props) => { if (RefreshPicker.isLive(props.interval)) { return of({}); } return props.loading ? NEVER : interval(stringToMs(props.interval)); }), // tap will execute function passed by props // * on value from `of` stream merged if query is live // * on specified interval (triggered by values emitted by interval) tap(() => { propsSubjectRef.current && func(); }) ).subscribe(); propsSubjectRef.current.next({ func, loading, interval: intervalStr }); return () => { if (subscriptionRef.current) { subscriptionRef.current.unsubscribe(); } if (propsSubjectRef.current) { propsSubjectRef.current.complete(); } }; }, []); useEffect(() => { const prev = prevPropsRef.current; const currentProps = { func, loading, interval: intervalStr }; if (RefreshPicker.isLive(prev.interval) && RefreshPicker.isLive(currentProps.interval)) { return; } propsSubjectRef.current && propsSubjectRef.current.next(currentProps); prevPropsRef.current = currentProps; }, [func, loading, intervalStr]); return null; }); SetInterval.displayName = "SetInterval"; export { SetInterval }; //# sourceMappingURL=SetInterval.mjs.map