|
| 1 | +/** |
| 2 | + * Copyright (c) Meta Platforms, Inc. and affiliates. |
| 3 | + * |
| 4 | + * This source code is licensed under the MIT license found in the |
| 5 | + * LICENSE file in the root directory of this source tree. |
| 6 | + * |
| 7 | + * @flow |
| 8 | + */ |
| 9 | + |
| 10 | +import type {HostInstance} from './types'; |
| 11 | + |
| 12 | +// Some environments (e.g. React Native / Hermes) don't support the performance API yet. |
| 13 | +export const getCurrentTime: () => number = |
| 14 | + // $FlowFixMe[method-unbinding] |
| 15 | + typeof performance === 'object' && typeof performance.now === 'function' |
| 16 | + ? () => performance.now() |
| 17 | + : () => Date.now(); |
| 18 | + |
| 19 | +// Ideally, this should be injected from Reconciler config |
| 20 | +export function getPublicInstance(instance: HostInstance): HostInstance { |
| 21 | + // Typically the PublicInstance and HostInstance is the same thing but not in Fabric. |
| 22 | + // So we need to detect this and use that as the public instance. |
| 23 | + |
| 24 | + // React Native. Modern. Fabric. |
| 25 | + if (typeof instance === 'object' && instance !== null) { |
| 26 | + if (typeof instance.canonical === 'object' && instance.canonical !== null) { |
| 27 | + if ( |
| 28 | + typeof instance.canonical.publicInstance === 'object' && |
| 29 | + instance.canonical.publicInstance !== null |
| 30 | + ) { |
| 31 | + return instance.canonical.publicInstance; |
| 32 | + } |
| 33 | + } |
| 34 | + |
| 35 | + // React Native. Legacy. Paper. |
| 36 | + if (typeof instance._nativeTag === 'number') { |
| 37 | + return instance._nativeTag; |
| 38 | + } |
| 39 | + } |
| 40 | + |
| 41 | + // React Web. Usually a DOM element. |
| 42 | + return instance; |
| 43 | +} |
| 44 | + |
| 45 | +export function getNativeTag(instance: HostInstance): number | null { |
| 46 | + if (typeof instance !== 'object' || instance === null) { |
| 47 | + return null; |
| 48 | + } |
| 49 | + |
| 50 | + // Modern. Fabric. |
| 51 | + if ( |
| 52 | + instance.canonical != null && |
| 53 | + typeof instance.canonical.nativeTag === 'number' |
| 54 | + ) { |
| 55 | + return instance.canonical.nativeTag; |
| 56 | + } |
| 57 | + |
| 58 | + // Legacy. Paper. |
| 59 | + if (typeof instance._nativeTag === 'number') { |
| 60 | + return instance._nativeTag; |
| 61 | + } |
| 62 | + |
| 63 | + return null; |
| 64 | +} |
0 commit comments