Skip to content

Commit

Permalink
[Lens] Fix console.error react memory leak when saving new viz (#65490)…
Browse files Browse the repository at this point in the history
… (#65713)
  • Loading branch information
mbondyra authored May 7, 2020
1 parent ca05440 commit 24be297
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* you may not use this file except in compliance with the Elastic License.
*/

import React, { useState, useMemo, memo, FunctionComponent } from 'react';
import React, { useState, useMemo, useEffect, memo, FunctionComponent } from 'react';
import { debounce } from 'lodash';

/**
Expand All @@ -17,7 +17,11 @@ export function debouncedComponent<TProps>(component: FunctionComponent<TProps>,

return (props: TProps) => {
const [cachedProps, setCachedProps] = useState(props);
const delayRender = useMemo(() => debounce(setCachedProps, delay), []);
const debouncePropsChange = debounce(setCachedProps, delay);
const delayRender = useMemo(() => debouncePropsChange, []);

// cancel debounced prop change if component has been unmounted in the meantime
useEffect(() => () => debouncePropsChange.cancel(), []);

delayRender(props);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ export function EditorFrame(props: EditorFrameProps) {

// Initialize current datasource and all active datasources
useEffect(() => {
// prevents executing dispatch on unmounted component
let isUnmounted = false;
if (!allLoaded) {
Object.entries(props.datasourceMap).forEach(([datasourceId, datasource]) => {
if (
Expand All @@ -70,16 +72,21 @@ export function EditorFrame(props: EditorFrameProps) {
datasource
.initialize(state.datasourceStates[datasourceId].state || undefined)
.then(datasourceState => {
dispatch({
type: 'UPDATE_DATASOURCE_STATE',
updater: datasourceState,
datasourceId,
});
if (!isUnmounted) {
dispatch({
type: 'UPDATE_DATASOURCE_STATE',
updater: datasourceState,
datasourceId,
});
}
})
.catch(onError);
}
});
}
return () => {
isUnmounted = true;
};
}, [allLoaded]);

const datasourceLayers: Record<string, DatasourcePublicAPI> = {};
Expand Down

0 comments on commit 24be297

Please sign in to comment.