-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
WorkletEventHandler revamp #5845
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
87c27e0
6b9c8f3
86e762b
42adc74
30caad9
614c688
88583b2
89cb76d
20b8d67
b77737d
9ba75de
7de562a
f493098
a2de0b7
ad1fe89
4e911b8
faa8ec9
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -73,6 +73,7 @@ function useScrollViewOffsetNative( | |
| const internalOffset = useSharedValue(0); | ||
| const offset = useRef(providedOffset ?? internalOffset).current; | ||
| const scrollRef = useRef<AnimatedScrollView | null>(null); | ||
| const scrollRefTag = useRef<number | null>(null); | ||
|
|
||
| const eventHandler = useEvent<RNNativeScrollEvent>( | ||
| (event: ReanimatedScrollEvent) => { | ||
|
|
@@ -89,15 +90,30 @@ function useScrollViewOffsetNative( | |
|
|
||
| useEffect(() => { | ||
| // We need to make sure that listener for old animatedRef value is removed | ||
| if (scrollRef.current !== null) { | ||
| eventHandler.workletEventHandler.unregisterFromEvents(); | ||
| if (scrollRef.current !== null && scrollRefTag.current !== null) { | ||
| eventHandler.workletEventHandler.unregisterFromEvents( | ||
| scrollRefTag.current | ||
| ); | ||
| } | ||
|
|
||
| // Store the ref and viewTag for future cleanup | ||
| scrollRef.current = animatedRef.current; | ||
| scrollRefTag.current = animatedRef.getTag(); | ||
|
|
||
| if (scrollRefTag === null) { | ||
| console.warn( | ||
| '[Reanimated] ScrollViewOffset failed to resolve the view tag from animated ref. Did you forget to attach the ref to a component?' | ||
| ); | ||
| } else { | ||
| eventHandler.workletEventHandler.registerForEvents(scrollRefTag.current); | ||
| } | ||
|
|
||
| const viewTag = animatedRef.getTag(); | ||
| eventHandler.workletEventHandler.registerForEvents(viewTag); | ||
| return () => { | ||
| eventHandler.workletEventHandler.unregisterFromEvents(); | ||
| if (scrollRefTag.current !== null) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Note generally reading a mutable reference in effect cleanup is risky because it could've changed by the time you got to cleanup (e.g. it could've been ... cleaned up). Or replaced by a newer reference. So it's a good idea to do useEffect(() => {
const instance = someRef.current
doStuff(instance)
return () => {
undoStuff(instance)
})
})
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, thanks for pointing out - luckily newer versions have already made it this way 😄 |
||
| eventHandler.workletEventHandler.unregisterFromEvents( | ||
| scrollRefTag.current | ||
| ); | ||
| } | ||
| }; | ||
| // React here has a problem with `animatedRef.current` since a Ref .current | ||
| // field shouldn't be used as a dependency. However, in this case we have | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.