From 1a3f1afbd3cf815d4e55628cd7d84ef20171bab8 Mon Sep 17 00:00:00 2001 From: Samuel Susla Date: Tue, 8 Jun 2021 17:26:21 +0100 Subject: [PATCH] [React Native] Fabric get current event priority (#21553) * Call into Fabric to get current event priority Fix flow errors * Prettier * Better handle null and undefined cases * Remove optional chaining and use ?? operator * prettier-all * Use conditional ternary operator * prettier --- .../src/ReactFabricHostConfig.js | 22 ++++++++++++++++++- scripts/flow/react-native-host-hooks.js | 3 +++ 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/packages/react-native-renderer/src/ReactFabricHostConfig.js b/packages/react-native-renderer/src/ReactFabricHostConfig.js index ac4a97bb4609e..842a33291fb07 100644 --- a/packages/react-native-renderer/src/ReactFabricHostConfig.js +++ b/packages/react-native-renderer/src/ReactFabricHostConfig.js @@ -25,7 +25,10 @@ import invariant from 'shared/invariant'; import {dispatchEvent} from './ReactFabricEventEmitter'; -import {DefaultEventPriority} from 'react-reconciler/src/ReactEventPriorities'; +import { + DefaultEventPriority, + DiscreteEventPriority, +} from 'react-reconciler/src/ReactEventPriorities'; // Modules provided by RN: import { @@ -48,6 +51,9 @@ const { measure: fabricMeasure, measureInWindow: fabricMeasureInWindow, measureLayout: fabricMeasureLayout, + unstable_DefaultEventPriority: FabricDefaultPriority, + unstable_DiscreteEventPriority: FabricDiscretePriority, + unstable_getCurrentEventPriority: fabricGetCurrentEventPriority, } = nativeFabricUIManager; const {get: getViewConfigForType} = ReactNativeViewConfigRegistry; @@ -343,6 +349,20 @@ export function shouldSetTextContent(type: string, props: Props): boolean { } export function getCurrentEventPriority(): * { + const currentEventPriority = fabricGetCurrentEventPriority + ? fabricGetCurrentEventPriority() + : null; + + if (currentEventPriority != null) { + switch (currentEventPriority) { + case FabricDiscretePriority: + return DiscreteEventPriority; + case FabricDefaultPriority: + default: + return DefaultEventPriority; + } + } + return DefaultEventPriority; } diff --git a/scripts/flow/react-native-host-hooks.js b/scripts/flow/react-native-host-hooks.js index 92f766dfad415..770b77a84082e 100644 --- a/scripts/flow/react-native-host-hooks.js +++ b/scripts/flow/react-native-host-hooks.js @@ -184,6 +184,9 @@ declare var nativeFabricUIManager: { isJsResponder: boolean, blockNativeResponder: boolean, ) => void, + unstable_DefaultEventPriority: number, + unstable_DiscreteEventPriority: number, + unstable_getCurrentEventPriority: () => number, ... };