GeezerVison is showing github.com — Experiment: Infer the current event priority from the native event (#… · react/react@97fce31
File treeExpand file treeCollapse file tree Open diff view settings react-native-renderer/src
Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: packages/react-art/src/ReactARTHostConfig.js +12Lines changed: 12 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 7 | 7 |
| 8 | 8 | import Transform from 'art/core/transform'; | 9 | 9 | import Mode from 'art/modes/current'; | | 10 | +import {enableNewReconciler} from 'shared/ReactFeatureFlags'; | 10 | 11 | import invariant from 'shared/invariant'; | 11 | 12 |
| 12 | 13 | import {TYPES, EVENT_TYPES, childrenAsString} from './ReactARTInternals'; | 13 | 14 |
| | 15 | +import {DefaultLanePriority as DefaultLanePriority_old} from 'react-reconciler/src/ReactFiberLane.old'; | | 16 | +import {DefaultLanePriority as DefaultLanePriority_new} from 'react-reconciler/src/ReactFiberLane.new'; | | 17 | + | | 18 | +const DefaultLanePriority = enableNewReconciler | | 19 | + ? DefaultLanePriority_new | | 20 | + : DefaultLanePriority_old; | | 21 | + | 14 | 22 | const pooledTransform = new Transform(); | 15 | 23 |
| 16 | 24 | const NO_CONTEXT = {}; | @@ -340,6 +348,10 @@ export function shouldSetTextContent(type, props) { | 340 | 348 | ); | 341 | 349 | } | 342 | 350 |
| | 351 | +export function getCurrentEventPriority() { | | 352 | + return DefaultLanePriority; | | 353 | +} | | 354 | + | 343 | 355 | // The ART renderer is secondary to the React DOM renderer. | 344 | 356 | export const isPrimaryRenderer = false; | 345 | 357 |
| |
Collapse file Copy file name to clipboardExpand all lines: packages/react-dom/src/client/ReactDOMHostConfig.js +17Lines changed: 17 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -47,6 +47,7 @@ import {validateDOMNesting, updatedAncestorInfo} from './validateDOMNesting'; | 47 | 47 | import { | 48 | 48 | isEnabled as ReactBrowserEventEmitterIsEnabled, | 49 | 49 | setEnabled as ReactBrowserEventEmitterSetEnabled, | | 50 | + getEventPriority, | 50 | 51 | } from '../events/ReactDOMEventListener'; | 51 | 52 | import {getChildNamespace} from '../shared/DOMNamespaces'; | 52 | 53 | import { | @@ -65,10 +66,18 @@ import { | 65 | 66 | enableSuspenseServerRenderer, | 66 | 67 | enableCreateEventHandleAPI, | 67 | 68 | enableScopeAPI, | | 69 | + enableNewReconciler, | 68 | 70 | } from 'shared/ReactFeatureFlags'; | 69 | 71 | import {HostComponent, HostText} from 'react-reconciler/src/ReactWorkTags'; | 70 | 72 | import {listenToAllSupportedEvents} from '../events/DOMPluginEventSystem'; | 71 | 73 |
| | 74 | +import {DefaultLanePriority as DefaultLanePriority_old} from 'react-reconciler/src/ReactFiberLane.old'; | | 75 | +import {DefaultLanePriority as DefaultLanePriority_new} from 'react-reconciler/src/ReactFiberLane.new'; | | 76 | + | | 77 | +const DefaultLanePriority = enableNewReconciler | | 78 | + ? DefaultLanePriority_new | | 79 | + : DefaultLanePriority_old; | | 80 | + | 72 | 81 | export type Type = string; | 73 | 82 | export type Props = { | 74 | 83 | autoFocus?: boolean, | @@ -372,6 +381,14 @@ export function createTextInstance( | 372 | 381 | return textNode; | 373 | 382 | } | 374 | 383 |
| | 384 | +export function getCurrentEventPriority(): * { | | 385 | + const currentEvent = window.event; | | 386 | + if (currentEvent === undefined) { | | 387 | + return DefaultLanePriority; | | 388 | + } | | 389 | + return getEventPriority(currentEvent.type); | | 390 | +} | | 391 | + | 375 | 392 | export const isPrimaryRenderer = true; | 376 | 393 | export const warnsIfNotActing = true; | 377 | 394 | // This initialization code may run even on server environments | |
Collapse file Copy file name to clipboardExpand all lines: packages/react-dom/src/events/ReactDOMEventListener.js +1-1Lines changed: 1 addition & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -348,7 +348,7 @@ export function attemptToDispatchEvent( | 348 | 348 | return null; | 349 | 349 | } | 350 | 350 |
| 351 | | -function getEventPriority(domEventName: DOMEventName) { | | 351 | +export function getEventPriority(domEventName: DOMEventName): * { | 352 | 352 | switch (domEventName) { | 353 | 353 | // Used by SimpleEventPlugin: | 354 | 354 | case 'cancel': | |
Collapse file Copy file name to clipboardExpand all lines: packages/react-native-renderer/src/ReactFabricHostConfig.js +12Lines changed: 12 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -21,10 +21,18 @@ import type { | 21 | 21 | import {mountSafeCallback_NOT_REALLY_SAFE} from './NativeMethodsMixinUtils'; | 22 | 22 | import {create, diff} from './ReactNativeAttributePayload'; | 23 | 23 |
| | 24 | +import {enableNewReconciler} from 'shared/ReactFeatureFlags'; | 24 | 25 | import invariant from 'shared/invariant'; | 25 | 26 |
| 26 | 27 | import {dispatchEvent} from './ReactFabricEventEmitter'; | 27 | 28 |
| | 29 | +import {DefaultLanePriority as DefaultLanePriority_old} from 'react-reconciler/src/ReactFiberLane.old'; | | 30 | +import {DefaultLanePriority as DefaultLanePriority_new} from 'react-reconciler/src/ReactFiberLane.new'; | | 31 | + | | 32 | +const DefaultLanePriority = enableNewReconciler | | 33 | + ? DefaultLanePriority_new | | 34 | + : DefaultLanePriority_old; | | 35 | + | 28 | 36 | // Modules provided by RN: | 29 | 37 | import { | 30 | 38 | ReactNativeViewConfigRegistry, | @@ -339,6 +347,10 @@ export function shouldSetTextContent(type: string, props: Props): boolean { | 339 | 347 | return false; | 340 | 348 | } | 341 | 349 |
| | 350 | +export function getCurrentEventPriority(): * { | | 351 | + return DefaultLanePriority; | | 352 | +} | | 353 | + | 342 | 354 | // The Fabric renderer is secondary to the existing React Native renderer. | 343 | 355 | export const isPrimaryRenderer = false; | 344 | 356 |
| |
Collapse file Copy file name to clipboardExpand all lines: packages/react-native-renderer/src/ReactNativeHostConfig.js +12Lines changed: 12 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 10 | 10 | import type {TouchedViewDataAtPoint} from './ReactNativeTypes'; | 11 | 11 |
| 12 | 12 | import invariant from 'shared/invariant'; | | 13 | +import {enableNewReconciler} from 'shared/ReactFeatureFlags'; | 13 | 14 |
| 14 | 15 | // Modules provided by RN: | 15 | 16 | import { | @@ -26,6 +27,13 @@ import { | 26 | 27 | } from './ReactNativeComponentTree'; | 27 | 28 | import ReactNativeFiberHostComponent from './ReactNativeFiberHostComponent'; | 28 | 29 |
| | 30 | +import {DefaultLanePriority as DefaultLanePriority_old} from 'react-reconciler/src/ReactFiberLane.old'; | | 31 | +import {DefaultLanePriority as DefaultLanePriority_new} from 'react-reconciler/src/ReactFiberLane.new'; | | 32 | + | | 33 | +const DefaultLanePriority = enableNewReconciler | | 34 | + ? DefaultLanePriority_new | | 35 | + : DefaultLanePriority_old; | | 36 | + | 29 | 37 | const {get: getViewConfigForType} = ReactNativeViewConfigRegistry; | 30 | 38 |
| 31 | 39 | export type Type = string; | @@ -261,6 +269,10 @@ export function shouldSetTextContent(type: string, props: Props): boolean { | 261 | 269 | return false; | 262 | 270 | } | 263 | 271 |
| | 272 | +export function getCurrentEventPriority(): * { | | 273 | + return DefaultLanePriority; | | 274 | +} | | 275 | + | 264 | 276 | // ------------------- | 265 | 277 | // Mutation | 266 | 278 | // ------------------- | |
Collapse file Copy file name to clipboardExpand all lines: packages/react-noop-renderer/src/createReactNoop.js +4Lines changed: 4 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -391,6 +391,10 @@ function createReactNoop(reconciler: Function, useMutation: boolean) { | 391 | 391 |
| 392 | 392 | resetAfterCommit(): void {}, | 393 | 393 |
| | 394 | + getCurrentEventPriority() { | | 395 | + return NoopRenderer.DefaultEventPriority; | | 396 | + }, | | 397 | + | 394 | 398 | now: Scheduler.unstable_now, | 395 | 399 |
| 396 | 400 | isPrimaryRenderer: true, | |
Collapse file Copy file name to clipboardExpand all lines: packages/react-reconciler/README.md +26Lines changed: 26 additions & 0 deletions - Display the source diff
- Display the rich diff
| Original file line number | Diff line number | Diff line change |
|---|
@@ -211,6 +211,32 @@ You can proxy this to `queueMicrotask` or its equivalent in your environment. | 211 | 211 |
| 212 | 212 | This is a property (not a function) that should be set to `true` if your renderer is the main one on the page. For example, if you're writing a renderer for the Terminal, it makes sense to set it to `true`, but if your renderer is used *on top of* React DOM or some other existing renderer, set it to `false`. | 213 | 213 |
| | 214 | +#### `getCurrentEventPriority` | | 215 | + | | 216 | +To implement this method, you'll need some constants available on the _returned_ `Renderer` object: | | 217 | + | | 218 | +```js | | 219 | +const HostConfig = { | | 220 | + // ... | | 221 | + getCurrentEventPriority() { | | 222 | + return MyRenderer.DefaultEventPriority; | | 223 | + }, | | 224 | + // ... | | 225 | +} | | 226 | + | | 227 | +const MyRenderer = Reconciler(HostConfig); | | 228 | +``` | | 229 | + | | 230 | +The constant you return depends on which event, if any, is being handled right now. (In the browser, you can check this using `window.event && window.event.type`). | | 231 | + | | 232 | +* **Discrete events:** If the active event is _directly caused by the user_ (such as mouse and keyboard events) and _each event in a sequence is intentional_ (e.g. `click`), return `MyRenderer.DiscreteEventPriority`. This tells React that they should interrupt any background work and cannot be batched across time. | | 233 | + | | 234 | +* **Continuous events:** If the active event is _directly caused by the user_ but _the user can't distinguish between individual events in a sequence_ (e.g. `mouseover`), return `MyRenderer.ContinuousEventPriority`. This tells React they should interrupt any background work but can be batched across time. | | 235 | + | | 236 | +* **Other events / No active event:** In all other cases, return `MyRenderer.DefaultEventPriority`. This tells React that this event is considered background work, and interactive events will be prioritized over it. | | 237 | + | | 238 | +You can consult the `getCurrentEventPriority()` implementation in `ReactDOMHostConfig.js` for a reference implementation. | | 239 | + | 214 | 240 | ### Mutation Methods | 215 | 241 |
| 216 | 242 | If you're using React in mutation mode (you probably do), you'll need to implement a few more methods. | |
Collapse file Copy file name to clipboardExpand all lines: packages/react-reconciler/src/ReactFiberReconciler.js +15Lines changed: 15 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -52,6 +52,9 @@ import { | 52 | 52 | registerMutableSourceForHydration as registerMutableSourceForHydration_old, | 53 | 53 | runWithPriority as runWithPriority_old, | 54 | 54 | getCurrentUpdateLanePriority as getCurrentUpdateLanePriority_old, | | 55 | + DefaultEventPriority as DefaultEventPriority_old, | | 56 | + DiscreteEventPriority as DiscreteEventPriority_old, | | 57 | + ContinuousEventPriority as ContinuousEventPriority_old, | 55 | 58 | } from './ReactFiberReconciler.old'; | 56 | 59 |
| 57 | 60 | import { | @@ -92,6 +95,9 @@ import { | 92 | 95 | registerMutableSourceForHydration as registerMutableSourceForHydration_new, | 93 | 96 | runWithPriority as runWithPriority_new, | 94 | 97 | getCurrentUpdateLanePriority as getCurrentUpdateLanePriority_new, | | 98 | + DefaultEventPriority as DefaultEventPriority_new, | | 99 | + DiscreteEventPriority as DiscreteEventPriority_new, | | 100 | + ContinuousEventPriority as ContinuousEventPriority_new, | 95 | 101 | } from './ReactFiberReconciler.new'; | 96 | 102 |
| 97 | 103 | export const createContainer = enableNewReconciler | @@ -168,6 +174,15 @@ export const createPortal = enableNewReconciler | 168 | 174 | export const createComponentSelector = enableNewReconciler | 169 | 175 | ? createComponentSelector_new | 170 | 176 | : createComponentSelector_old; | | 177 | +export const DefaultEventPriority = enableNewReconciler | | 178 | + ? DefaultEventPriority_new | | 179 | + : DefaultEventPriority_old; | | 180 | +export const DiscreteEventPriority = enableNewReconciler | | 181 | + ? DiscreteEventPriority_new | | 182 | + : DiscreteEventPriority_old; | | 183 | +export const ContinuousEventPriority = enableNewReconciler | | 184 | + ? ContinuousEventPriority_new | | 185 | + : ContinuousEventPriority_old; | 171 | 186 |
| 172 | 187 | //TODO: "psuedo" is spelled "pseudo" | 173 | 188 | export const createHasPsuedoClassSelector = enableNewReconciler | |
Collapse file Copy file name to clipboardExpand all lines: packages/react-reconciler/src/ReactFiberReconciler.new.js +9Lines changed: 9 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -94,6 +94,15 @@ import { | 94 | 94 | } from './ReactFiberHotReloading.new'; | 95 | 95 | import {markRenderScheduled} from './SchedulingProfiler'; | 96 | 96 |
| | 97 | +// Ideally host configs would import these constants from the reconciler | | 98 | +// entry point, but we can't do this because of a circular dependency. | | 99 | +// They are used by third-party renderers so they need to stay up to date. | | 100 | +export { | | 101 | + InputDiscreteLanePriority as DiscreteEventPriority, | | 102 | + InputContinuousLanePriority as ContinuousEventPriority, | | 103 | + DefaultLanePriority as DefaultEventPriority, | | 104 | +} from './ReactFiberLane.new'; | | 105 | + | 97 | 106 | export {registerMutableSourceForHydration} from './ReactMutableSource.new'; | 98 | 107 | export {createPortal} from './ReactPortal'; | 99 | 108 | export { | |
Collapse file Copy file name to clipboardExpand all lines: packages/react-reconciler/src/ReactFiberReconciler.old.js +9Lines changed: 9 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -94,6 +94,15 @@ import { | 94 | 94 | } from './ReactFiberHotReloading.old'; | 95 | 95 | import {markRenderScheduled} from './SchedulingProfiler'; | 96 | 96 |
| | 97 | +// Ideally host configs would import these constants from the reconciler | | 98 | +// entry point, but we can't do this because of a circular dependency. | | 99 | +// They are used by third-party renderers so they need to stay up to date. | | 100 | +export { | | 101 | + InputDiscreteLanePriority as DiscreteEventPriority, | | 102 | + InputContinuousLanePriority as ContinuousEventPriority, | | 103 | + DefaultLanePriority as DefaultEventPriority, | | 104 | +} from './ReactFiberLane.old'; | | 105 | + | 97 | 106 | export {registerMutableSourceForHydration} from './ReactMutableSource.new'; | 98 | 107 | export {createPortal} from './ReactPortal'; | 99 | 108 | export { | |
|