Skip to content

Commit

Permalink
Add getCurrentEventPriority to the host config
Browse files Browse the repository at this point in the history
  • Loading branch information
gaearon committed Feb 5, 2021
1 parent d445451 commit 6bfc2fb
Show file tree
Hide file tree
Showing 9 changed files with 55 additions and 4 deletions.
5 changes: 5 additions & 0 deletions packages/react-art/src/ReactARTHostConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import Transform from 'art/core/transform';
import Mode from 'art/modes/current';
import invariant from 'shared/invariant';
import {DefaultEvent} from 'shared/ReactTypes';

import {TYPES, EVENT_TYPES, childrenAsString} from './ReactARTInternals';

Expand Down Expand Up @@ -327,6 +328,10 @@ export function getChildHostContext() {
return NO_CONTEXT;
}

export function getCurrentEventPriority() {
return DefaultEvent;
}

export const scheduleTimeout = setTimeout;
export const cancelTimeout = clearTimeout;
export const noTimeout = -1;
Expand Down
13 changes: 12 additions & 1 deletion packages/react-dom/src/client/ReactDOMHostConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import type {
ObserveVisibleRectsCallback,
} from 'react-reconciler/src/ReactTestSelectors';
import type {RootType} from './ReactDOMRoot';
import type {ReactScopeInstance} from 'shared/ReactTypes';
import type {ReactScopeInstance, EventPriority} from 'shared/ReactTypes';

