-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
Reintroduce onReanimatedPropsChange
#4821
Merged
Merged
Changes from all commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
f2efff4
onReanimatedPropsChange
5b5a4aa
Fix on Paper
f56006e
Remove examples
4842649
Remove lottie.json
0263be1
Remove examples
96ab649
Mock only used functions
3c18eeb
Merge branch 'main' into acynk/add-js-props
Latropos 46f3d61
Suggested refactor
463c4df
Code review
fe0267a
Small fixes
38ca0f4
Small fixes 2
42b9eeb
Small fixes 3
588e9c2
Code review
d6947f2
Add public keyword
172caaf
UPPER_CASE to camelCase
af3978c
Refactor
a66ce91
Remove underscore
b6ddc07
Merge branch 'main' into acynk/add-js-props
Latropos 91f4540
Fix typo
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
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,65 @@ | ||
import { | ||
NativeEventEmitter, | ||
NativeModules, | ||
findNodeHandle, | ||
} from 'react-native'; | ||
import { nativeShouldBeMock } from './reanimated2/PlatformChecker'; | ||
import { StyleProps } from './reanimated2'; | ||
|
||
interface ListenerData { | ||
viewTag: number; | ||
props: StyleProps; | ||
} | ||
|
||
export class JSPropUpdater { | ||
private static _tagToComponentMapping = new Map(); | ||
private _reanimatedEventEmitter: NativeEventEmitter; | ||
private static _reanimatedModuleMock = { | ||
async addListener(): Promise<void> { | ||
// noop | ||
}, | ||
async removeListeners(): Promise<void> { | ||
// noop | ||
}, | ||
}; | ||
|
||
private static _listener(data: ListenerData) { | ||
const component = JSPropUpdater._tagToComponentMapping.get(data.viewTag); | ||
component && component._updateFromNative(data.props); | ||
} | ||
|
||
constructor() { | ||
let reanimatedModule: typeof JSPropUpdater._reanimatedModuleMock; | ||
if (nativeShouldBeMock()) { | ||
reanimatedModule = JSPropUpdater._reanimatedModuleMock; | ||
} else { | ||
reanimatedModule = NativeModules.ReanimatedModule; | ||
} | ||
this._reanimatedEventEmitter = new NativeEventEmitter(reanimatedModule); | ||
} | ||
|
||
public addOnJSPropsChangeListener( | ||
animatedComponent: React.Component<unknown, unknown> | ||
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. Is there any TypeScript way to enforce that the component must be animated? |
||
) { | ||
const viewTag = findNodeHandle(animatedComponent); | ||
JSPropUpdater._tagToComponentMapping.set(viewTag, animatedComponent); | ||
if (JSPropUpdater._tagToComponentMapping.size === 1) { | ||
this._reanimatedEventEmitter.addListener( | ||
'onReanimatedPropsChange', | ||
JSPropUpdater._listener | ||
); | ||
} | ||
} | ||
|
||
public removeOnJSPropsChangeListener( | ||
animatedComponent: React.Component<unknown, unknown> | ||
) { | ||
const viewTag = findNodeHandle(animatedComponent); | ||
JSPropUpdater._tagToComponentMapping.delete(viewTag); | ||
if (JSPropUpdater._tagToComponentMapping.size === 0) { | ||
this._reanimatedEventEmitter.removeAllListeners( | ||
'onReanimatedPropsChange' | ||
); | ||
} | ||
} | ||
} |
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 |
---|---|---|
|
@@ -53,6 +53,7 @@ import NativeReanimatedModule from './reanimated2/NativeReanimated'; | |
import { isSharedValue } from './reanimated2'; | ||
import type { AnimateProps } from './reanimated2/helperTypes'; | ||
import { removeFromPropsRegistry } from './reanimated2/PropsRegistry'; | ||
import { JSPropUpdater } from './JSPropUpdater'; | ||
|
||
function dummyListener() { | ||
// empty listener we use to assign to listener properties for which animated | ||
|
@@ -286,6 +287,7 @@ export default function createAnimatedComponent( | |
_inlinePropsMapperId: number | null = null; | ||
_inlineProps: StyleProps = {}; | ||
_sharedElementTransition: SharedTransition | null = null; | ||
_JSPropUpdater = new JSPropUpdater(); | ||
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. Probably |
||
static displayName: string; | ||
|
||
constructor(props: AnimatedComponentProps<InitialComponentProps>) { | ||
|
@@ -297,13 +299,15 @@ export default function createAnimatedComponent( | |
|
||
componentWillUnmount() { | ||
this._detachNativeEvents(); | ||
this._JSPropUpdater.removeOnJSPropsChangeListener(this); | ||
this._detachStyles(); | ||
this._detachInlineProps(); | ||
this._sharedElementTransition?.unregisterTransition(this._viewTag); | ||
} | ||
|
||
componentDidMount() { | ||
this._attachNativeEvents(); | ||
this._JSPropUpdater.addOnJSPropsChangeListener(this); | ||
this._attachAnimatedStyles(); | ||
this._attachInlineProps(); | ||
} | ||
|
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.
I know this snippet of code was copied but it may be finally worth to convert it into a proper conditional.