-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #15 from GetStream/vishal/improved-implementation
Improving implementation to fix NativeModule exception issue
- Loading branch information
Showing
9 changed files
with
281 additions
and
231 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
import React, { MutableRefObject, useEffect, useRef } from 'react'; | ||
import { FlatList, FlatListProps, NativeModules, Platform } from 'react-native'; | ||
|
||
export const ScrollViewManager = NativeModules.MvcpScrollViewManager; | ||
|
||
export default (React.forwardRef( | ||
<T extends any>( | ||
props: FlatListProps<T>, | ||
forwardedRef: | ||
| ((instance: FlatList<T> | null) => void) | ||
| MutableRefObject<FlatList<T> | null> | ||
| null | ||
) => { | ||
const { maintainVisibleContentPosition: mvcp } = props; | ||
|
||
const flRef = useRef<FlatList<T> | null>(null); | ||
const isMvcpEnabled = useRef<any>(null); | ||
const autoscrollToTopThreshold = useRef<number | null>(); | ||
const minIndexForVisible = useRef<number>(); | ||
const handle = useRef<any>(null); | ||
const enableMvcpRetries = useRef<number>(0); | ||
|
||
const propAutoscrollToTopThreshold = | ||
mvcp?.autoscrollToTopThreshold || -Number.MAX_SAFE_INTEGER; | ||
const propMinIndexForVisible = mvcp?.minIndexForVisible || 1; | ||
|
||
const hasMvcpChanged = | ||
autoscrollToTopThreshold.current !== propAutoscrollToTopThreshold || | ||
minIndexForVisible.current !== propMinIndexForVisible; | ||
|
||
const enableMvcp = () => { | ||
if (!flRef.current) return; | ||
|
||
const scrollableNode = flRef.current.getScrollableNode(); | ||
const enableMvcpPromise = ScrollViewManager.enableMaintainVisibleContentPosition( | ||
scrollableNode, | ||
autoscrollToTopThreshold.current, | ||
minIndexForVisible.current | ||
); | ||
|
||
return enableMvcpPromise.then((_handle: number) => { | ||
handle.current = _handle; | ||
enableMvcpRetries.current = 0; | ||
}); | ||
}; | ||
|
||
const enableMvcpWithRetries = () => { | ||
autoscrollToTopThreshold.current = propAutoscrollToTopThreshold; | ||
minIndexForVisible.current = propMinIndexForVisible; | ||
|
||
return enableMvcp()?.catch(() => { | ||
/** | ||
* enableMaintainVisibleContentPosition from native module may throw IllegalViewOperationException, | ||
* in case view is not ready yet. In that case, lets do a retry!! | ||
*/ | ||
if (enableMvcpRetries.current < 10) { | ||
setTimeout(enableMvcp, 10); | ||
enableMvcpRetries.current += 1; | ||
} | ||
}); | ||
}; | ||
|
||
const disableMvcp: () => Promise<void> = () => { | ||
if (!ScrollViewManager || !handle?.current) { | ||
return Promise.resolve(); | ||
} | ||
|
||
return ScrollViewManager.disableMaintainVisibleContentPosition( | ||
handle.current | ||
); | ||
}; | ||
|
||
// We can only call enableMaintainVisibleContentPosition once the ref to underlying scrollview is ready. | ||
const resetMvcpIfNeeded = (): void => { | ||
if (!mvcp || Platform.OS !== 'android' || !flRef.current) { | ||
return; | ||
} | ||
|
||
/** | ||
* If the enableMaintainVisibleContentPosition has already been called, then | ||
* lets not call it again, unless prop values of mvcp changed. | ||
* | ||
* This condition is important since `resetMvcpIfNeeded` gets called in refCallback, | ||
* which gets called by react on every update to list. | ||
*/ | ||
if (isMvcpEnabled.current && !hasMvcpChanged) { | ||
return; | ||
} | ||
|
||
isMvcpEnabled.current = true; | ||
disableMvcp().then(enableMvcpWithRetries); | ||
}; | ||
|
||
const refCallback: (instance: FlatList<T> | null) => void = (ref) => { | ||
flRef.current = ref; | ||
|
||
resetMvcpIfNeeded(); | ||
if (typeof forwardedRef === 'function') { | ||
forwardedRef(ref); | ||
} else if (forwardedRef) { | ||
forwardedRef.current = ref; | ||
} | ||
}; | ||
|
||
useEffect(() => { | ||
// disable before unmounting | ||
return () => { | ||
disableMvcp(); | ||
}; | ||
}, []); | ||
|
||
return <FlatList<T> {...props} ref={refCallback} />; | ||
} | ||
) as unknown) as typeof FlatList; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,108 +1,3 @@ | ||
import React, { MutableRefObject, useRef } from 'react'; | ||
import { FlatList, FlatListProps, NativeModules, Platform } from 'react-native'; | ||
import debounce from 'lodash/debounce'; | ||
import { FlatList } from 'react-native'; | ||
|
||
export const MvcpScrollViewManager = NativeModules.MvcpScrollViewManager; | ||
|
||
const debouncedEnable = debounce( | ||
( | ||
enableMvcpPromise: MutableRefObject<Promise<any> | null>, | ||
disableMvcpPromise: MutableRefObject<Promise<any> | null>, | ||
viewTag: any, | ||
autoscrollToTopThreshold: number, | ||
minIndexForVisible: number | ||
) => { | ||
if (disableMvcpPromise.current) { | ||
disableMvcpPromise.current.then(() => { | ||
enableMvcpPromise.current = MvcpScrollViewManager.enableMaintainVisibleContentPosition( | ||
viewTag, | ||
autoscrollToTopThreshold, | ||
minIndexForVisible | ||
); | ||
}); | ||
} else { | ||
enableMvcpPromise.current = MvcpScrollViewManager.enableMaintainVisibleContentPosition( | ||
viewTag, | ||
autoscrollToTopThreshold, | ||
minIndexForVisible | ||
); | ||
} | ||
}, | ||
100, | ||
{ | ||
trailing: true, | ||
} | ||
); | ||
|
||
const debouncedDisable = debounce( | ||
( | ||
enableMvcpPromise: MutableRefObject<Promise<any> | null>, | ||
disableMvcpPromise: MutableRefObject<Promise<any> | null> | ||
) => { | ||
enableMvcpPromise.current?.then((handle) => { | ||
disableMvcpPromise.current = MvcpScrollViewManager.disableMaintainVisibleContentPosition( | ||
handle | ||
); | ||
}); | ||
}, | ||
50, | ||
{ | ||
trailing: true, | ||
} | ||
); | ||
|
||
export default (React.forwardRef( | ||
<T extends any>( | ||
props: FlatListProps<T>, | ||
forwardedRef: | ||
| ((instance: FlatList<T> | null) => void) | ||
| MutableRefObject<FlatList<T> | null> | ||
| null | ||
) => { | ||
const flRef = useRef<FlatList<T> | null>(null); | ||
const { maintainVisibleContentPosition: mvcp } = props; | ||
|
||
const autoscrollToTopThreshold = useRef<number | null>(); | ||
const minIndexForVisible = useRef<number>(); | ||
const enableMvcpPromise = useRef<Promise<any> | null>(null); | ||
const disableMvcpPromise = useRef<Promise<any> | null>(null); | ||
|
||
const resetMvcpIfNeeded = (): void => { | ||
if (!mvcp || Platform.OS !== 'android' || !flRef.current) { | ||
return; | ||
} | ||
|
||
enableMvcpPromise && | ||
enableMvcpPromise.current && | ||
debouncedDisable(enableMvcpPromise, disableMvcpPromise); | ||
|
||
autoscrollToTopThreshold.current = mvcp?.autoscrollToTopThreshold; | ||
minIndexForVisible.current = mvcp?.minIndexForVisible; | ||
|
||
const viewTag = flRef.current.getScrollableNode(); | ||
debouncedEnable( | ||
enableMvcpPromise, | ||
disableMvcpPromise, | ||
viewTag, | ||
autoscrollToTopThreshold.current || -Number.MAX_SAFE_INTEGER, | ||
minIndexForVisible.current || 1 | ||
); | ||
}; | ||
|
||
return ( | ||
<FlatList<T> | ||
{...props} | ||
ref={(ref) => { | ||
flRef.current = ref; | ||
|
||
resetMvcpIfNeeded(); | ||
if (typeof forwardedRef === 'function') { | ||
forwardedRef(ref); | ||
} else if (forwardedRef?.current) { | ||
forwardedRef.current = ref; | ||
} | ||
}} | ||
/> | ||
); | ||
} | ||
) as unknown) as typeof FlatList; | ||
export default FlatList; |
Oops, something went wrong.