import {
precacheFiberNode,
Expand Down Expand Up @@ -59,6 +59,7 @@ import {
import dangerousStyleValue from '../shared/dangerousStyleValue';

import {REACT_OPAQUE_ID_TYPE} from 'shared/ReactSymbols';
import {getEventPriorityForPluginSystem} from '../events/DOMEventProperties';
import {retryIfBlockedOn} from '../events/ReactDOMEventReplaying';

import {
Expand All @@ -68,6 +69,7 @@ import {
} from 'shared/ReactFeatureFlags';
import {HostComponent, HostText} from 'react-reconciler/src/ReactWorkTags';
import {listenToAllSupportedEvents} from '../events/DOMPluginEventSystem';
import {DefaultEvent} from 'shared/ReactTypes';

export type Type = string;
export type Props = {
Expand Down Expand Up @@ -372,6 +374,15 @@ export function createTextInstance(
return textNode;
}

export function getCurrentEventPriority(): EventPriority {
const currentEvent = window.event;
if (currentEvent === undefined) {
return DefaultEvent;
}
// TODO: Is this fast enough? It reads from a Map.
return getEventPriorityForPluginSystem(currentEvent.type);
}

export const isPrimaryRenderer = true;
export const warnsIfNotActing = true;
// This initialization code may run even on server environments
Expand Down
6 changes: 6 additions & 0 deletions packages/react-native-renderer/src/ReactFabricHostConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,13 @@ import type {
ReactNativeBaseComponentViewConfig,
TouchedViewDataAtPoint,
} from './ReactNativeTypes';
import type {EventPriority} from 'shared/ReactTypes';

import {mountSafeCallback_NOT_REALLY_SAFE} from './NativeMethodsMixinUtils';
import {create, diff} from './ReactNativeAttributePayload';

import invariant from 'shared/invariant';
import {DefaultEvent} from 'shared/ReactTypes';

import {dispatchEvent} from './ReactFabricEventEmitter';

Expand Down Expand Up @@ -339,6 +341,10 @@ export function shouldSetTextContent(type: string, props: Props): boolean {
return false;
}

export function getCurrentEventPriority(): EventPriority {
return DefaultEvent;
}

// The Fabric renderer is secondary to the existing React Native renderer.
export const isPrimaryRenderer = false;

Expand Down
6 changes: 6 additions & 0 deletions packages/react-native-renderer/src/ReactNativeHostConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
*/

import type {TouchedViewDataAtPoint} from './ReactNativeTypes';
import type {EventPriority} from 'shared/ReactTypes';

import invariant from 'shared/invariant';

Expand All @@ -25,6 +26,7 @@ import {
updateFiberProps,
} from './ReactNativeComponentTree';
import ReactNativeFiberHostComponent from './ReactNativeFiberHostComponent';
import {DefaultEvent} from 'shared/ReactTypes';

const {get: getViewConfigForType} = ReactNativeViewConfigRegistry;

Expand Down Expand Up @@ -241,6 +243,10 @@ export function resetAfterCommit(containerInfo: Container): void {
// Noop
}

export function getCurrentEventPriority(): EventPriority {
return DefaultEvent;
}

export const isPrimaryRenderer = true;
export const warnsIfNotActing = true;

Expand Down
7 changes: 6 additions & 1 deletion packages/react-noop-renderer/src/createReactNoop.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,12 @@

import type {Fiber} from 'react-reconciler/src/ReactInternalTypes';
import type {UpdateQueue} from 'react-reconciler/src/ReactUpdateQueue';
import type {ReactNodeList, Thenable} from 'shared/ReactTypes';
import type {ReactNodeList, Thenable, EventPriority} from 'shared/ReactTypes';
import type {RootTag} from 'react-reconciler/src/ReactRootTags';

import * as Scheduler from 'scheduler/unstable_mock';
import {REACT_FRAGMENT_TYPE, REACT_ELEMENT_TYPE} from 'shared/ReactSymbols';
import {DefaultEvent} from 'shared/REactTypes';
import {
ConcurrentRoot,
BlockingRoot,
Expand Down Expand Up @@ -391,6 +392,10 @@ function createReactNoop(reconciler: Function, useMutation: boolean) {

resetAfterCommit(): void {},

getCurrentEventPriority(): EventPriority {
return DefaultEvent;
},

now: Scheduler.unstable_now,

isPrimaryRenderer: true,
Expand Down
10 changes: 10 additions & 0 deletions packages/react-reconciler/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,16 @@ This is a property (not a function) that should be set to something that can nev

You can proxy this to `queueMicrotask` or its equivalent in your environment.

#### `getCurrentEventPriority(fn)`

If there is no ongoing event (like `window.event` in the DOM), return `0`.

Else, return one of the numbers depending on the ongoing event type:

- For events originating from user input where each event is intentional (e.g. clicks), return `2`.
- For events originating from continuous user input (e.g. mouse move), return `1`.
- For any other events, return `0`.

#### `isPrimaryRenderer`

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`.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ export const finalizeInitialChildren = $$$hostConfig.finalizeInitialChildren;
export const prepareUpdate = $$$hostConfig.prepareUpdate;
export const shouldSetTextContent = $$$hostConfig.shouldSetTextContent;
export const createTextInstance = $$$hostConfig.createTextInstance;
export const getCurrentEventPriority = $$$hostConfig.getCurrentEventPriority;
export const scheduleTimeout = $$$hostConfig.scheduleTimeout;
export const cancelTimeout = $$$hostConfig.cancelTimeout;
export const scheduleMicrotask = $$$hostConfig.scheduleMicrotask;
Expand Down
7 changes: 7 additions & 0 deletions packages/react-test-renderer/src/ReactTestHostConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@
* @flow
*/

import type {EventPriority} from 'shared/ReactTypes';

import {REACT_OPAQUE_ID_TYPE} from 'shared/ReactSymbols';
import {DefaultEvent} from 'shared/ReactTypes';

export type Type = string;
export type Props = Object;
Expand Down Expand Up @@ -213,6 +216,10 @@ export function createTextInstance(
};
}

export function getCurrentEventPriority(): EventPriority {
return DefaultEvent;
}

export const isPrimaryRenderer = false;
export const warnsIfNotActing = true;

Expand Down
4 changes: 2 additions & 2 deletions packages/shared/ReactTypes.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,9 +88,9 @@ export type RefObject = {|

export type EventPriority = 0 | 1 | 2;

export const DiscreteEvent: EventPriority = 0;
export const DefaultEvent: EventPriority = 0;
export const ContinuousEvent: EventPriority = 1;
export const DefaultEvent: EventPriority = 2;
export const DiscreteEvent: EventPriority = 2;

export type ReactScope = {|
$$typeof: Symbol | number,
Expand Down

0 comments on commit 6bfc2fb

Please sign in to comment.