Explore: Removes Promise.All from runQueries thunk (#16957)

This commit is contained in:
Hugo Häggmark
2019-05-09 12:24:48 +02:00
committed by GitHub
parent 2abb009d68
commit a04b3a13e0
3 changed files with 68 additions and 73 deletions

View File

@@ -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;