mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
Explore: Removes Promise.All from runQueries thunk (#16957)
This commit is contained in:
@@ -1,47 +1,48 @@
|
||||
import { PureComponent } from 'react';
|
||||
import { interval, Subscription, empty, Subject } from 'rxjs';
|
||||
import { tap, switchMap } from 'rxjs/operators';
|
||||
|
||||
import { stringToMs } from '../../utils/string';
|
||||
|
||||
interface Props {
|
||||
func: () => any; // TODO
|
||||
loading: boolean;
|
||||
interval: string;
|
||||
}
|
||||
|
||||
export class SetInterval extends PureComponent<Props> {
|
||||
private intervalId = 0;
|
||||
private propsSubject: Subject<Props>;
|
||||
private subscription: Subscription | null;
|
||||
|
||||
componentDidMount() {
|
||||
this.addInterval();
|
||||
constructor(props: Props) {
|
||||
super(props);
|
||||
this.propsSubject = new Subject<Props>();
|
||||
this.subscription = null;
|
||||
}
|
||||
|
||||
componentDidUpdate(prevProps: Props) {
|
||||
const { interval } = this.props;
|
||||
if (interval !== prevProps.interval) {
|
||||
this.clearInterval();
|
||||
this.addInterval();
|
||||
}
|
||||
componentDidMount() {
|
||||
this.subscription = this.propsSubject
|
||||
.pipe(
|
||||
switchMap(props => {
|
||||
return props.loading ? empty() : interval(stringToMs(props.interval));
|
||||
}),
|
||||
tap(() => this.props.func())
|
||||
)
|
||||
.subscribe();
|
||||
this.propsSubject.next(this.props);
|
||||
}
|
||||
|
||||
componentDidUpdate() {
|
||||
this.propsSubject.next(this.props);
|
||||
}
|
||||
|
||||
componentWillUnmount() {
|
||||
this.clearInterval();
|
||||
}
|
||||
|
||||
addInterval = () => {
|
||||
const { func, interval } = this.props;
|
||||
|
||||
if (interval) {
|
||||
func().then(() => {
|
||||
if (interval) {
|
||||
this.intervalId = window.setTimeout(() => {
|
||||
this.addInterval();
|
||||
}, stringToMs(interval));
|
||||
}
|
||||
});
|
||||
if (this.subscription) {
|
||||
this.subscription.unsubscribe();
|
||||
}
|
||||
};
|
||||
|
||||
clearInterval = () => {
|
||||
window.clearTimeout(this.intervalId);
|
||||
};
|
||||
this.propsSubject.unsubscribe();
|
||||
}
|
||||
|
||||
render() {
|
||||
return null;
|
||||
|
||||
Reference in New Issue
Block a user