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

Refactor findNodeHandler #6736

Merged
merged 13 commits into from
Dec 4, 2024
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,9 @@ export class NativeEventsManager implements INativeEventsManager {
// Get the tag for registering events - since the event emitting view can be nested inside the main component
const componentAnimatedRef = this.#managedComponent
._componentRef as AnimatedComponentRef & {
// Fabric
__nativeTag?: number;
// Paper
_nativeTag?: number;
};
if (componentAnimatedRef.getScrollableNode) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { Platform } from 'react-native';
import '../layoutReanimation/animationsManager';
import invariant from 'invariant';
import { adaptViewConfig } from '../ConfigHelper';
import { findHostInstance } from '../platform-specific/RNRenderer';
import { findHostInstance } from '../platform-specific/findHostInstance';
import { enableLayoutAnimations } from '../core';
import { SharedTransition } from '../layoutReanimation';
import { LayoutAnimationType } from '../commonTypes';
Expand Down Expand Up @@ -497,7 +497,7 @@ export function createAnimatedComponent(
>,
setLocalRef: (ref) => {
if (!ref) {
// component is unmounted
// component has been unmounted
return;
}
if (ref !== this._componentRef) {
piaskowyk marked this conversation as resolved.
Show resolved Hide resolved
Expand Down
7 changes: 5 additions & 2 deletions packages/react-native-reanimated/src/fabricUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,18 @@
/* eslint-disable */

import type { ShadowNodeWrapper } from './commonTypes';
import { findHostInstance } from './platform-specific/RNRenderer';
import {
findHostInstance,
HostInstance,
} from './platform-specific/findHostInstance';

let getInternalInstanceHandleFromPublicInstance: (ref: unknown) => {
stateNode: { node: unknown };
};

export function getShadowNodeWrapperFromRef(
ref: React.Component,
hostInstance?: unknown
hostInstance?: HostInstance
): ShadowNodeWrapper {
if (getInternalInstanceHandleFromPublicInstance === undefined) {
try {
Expand Down
piaskowyk marked this conversation as resolved.
Outdated
Show resolved Hide resolved

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
/* eslint-disable camelcase */
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
piaskowyk marked this conversation as resolved.
Show resolved Hide resolved
'use strict';

import type { IAnimatedComponentInternal } from '../createAnimatedComponent/commonTypes';
import { ReanimatedError } from '../errors';
import { isFabric } from '../PlatformChecker';

export type HostInstance = {
// Fabric fields
__internalInstanceHandle?: Record<string, unknown>;
__nativeTag?: number;
_viewConfig?: Record<string, unknown>;
// Paper fields
_nativeTag?: number;
viewConfig?: Record<string, unknown>;
};
piaskowyk marked this conversation as resolved.
Show resolved Hide resolved

function findHostInstanceFastPath(maybeNativeRef: HostInstance) {
if (
maybeNativeRef.__internalInstanceHandle &&
maybeNativeRef.__nativeTag &&
maybeNativeRef._viewConfig
) {
// This is a native ref to a Fabric component
return maybeNativeRef;
}
if (maybeNativeRef._nativeTag && maybeNativeRef.viewConfig) {
// This is a native ref to a Paper component
return maybeNativeRef;
}
// That means it’s a ref to a non-native component, and it’s necessary
// to call `findHostInstance_DEPRECATED` on them.
return undefined;
}

function resolveFindHostInstance_DEPRECATED() {
if (findHostInstance_DEPRECATED !== undefined) {
return;
}
if (isFabric()) {
try {
findHostInstance_DEPRECATED =
require('react-native/Libraries/Renderer/shims/ReactFabric').findHostInstance_DEPRECATED;
} catch (e) {
throw new ReanimatedError(
'Failed to resolve findHostInstance_DEPRECATED'
);
}
} else {
findHostInstance_DEPRECATED =
require('react-native/Libraries/Renderer/shims/ReactNative').findHostInstance_DEPRECATED;
}
}

let findHostInstance_DEPRECATED: (ref: unknown) => HostInstance;
export function findHostInstance(
component: IAnimatedComponentInternal | React.Component
): HostInstance {
// Fast path for native refs
const hostInstance = findHostInstanceFastPath(
(component as IAnimatedComponentInternal)._componentRef as HostInstance
);
if (hostInstance !== undefined) {
return hostInstance;
}

resolveFindHostInstance_DEPRECATED();
// Fabric implementation of findHostInstance_DEPRECATED doesn't accept a ref as an argument
return findHostInstance_DEPRECATED(
isFabric()
? component
: (component as IAnimatedComponentInternal)._componentRef
);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
'use strict';

export function findHostInstance(_component: any): void {}
Loading