Skip to content

Commit

Permalink
Update implementation of findHostInstance followin reanimated
Browse files Browse the repository at this point in the history
  • Loading branch information
kkafar committed Jan 9, 2025
1 parent b8d896c commit af50c66
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 23 deletions.
5 changes: 5 additions & 0 deletions src/gesture-handler/fabricUtils/arch-check.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export type LocalGlobal = typeof global & Record<string, unknown>;

export function isFabric() {
return !!(global as LocalGlobal)._IS_FABRIC;
}
72 changes: 72 additions & 0 deletions src/gesture-handler/fabricUtils/findHostInstance.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/* eslint-disable camelcase */
'use strict';

// import type { IAnimatedComponentInternal } from '../createAnimatedComponent/commonTypes';
import { isFabric } from './arch-check';

type HostInstanceFabric = {
__internalInstanceHandle?: Record<string, unknown>;
__nativeTag?: number;
_viewConfig?: Record<string, unknown>;
};

type HostInstancePaper = {
_nativeTag?: number;
viewConfig?: Record<string, unknown>;
};

export type HostInstance = HostInstanceFabric & HostInstancePaper;

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 =
// eslint-disable-next-line @typescript-eslint/no-var-requires
require('react-native/Libraries/Renderer/shims/ReactFabric').findHostInstance_DEPRECATED;
} catch (e) {
throw new Error('Failed to resolve findHostInstance_DEPRECATED');
}
} else {
findHostInstance_DEPRECATED =
// eslint-disable-next-line @typescript-eslint/no-var-requires
require('react-native/Libraries/Renderer/shims/ReactNative').findHostInstance_DEPRECATED;
}
}

let findHostInstance_DEPRECATED: (ref: unknown) => HostInstance;
export function findHostInstance(component: React.Component): HostInstance {
// Fast path for native refs
const hostInstance = findHostInstanceFastPath(
(component as any)._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 any)._componentRef,
);
}
3 changes: 3 additions & 0 deletions src/gesture-handler/fabricUtils/findHostInstance.web.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
'use strict';

export function findHostInstance(_component: any): void {}
Original file line number Diff line number Diff line change
@@ -1,40 +1,27 @@
'use strict';

import { View } from 'react-native';
import { HostInstance, findHostInstance } from './findHostInstance';
export { isFabric } from './arch-check';

/* eslint-disable */

type LocalGlobal = typeof global & Record<string, unknown>;

export function isFabric() {
return !!(global as LocalGlobal)._IS_FABRIC;
}

export type ShadowNodeWrapper = {
__hostObjectShadowNodeWrapper: never;
};

let findHostInstance_DEPRECATED: (ref: unknown) => void;

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

// Taken and modifies from reanimated
export function getShadowNodeWrapperAndTagFromRef(ref: View | null): {
export function getShadowNodeWrapperAndTagFromRef(
ref: View | null,
hostInstance?: HostInstance,
): {
shadowNodeWrapper: ShadowNodeWrapper;
tag: number;
} {
// load findHostInstance_DEPRECATED lazily because it may not be available before render
if (findHostInstance_DEPRECATED === undefined) {
try {
findHostInstance_DEPRECATED =
require('react-native/Libraries/Renderer/shims/ReactFabric').findHostInstance_DEPRECATED;
} catch (e) {
findHostInstance_DEPRECATED = (_ref: unknown) => null;
}
}

if (getInternalInstanceHandleFromPublicInstance === undefined) {
try {
getInternalInstanceHandleFromPublicInstance =
Expand Down Expand Up @@ -73,13 +60,19 @@ export function getShadowNodeWrapperAndTagFromRef(ref: View | null): {
tag: (ref as any)?.__nativeTag,
};
} else {
const hostInstance = findHostInstance_DEPRECATED(ref);
const instance = hostInstance ?? findHostInstance(ref as any);
resolvedRef = {
shadowNodeWrapper:
getInternalInstanceHandleFromPublicInstance(hostInstance).stateNode
.node,
tag: (hostInstance as any)?._nativeTag,
getInternalInstanceHandleFromPublicInstance(instance).stateNode.node,
tag: (instance as any)?._nativeTag ?? (instance as any)?.__nativeTag,
};
//const hostInstance = findHostInstance_DEPRECATED(ref);
//resolvedRef = {
// shadowNodeWrapper:
// getInternalInstanceHandleFromPublicInstance(hostInstance).stateNode
// .node,
// tag: (hostInstance as any)?._nativeTag,
//};
}

return resolvedRef;
Expand Down

0 comments on commit af50c66

Please sign in to comment.