-
-
Notifications
You must be signed in to change notification settings - Fork 1k
[General] Reduce number of created closures #3853
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
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
57992c4
Reduce number of closures
j-piasecki 913697f
Don't modify memoized halnders
j-piasecki e83b7c1
Merge branch 'next' into @jpiasecki/less-closures
j-piasecki b9aa092
Fix after merge
j-piasecki c72fe9f
Memoize modified handlers
j-piasecki 9f1c87f
config -> callbacks
j-piasecki 30dfc3c
Remove assignment
j-piasecki 527e417
Rename stateMachine -> eventHandler
j-piasecki 47b29ed
Rename methods
j-piasecki File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
127 changes: 127 additions & 0 deletions
127
packages/react-native-gesture-handler/src/v3/hooks/callbacks/eventHandler.ts
This file contains hidden or 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,127 @@ | ||
| import { | ||
| flattenAndFilterEvent, | ||
| isEventForHandlerWithTag, | ||
| maybeExtractNativeEvent, | ||
| runCallback, | ||
| touchEventTypeToCallbackType, | ||
| } from '../utils'; | ||
| import { tagMessage } from '../../../utils'; | ||
| import { ReanimatedContext } from '../../../handlers/gestures/reanimatedWrapper'; | ||
| import { | ||
| ChangeCalculatorType, | ||
| GestureCallbacks, | ||
| GestureHandlerEventWithHandlerData, | ||
| GestureStateChangeEventWithHandlerData, | ||
| GestureUpdateEventWithHandlerData, | ||
| } from '../../types'; | ||
| import { CALLBACK_TYPE } from '../../../handlers/gestures/gesture'; | ||
| import { State } from '../../../State'; | ||
| import { TouchEventType } from '../../../TouchEventType'; | ||
| import { GestureTouchEvent } from '../../../handlers/gestureHandlerCommon'; | ||
|
|
||
| function handleStateChangeEvent<THandlerData>( | ||
| eventWithData: GestureStateChangeEventWithHandlerData<THandlerData>, | ||
| callbacks: GestureCallbacks<THandlerData>, | ||
| context: ReanimatedContext<THandlerData> | ||
| ) { | ||
| 'worklet'; | ||
| const { oldState, state } = eventWithData; | ||
| const event = flattenAndFilterEvent(eventWithData); | ||
|
|
||
| if (oldState === State.UNDETERMINED && state === State.BEGAN) { | ||
| runCallback(CALLBACK_TYPE.BEGAN, callbacks, event); | ||
| } else if ( | ||
| (oldState === State.BEGAN || oldState === State.UNDETERMINED) && | ||
| state === State.ACTIVE | ||
| ) { | ||
| runCallback(CALLBACK_TYPE.START, callbacks, event); | ||
| } else if (oldState !== state && state === State.END) { | ||
| if (oldState === State.ACTIVE) { | ||
| runCallback(CALLBACK_TYPE.END, callbacks, event, true); | ||
| } | ||
| runCallback(CALLBACK_TYPE.FINALIZE, callbacks, event, true); | ||
|
|
||
| if (context) { | ||
| context.lastUpdateEvent = undefined; | ||
| } | ||
| } else if ( | ||
| (state === State.FAILED || state === State.CANCELLED) && | ||
| state !== oldState | ||
| ) { | ||
| if (oldState === State.ACTIVE) { | ||
| runCallback(CALLBACK_TYPE.END, callbacks, event, false); | ||
| } | ||
| runCallback(CALLBACK_TYPE.FINALIZE, callbacks, event, false); | ||
|
|
||
| if (context) { | ||
| context.lastUpdateEvent = undefined; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| export function handleUpdateEvent<THandlerData>( | ||
| eventWithData: GestureUpdateEventWithHandlerData<THandlerData>, | ||
| handlers: GestureCallbacks<THandlerData>, | ||
| changeEventCalculator: ChangeCalculatorType<THandlerData> | undefined, | ||
| context: ReanimatedContext<THandlerData> | ||
| ) { | ||
| 'worklet'; | ||
| const eventWithChanges = changeEventCalculator | ||
| ? changeEventCalculator( | ||
| eventWithData, | ||
| context ? context.lastUpdateEvent : undefined | ||
| ) | ||
| : eventWithData; | ||
|
|
||
| const event = flattenAndFilterEvent(eventWithChanges); | ||
|
|
||
| // This should never happen, but since we don't want to call hooks conditionally, we have to mark | ||
| // context as possibly undefined to make TypeScript happy. | ||
| if (!context) { | ||
| throw new Error(tagMessage('Event handler context is not defined')); | ||
| } | ||
|
|
||
| runCallback(CALLBACK_TYPE.UPDATE, handlers, event); | ||
|
|
||
| context.lastUpdateEvent = eventWithData; | ||
| } | ||
|
|
||
| export function handleTouchEvent<THandlerData>( | ||
| event: GestureTouchEvent, | ||
| handlers: GestureCallbacks<THandlerData> | ||
| ) { | ||
| 'worklet'; | ||
|
|
||
| if (event.eventType !== TouchEventType.UNDETERMINED) { | ||
| runCallback(touchEventTypeToCallbackType(event.eventType), handlers, event); | ||
| } | ||
| } | ||
|
|
||
| export function eventHandler<THandlerData>( | ||
| handlerTag: number, | ||
| sourceEvent: GestureHandlerEventWithHandlerData<THandlerData>, | ||
| handlers: GestureCallbacks<THandlerData>, | ||
| changeEventCalculator: ChangeCalculatorType<THandlerData> | undefined, | ||
| jsContext: ReanimatedContext<THandlerData>, | ||
| dispatchesAnimatedEvents: boolean | ||
| ) { | ||
| 'worklet'; | ||
| const eventWithData = maybeExtractNativeEvent(sourceEvent); | ||
|
|
||
| if (!isEventForHandlerWithTag(handlerTag, eventWithData)) { | ||
| return; | ||
| } | ||
|
|
||
| if ('oldState' in eventWithData && eventWithData.oldState !== undefined) { | ||
| handleStateChangeEvent(eventWithData, handlers, jsContext); | ||
| } else if ('allTouches' in eventWithData) { | ||
| handleTouchEvent(eventWithData, handlers); | ||
| } else if (!dispatchesAnimatedEvents) { | ||
| handleUpdateEvent( | ||
| eventWithData, | ||
| handlers, | ||
| changeEventCalculator, | ||
| jsContext | ||
| ); | ||
| } | ||
| } | ||
28 changes: 0 additions & 28 deletions
28
...ages/react-native-gesture-handler/src/v3/hooks/callbacks/js/useGestureStateChangeEvent.ts
This file was deleted.
Oops, something went wrong.
25 changes: 0 additions & 25 deletions
25
packages/react-native-gesture-handler/src/v3/hooks/callbacks/js/useGestureTouchEvent.ts
This file was deleted.
Oops, something went wrong.
35 changes: 0 additions & 35 deletions
35
packages/react-native-gesture-handler/src/v3/hooks/callbacks/js/useGestureUpdateEvent.ts
This file was deleted.
Oops, something went wrong.
39 changes: 39 additions & 0 deletions
39
packages/react-native-gesture-handler/src/v3/hooks/callbacks/useGestureEventHandler.ts
This file contains hidden or 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,39 @@ | ||
| import { ReanimatedContext } from '../../../handlers/gestures/reanimatedWrapper'; | ||
| import { | ||
| BaseGestureConfig, | ||
| GestureCallbacks, | ||
| GestureHandlerEventWithHandlerData, | ||
| } from '../../types'; | ||
| import { useMemo } from 'react'; | ||
| import { eventHandler } from './eventHandler'; | ||
|
|
||
| export function useGestureEventHandler<THandlerData, TConfig>( | ||
| handlerTag: number, | ||
| handlers: GestureCallbacks<THandlerData>, | ||
| config: BaseGestureConfig<THandlerData, TConfig> | ||
| ) { | ||
| const jsContext: ReanimatedContext<THandlerData> = useMemo(() => { | ||
| return { | ||
| lastUpdateEvent: undefined, | ||
| }; | ||
| }, []); | ||
|
|
||
| return useMemo(() => { | ||
| return (event: GestureHandlerEventWithHandlerData<THandlerData>) => { | ||
| eventHandler( | ||
| handlerTag, | ||
| event, | ||
| handlers, | ||
| config.changeEventCalculator, | ||
| jsContext, | ||
| !!config.dispatchesAnimatedEvents | ||
| ); | ||
| }; | ||
| }, [ | ||
| handlerTag, | ||
| handlers, | ||
| config.changeEventCalculator, | ||
| config.dispatchesAnimatedEvents, | ||
| jsContext, | ||
| ]); | ||
| } |
This file contains hidden or 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 hidden or 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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Probably not in this PR, but we should rename
START,ENDcallbacks