44 * you may not use this file except in compliance with the Elastic License.
55 */
66import { useEffect , useState , useReducer , useCallback } from 'react' ;
7+ import { useMountedState } from 'react-use' ;
78import createContainer from 'constate' ;
89import { pick , throttle } from 'lodash' ;
910import { TimeKey , timeKeyIsBetween } from '../../../../common/time' ;
@@ -146,15 +147,20 @@ const useFetchEntriesEffect = (
146147 props : LogEntriesProps
147148) => {
148149 const { services } = useKibanaContextForPlugin ( ) ;
150+ const isMounted = useMountedState ( ) ;
149151 const [ prevParams , cachePrevParams ] = useState < LogEntriesProps | undefined > ( ) ;
150152 const [ startedStreaming , setStartedStreaming ] = useState ( false ) ;
153+ const dispatchIfMounted = useCallback ( ( action ) => ( isMounted ( ) ? dispatch ( action ) : undefined ) , [
154+ dispatch ,
155+ isMounted ,
156+ ] ) ;
151157
152158 const runFetchNewEntriesRequest = async ( overrides : Partial < LogEntriesProps > = { } ) => {
153159 if ( ! props . startTimestamp || ! props . endTimestamp ) {
154160 return ;
155161 }
156162
157- dispatch ( { type : Action . FetchingNewEntries } ) ;
163+ dispatchIfMounted ( { type : Action . FetchingNewEntries } ) ;
158164
159165 try {
160166 const commonFetchArgs : LogEntriesBaseRequest = {
@@ -175,13 +181,15 @@ const useFetchEntriesEffect = (
175181 } ;
176182
177183 const { data : payload } = await fetchLogEntries ( fetchArgs , services . http . fetch ) ;
178- dispatch ( { type : Action . ReceiveNewEntries , payload } ) ;
184+ dispatchIfMounted ( { type : Action . ReceiveNewEntries , payload } ) ;
179185
180186 // Move position to the bottom if it's the first load.
181187 // Do it in the next tick to allow the `dispatch` to fire
182188 if ( ! props . timeKey && payload . bottomCursor ) {
183189 setTimeout ( ( ) => {
184- props . jumpToTargetPosition ( payload . bottomCursor ! ) ;
190+ if ( isMounted ( ) ) {
191+ props . jumpToTargetPosition ( payload . bottomCursor ! ) ;
192+ }
185193 } ) ;
186194 } else if (
187195 props . timeKey &&
@@ -192,7 +200,7 @@ const useFetchEntriesEffect = (
192200 props . jumpToTargetPosition ( payload . topCursor ) ;
193201 }
194202 } catch ( e ) {
195- dispatch ( { type : Action . ErrorOnNewEntries } ) ;
203+ dispatchIfMounted ( { type : Action . ErrorOnNewEntries } ) ;
196204 }
197205 } ;
198206
@@ -210,7 +218,7 @@ const useFetchEntriesEffect = (
210218 return ;
211219 }
212220
213- dispatch ( { type : Action . FetchingMoreEntries } ) ;
221+ dispatchIfMounted ( { type : Action . FetchingMoreEntries } ) ;
214222
215223 try {
216224 const commonFetchArgs : LogEntriesBaseRequest = {
@@ -232,14 +240,14 @@ const useFetchEntriesEffect = (
232240
233241 const { data : payload } = await fetchLogEntries ( fetchArgs , services . http . fetch ) ;
234242
235- dispatch ( {
243+ dispatchIfMounted ( {
236244 type : getEntriesBefore ? Action . ReceiveEntriesBefore : Action . ReceiveEntriesAfter ,
237245 payload,
238246 } ) ;
239247
240248 return payload . bottomCursor ;
241249 } catch ( e ) {
242- dispatch ( { type : Action . ErrorOnMoreEntries } ) ;
250+ dispatchIfMounted ( { type : Action . ErrorOnMoreEntries } ) ;
243251 }
244252 } ;
245253
@@ -322,7 +330,7 @@ const useFetchEntriesEffect = (
322330 after : props . endTimestamp > prevParams . endTimestamp ,
323331 } ;
324332
325- dispatch ( { type : Action . ExpandRange , payload : shouldExpand } ) ;
333+ dispatchIfMounted ( { type : Action . ExpandRange , payload : shouldExpand } ) ;
326334 } ;
327335
328336 const expandRangeEffectDependencies = [
0 commit comments