Commit 27948b13 by Dominik Prokop

Make language provider cancelable in Loki and Prometheus QueryField, to avoid…

Make language provider cancelable in Loki and Prometheus QueryField, to avoid setting state on unmounted component
parent db17262e
...@@ -16,6 +16,7 @@ import RunnerPlugin from 'app/features/explore/slate-plugins/runner'; ...@@ -16,6 +16,7 @@ import RunnerPlugin from 'app/features/explore/slate-plugins/runner';
// Types // Types
import { LokiQuery } from '../types'; import { LokiQuery } from '../types';
import { TypeaheadOutput } from 'app/types/explore'; import { TypeaheadOutput } from 'app/types/explore';
import { makePromiseCancelable, CancelablePromise } from 'app/core/utils/CancelablePromise';
const PRISM_SYNTAX = 'promql'; const PRISM_SYNTAX = 'promql';
...@@ -85,6 +86,7 @@ class LokiQueryField extends React.PureComponent<LokiQueryFieldProps, LokiQueryF ...@@ -85,6 +86,7 @@ class LokiQueryField extends React.PureComponent<LokiQueryFieldProps, LokiQueryF
languageProvider: any; languageProvider: any;
modifiedSearch: string; modifiedSearch: string;
modifiedQuery: string; modifiedQuery: string;
languageProviderInitializationPromise: CancelablePromise<any>;
constructor(props: LokiQueryFieldProps, context) { constructor(props: LokiQueryFieldProps, context) {
super(props, context); super(props, context);
...@@ -112,12 +114,24 @@ class LokiQueryField extends React.PureComponent<LokiQueryFieldProps, LokiQueryF ...@@ -112,12 +114,24 @@ class LokiQueryField extends React.PureComponent<LokiQueryFieldProps, LokiQueryF
componentDidMount() { componentDidMount() {
if (this.languageProvider) { if (this.languageProvider) {
this.languageProvider this.languageProviderInitializationPromise = makePromiseCancelable(this.languageProvider.start());
.start()
this.languageProviderInitializationPromise.promise
.then(remaining => { .then(remaining => {
remaining.map(task => task.then(this.onUpdateLanguage).catch(() => {})); remaining.map(task => task.then(this.onUpdateLanguage).catch(() => {}));
}) })
.then(() => this.onUpdateLanguage()); .then(() => this.onUpdateLanguage())
.catch(({ isCanceled }) => {
if (isCanceled) {
console.warn('LokiQueryField has unmounted, language provider intialization was canceled');
}
});
}
}
componentWillUnmount() {
if (this.languageProviderInitializationPromise) {
this.languageProviderInitializationPromise.cancel();
} }
} }
......
...@@ -12,6 +12,7 @@ import BracesPlugin from 'app/features/explore/slate-plugins/braces'; ...@@ -12,6 +12,7 @@ import BracesPlugin from 'app/features/explore/slate-plugins/braces';
import RunnerPlugin from 'app/features/explore/slate-plugins/runner'; import RunnerPlugin from 'app/features/explore/slate-plugins/runner';
import QueryField, { TypeaheadInput, QueryFieldState } from 'app/features/explore/QueryField'; import QueryField, { TypeaheadInput, QueryFieldState } from 'app/features/explore/QueryField';
import { PromQuery } from '../types'; import { PromQuery } from '../types';
import { CancelablePromise, makePromiseCancelable } from 'app/core/utils/CancelablePromise';
const HISTOGRAM_GROUP = '__histograms__'; const HISTOGRAM_GROUP = '__histograms__';
const METRIC_MARK = 'metric'; const METRIC_MARK = 'metric';
...@@ -104,6 +105,7 @@ interface PromQueryFieldState { ...@@ -104,6 +105,7 @@ interface PromQueryFieldState {
class PromQueryField extends React.PureComponent<PromQueryFieldProps, PromQueryFieldState> { class PromQueryField extends React.PureComponent<PromQueryFieldProps, PromQueryFieldState> {
plugins: any[]; plugins: any[];
languageProvider: any; languageProvider: any;
languageProviderInitializationPromise: CancelablePromise<any>;
constructor(props: PromQueryFieldProps, context) { constructor(props: PromQueryFieldProps, context) {
super(props, context); super(props, context);
...@@ -129,12 +131,23 @@ class PromQueryField extends React.PureComponent<PromQueryFieldProps, PromQueryF ...@@ -129,12 +131,23 @@ class PromQueryField extends React.PureComponent<PromQueryFieldProps, PromQueryF
componentDidMount() { componentDidMount() {
if (this.languageProvider) { if (this.languageProvider) {
this.languageProvider this.languageProviderInitializationPromise = makePromiseCancelable(this.languageProvider.start());
.start() this.languageProviderInitializationPromise.promise
.then(remaining => { .then(remaining => {
remaining.map(task => task.then(this.onUpdateLanguage).catch(() => {})); remaining.map(task => task.then(this.onUpdateLanguage).catch(() => {}));
}) })
.then(() => this.onUpdateLanguage()); .then(() => this.onUpdateLanguage())
.catch(({ isCanceled }) => {
if (isCanceled) {
console.warn('PromQueryField has unmounted, language provider intialization was canceled');
}
});
}
}
componentWillUnmount() {
if (this.languageProviderInitializationPromise) {
this.languageProviderInitializationPromise.cancel();
} }
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment