-
-
Notifications
You must be signed in to change notification settings - Fork 538
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update implementation of findHostInstance followin reanimated
- Loading branch information
Showing
4 changed files
with
96 additions
and
23 deletions.
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,5 @@ | ||
export type LocalGlobal = typeof global & Record<string, unknown>; | ||
|
||
export function isFabric() { | ||
return !!(global as LocalGlobal)._IS_FABRIC; | ||
} |
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,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, | ||
); | ||
} |
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,3 @@ | ||
'use strict'; | ||
|
||
export function findHostInstance(_component: any): void {} |
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