Skip to content
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 19 commits into from
Aug 4, 2023
34 changes: 34 additions & 0 deletions src/ReanimatedModule.ts
Latropos marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { NativeEventEmitter, NativeModules } from 'react-native';
import { nativeShouldBeMock } from './reanimated2/PlatformChecker';
import { StyleProps } from './reanimated2';

export const NODE_MAPPING = new Map();
Latropos marked this conversation as resolved.
Show resolved Hide resolved

interface ListenerData {
viewTag: number;
props: StyleProps;
}

export function listener(data: ListenerData) {
const component = NODE_MAPPING.get(data.viewTag);
component && component._updateFromNative(data.props);
}

const ReanimatedModuleMock = {
async addListener(): Promise<void> {
// noop
},
async removeListeners(): Promise<void> {
// noop
},
};

let reanimatedModule: typeof ReanimatedModuleMock;
if (nativeShouldBeMock()) {
reanimatedModule = ReanimatedModuleMock;
} else {
const { ReanimatedModule } = NativeModules;
reanimatedModule = ReanimatedModule;
}

export const ReanimatedEventEmitter = new NativeEventEmitter(reanimatedModule);
piaskowyk marked this conversation as resolved.
Show resolved Hide resolved
23 changes: 23 additions & 0 deletions src/createAnimatedComponent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,11 @@ import NativeReanimatedModule from './reanimated2/NativeReanimated';
import { isSharedValue } from './reanimated2';
import type { AnimateProps } from './reanimated2/helperTypes';
import { removeFromPropsRegistry } from './reanimated2/PropsRegistry';
import {
NODE_MAPPING,
ReanimatedEventEmitter,
listener,
} from './ReanimatedModule';

function dummyListener() {
// empty listener we use to assign to listener properties for which animated
Expand Down Expand Up @@ -297,13 +302,15 @@ export default function createAnimatedComponent(

componentWillUnmount() {
this._detachNativeEvents();
this._detachPropUpdater();
this._detachStyles();
this._detachInlineProps();
this._sharedElementTransition?.unregisterTransition(this._viewTag);
}

componentDidMount() {
this._attachNativeEvents();
this._attachPropUpdater();
this._attachAnimatedStyles();
this._attachInlineProps();
}
Expand Down Expand Up @@ -409,6 +416,22 @@ export default function createAnimatedComponent(
}
}

_attachPropUpdater() {
const viewTag = findNodeHandle(this);
NODE_MAPPING.set(viewTag, this);
if (NODE_MAPPING.size === 1) {
ReanimatedEventEmitter.addListener('onReanimatedPropsChange', listener);
}
Latropos marked this conversation as resolved.
Show resolved Hide resolved
}

_detachPropUpdater() {
const viewTag = findNodeHandle(this);
NODE_MAPPING.delete(viewTag);
if (NODE_MAPPING.size === 0) {
ReanimatedEventEmitter.removeAllListeners('onReanimatedPropsChange');
}
Latropos marked this conversation as resolved.
Show resolved Hide resolved
}

_getViewInfo() {
let viewTag: number | null;
let viewName: string | null;
Expand Down