Skip to content

Commit 03578ef

Browse files
committed
move onRenderComplete logic to embeddable
1 parent 4907fe1 commit 03578ef

2 files changed

Lines changed: 24 additions & 27 deletions

File tree

x-pack/plugins/maps/public/embeddable/map_embeddable.tsx

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import { Provider } from 'react-redux';
1212
import fastIsEqual from 'fast-deep-equal';
1313
import { render, unmountComponentAtNode } from 'react-dom';
1414
import { Subscription } from 'rxjs';
15+
import { debounceTime, distinctUntilChanged, filter, skip, startWith } from 'rxjs/operators';
1516
import { Unsubscribe } from 'redux';
1617
import { EuiEmptyPrompt } from '@elastic/eui';
1718
import { type Filter } from '@kbn/es-query';
@@ -68,6 +69,7 @@ import {
6869
getFullPath,
6970
MAP_SAVED_OBJECT_TYPE,
7071
RawValue,
72+
RENDER_TIMEOUT,
7173
} from '../../common/constants';
7274
import { RenderToolTipContent } from '../classes/tooltips/tooltip_property';
7375
import {
@@ -130,6 +132,7 @@ export class MapEmbeddable
130132
private _onInitialRenderComplete?: () => void = undefined;
131133
private _hasInitialRenderCompleteFired = false;
132134
private _isSharable = true;
135+
private readonly _onRenderComplete$;
133136

134137
constructor(config: MapEmbeddableConfig, initialInput: MapEmbeddableInput, parent?: IContainer) {
135138
super(
@@ -147,6 +150,19 @@ export class MapEmbeddable
147150
this._initializeSaveMap();
148151
this._subscriptions.push(this.getUpdated$().subscribe(() => this.onUpdate()));
149152
this._controlledBy = getControlledBy(this.id);
153+
154+
this._onRenderComplete$ = this.getOutput$().pipe(
155+
// wrapping distinctUntilChanged with startWith and skip to prime distinctUntilChanged with an initial value.
156+
startWith(this.getOutput()),
157+
distinctUntilChanged((a, b) => a.loading === b.loading),
158+
skip(1),
159+
debounceTime(RENDER_TIMEOUT),
160+
filter(output => !output.loading)
161+
);
162+
}
163+
164+
public getOnRenderComplete$() {
165+
return this._onRenderComplete$;
150166
}
151167

152168
public reportsEmbeddableLoad() {

x-pack/plugins/maps/public/lens/passive_map.tsx

Lines changed: 8 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,12 @@
66
*/
77

88
import React, { Component, RefObject } from 'react';
9-
import { debounce } from 'lodash';
109
import { Subscription } from 'rxjs';
11-
import { distinctUntilChanged, skip, startWith } from 'rxjs/operators';
1210
import { v4 as uuidv4 } from 'uuid';
1311
import { EuiLoadingChart } from '@elastic/eui';
1412
import { EmbeddableFactory, ViewMode } from '@kbn/embeddable-plugin/public';
1513
import type { LayerDescriptor } from '../../common/descriptor_types';
1614
import { INITIAL_LOCATION } from '../../common';
17-
import { RENDER_TIMEOUT } from '../../common/constants';
1815
import { MapEmbeddable, MapEmbeddableInput, MapEmbeddableOutput } from '../embeddable';
1916
import { createBasemapLayerDescriptor } from '../classes/layers/create_basemap_layer_descriptor';
2017

@@ -39,7 +36,7 @@ export class PassiveMap extends Component<Props, State> {
3936
private _isMounted = false;
4037
private _prevPassiveLayer = this.props.passiveLayer;
4138
private readonly _embeddableRef: RefObject<HTMLDivElement> = React.createRef<HTMLDivElement>();
42-
private _outputSubscription: Subscription | undefined;
39+
private _onRenderSubscription: Subscription | undefined;
4340

4441
state: State = { mapEmbeddable: null };
4542

@@ -53,8 +50,8 @@ export class PassiveMap extends Component<Props, State> {
5350
if (this.state.mapEmbeddable) {
5451
this.state.mapEmbeddable.destroy();
5552
}
56-
if (this._outputSubscription) {
57-
this._outputSubscription.unsubscribe();
53+
if (this._onRenderSubscription) {
54+
this._onRenderSubscription.unsubscribe();
5855
}
5956
}
6057

@@ -65,14 +62,6 @@ export class PassiveMap extends Component<Props, State> {
6562
}
6663
}
6764

68-
_onRenderComplete = debounce(() => {
69-
if (!this._isMounted) {
70-
return;
71-
}
72-
73-
this.props.onRenderComplete();
74-
}, RENDER_TIMEOUT);
75-
7665
async _setupEmbeddable() {
7766
const basemapLayerDescriptor = createBasemapLayerDescriptor();
7867
const intialLayers = basemapLayerDescriptor ? [basemapLayerDescriptor] : [];
@@ -101,19 +90,11 @@ export class PassiveMap extends Component<Props, State> {
10190
return;
10291
}
10392

104-
this._outputSubscription = mapEmbeddable
105-
.getOutput$()
106-
.pipe(
107-
// wrapping distinctUntilChanged with startWith and skip to prime distinctUntilChanged with an initial value.
108-
startWith(mapEmbeddable.getOutput()),
109-
distinctUntilChanged((a, b) => a.loading === b.loading),
110-
skip(1)
111-
)
112-
.subscribe((output) => {
113-
if (output.loading) {
114-
this._onRenderComplete.cancel();
115-
} else {
116-
this._onRenderComplete();
93+
this._onRenderSubscription = mapEmbeddable
94+
.getOnRenderComplete$()
95+
.subscribe(() => {
96+
if (this._isMounted) {
97+
this.props.onRenderComplete();
11798
}
11899
});
119100

0 commit comments

Comments
 (0)