import type {AnyNativeEvent} from '../events/PluginModuleType';
import type {
Container,
ActivityInstance,
SuspenseInstance,
} from '../client/ReactFiberConfigDOM';
import type {DOMEventName} from '../events/DOMEventNames';
import type {EventSystemFlags} from './EventSystemFlags';
import type {FiberRoot} from 'react-reconciler/src/ReactInternalTypes';
import type {EventPriority} from 'react-reconciler/src/ReactEventPriorities';
import {
unstable_scheduleCallback as scheduleCallback,
unstable_NormalPriority as NormalPriority,
} from 'scheduler';
import {
getNearestMountedFiber,
getContainerFromFiber,
getActivityInstanceFromFiber,
getSuspenseInstanceFromFiber,
} from 'react-reconciler/src/ReactFiberTreeReflection';
import {
findInstanceBlockingEvent,
findInstanceBlockingTarget,
} from './ReactDOMEventListener';
import {setReplayingEvent, resetReplayingEvent} from './CurrentReplayingEvent';
import {
getInstanceFromNode,
getClosestInstanceFromNode,
getFiberCurrentPropsFromNode,
} from '../client/ReactDOMComponentTree';
import {
HostRoot,
ActivityComponent,
SuspenseComponent,
} from 'react-reconciler/src/ReactWorkTags';
import {isHigherEventPriority} from 'react-reconciler/src/ReactEventPriorities';
import {isRootDehydrated} from 'react-reconciler/src/ReactFiberShellHydration';
import {dispatchReplayedFormAction} from './plugins/FormActionEventPlugin';
import {
resolveUpdatePriority,
runWithPriority as attemptHydrationAtPriority,
} from '../client/ReactDOMUpdatePriority';
import {
attemptContinuousHydration,
attemptHydrationAtCurrentPriority,
} from 'react-reconciler/src/ReactFiberReconciler';
import {enableHydrationChangeEvent} from 'shared/ReactFeatureFlags';
type PointerEventType = Event & {
pointerId: number,
relatedTarget: EventTarget | null,
...
};
type QueuedReplayableEvent = {
blockedOn: null | Container | ActivityInstance | SuspenseInstance,
domEventName: DOMEventName,
eventSystemFlags: EventSystemFlags,
nativeEvent: AnyNativeEvent,
targetContainers: Array<EventTarget>,
};
let hasScheduledReplayAttempt = false;
let queuedFocus: null | QueuedReplayableEvent = null;
let queuedDrag: null | QueuedReplayableEvent = null;
let queuedMouse: null | QueuedReplayableEvent = null;
const queuedPointers: Map<number, QueuedReplayableEvent> = new Map();
const queuedPointerCaptures: Map<number, QueuedReplayableEvent> = new Map();
const queuedChangeEventTargets: Array<EventTarget> = [];
type QueuedHydrationTarget = {
blockedOn: null | Container | ActivityInstance | SuspenseInstance,
target: Node,
priority: EventPriority,
};
const queuedExplicitHydrationTargets: Array<QueuedHydrationTarget> = [];
const discreteReplayableEvents: Array<DOMEventName> = [
'mousedown',
'mouseup',
'touchcancel',
'touchend',
'touchstart',
'auxclick',
'dblclick',
'pointercancel',
'pointerdown',
'pointerup',
'dragend',
'dragstart',
'drop',
'compositionend',
'compositionstart',
'keydown',
'keypress',
'keyup',
'input',
'textInput',
'copy',
'cut',
'paste',
'click',
'change',
'contextmenu',
'reset',
];
export function isDiscreteEventThatRequiresHydration(
eventType: DOMEventName,
): boolean {
return discreteReplayableEvents.indexOf(eventType) > -1;
}
function createQueuedReplayableEvent(
blockedOn: null | Container | ActivityInstance | SuspenseInstance,
domEventName: DOMEventName,
eventSystemFlags: EventSystemFlags,
targetContainer: EventTarget,
nativeEvent: AnyNativeEvent,
): QueuedReplayableEvent {
return {
blockedOn,
domEventName,
eventSystemFlags,
nativeEvent,
targetContainers: [targetContainer],
};
}
export function clearIfContinuousEvent(
domEventName: DOMEventName,
nativeEvent: AnyNativeEvent,
): void {
switch (domEventName) {
case 'focusin':
case 'focusout':
queuedFocus = null;
break;
case 'dragenter':
case 'dragleave':
queuedDrag = null;
break;
case 'mouseover':
case 'mouseout':
queuedMouse = null;
break;
case 'pointerover':
case 'pointerout': {
const pointerId = ((nativeEvent: any): PointerEventType).pointerId;
queuedPointers.delete(pointerId);
break;
}
case 'gotpointercapture':
case 'lostpointercapture': {
const pointerId = ((nativeEvent: any): PointerEventType).pointerId;
queuedPointerCaptures.delete(pointerId);
break;
}
}
}
function accumulateOrCreateContinuousQueuedReplayableEvent(
existingQueuedEvent: null | QueuedReplayableEvent,
blockedOn: null | Container | ActivityInstance | SuspenseInstance,
domEventName: DOMEventName,
eventSystemFlags: EventSystemFlags,
targetContainer: EventTarget,
nativeEvent: AnyNativeEvent,
): QueuedReplayableEvent {
if (
existingQueuedEvent === null ||
existingQueuedEvent.nativeEvent !== nativeEvent
) {
const queuedEvent = createQueuedReplayableEvent(
blockedOn,
domEventName,
eventSystemFlags,
targetContainer,
nativeEvent,
);
if (blockedOn !== null) {
const fiber = getInstanceFromNode(blockedOn);
if (fiber !== null) {
attemptContinuousHydration(fiber);
}
}
return queuedEvent;
}
existingQueuedEvent.eventSystemFlags |= eventSystemFlags;
const targetContainers = existingQueuedEvent.targetContainers;
if (
targetContainer !== null &&
targetContainers.indexOf(targetContainer) === -1
) {
targetContainers.push(targetContainer);
}
return existingQueuedEvent;
}
export function queueIfContinuousEvent(
blockedOn: null | Container | ActivityInstance | SuspenseInstance,
domEventName: DOMEventName,
eventSystemFlags: EventSystemFlags,
targetContainer: EventTarget,
nativeEvent: AnyNativeEvent,
): boolean {
switch (domEventName) {
case 'focusin': {
const focusEvent = ((nativeEvent: any): FocusEvent);
queuedFocus = accumulateOrCreateContinuousQueuedReplayableEvent(
queuedFocus,
blockedOn,
domEventName,
eventSystemFlags,
targetContainer,
focusEvent,
);
return true;
}
case 'dragenter': {
const dragEvent = ((nativeEvent: any): DragEvent);
queuedDrag = accumulateOrCreateContinuousQueuedReplayableEvent(
queuedDrag,
blockedOn,
domEventName,
eventSystemFlags,
targetContainer,
dragEvent,
);
return true;
}
case 'mouseover': {
const mouseEvent = ((nativeEvent: any): MouseEvent);
queuedMouse = accumulateOrCreateContinuousQueuedReplayableEvent(
queuedMouse,
blockedOn,
domEventName,
eventSystemFlags,
targetContainer,
mouseEvent,
);
return true;
}
case 'pointerover': {
const pointerEvent = ((nativeEvent: any): PointerEventType);
const pointerId = pointerEvent.pointerId;
queuedPointers.set(
pointerId,
accumulateOrCreateContinuousQueuedReplayableEvent(
queuedPointers.get(pointerId) || null,
blockedOn,
domEventName,
eventSystemFlags,
targetContainer,
pointerEvent,
),
);
return true;
}
case 'gotpointercapture': {
const pointerEvent = ((nativeEvent: any): PointerEventType);
const pointerId = pointerEvent.pointerId;
queuedPointerCaptures.set(
pointerId,
accumulateOrCreateContinuousQueuedReplayableEvent(
queuedPointerCaptures.get(pointerId) || null,
blockedOn,
domEventName,
eventSystemFlags,
targetContainer,
pointerEvent,
),
);
return true;
}
}
return false;
}
function attemptExplicitHydrationTarget(
queuedTarget: QueuedHydrationTarget,
): void {
const targetInst = getClosestInstanceFromNode(queuedTarget.target);
if (targetInst !== null) {
const nearestMounted = getNearestMountedFiber(targetInst);
if (nearestMounted !== null) {
const tag = nearestMounted.tag;
if (tag === SuspenseComponent) {
const instance = getSuspenseInstanceFromFiber(nearestMounted);
if (instance !== null) {
queuedTarget.blockedOn = instance;
attemptHydrationAtPriority(queuedTarget.priority, () => {
attemptHydrationAtCurrentPriority(nearestMounted);
});
return;
}
} else if (tag === ActivityComponent) {
const instance = getActivityInstanceFromFiber(nearestMounted);
if (instance !== null) {
queuedTarget.blockedOn = instance;
attemptHydrationAtPriority(queuedTarget.priority, () => {
attemptHydrationAtCurrentPriority(nearestMounted);
});
return;
}
} else if (tag === HostRoot) {
const root: FiberRoot = nearestMounted.stateNode;
if (isRootDehydrated(root)) {
queuedTarget.blockedOn = getContainerFromFiber(nearestMounted);
return;
}
}
}
}
queuedTarget.blockedOn = null;
}
export function queueExplicitHydrationTarget(target: Node): void {
const updatePriority = resolveUpdatePriority();
const queuedTarget: QueuedHydrationTarget = {
blockedOn: null,
target: target,
priority: updatePriority,
};
let i = 0;
for (; i < queuedExplicitHydrationTargets.length; i++) {
if (
!isHigherEventPriority(
updatePriority,
queuedExplicitHydrationTargets[i].priority,
)
) {
break;
}
}
queuedExplicitHydrationTargets.splice(i, 0, queuedTarget);
if (i === 0) {
attemptExplicitHydrationTarget(queuedTarget);
}
}
function attemptReplayContinuousQueuedEvent(
queuedEvent: QueuedReplayableEvent,
): boolean {
if (queuedEvent.blockedOn !== null) {
return false;
}
const targetContainers = queuedEvent.targetContainers;
while (targetContainers.length > 0) {
const nextBlockedOn = findInstanceBlockingEvent(queuedEvent.nativeEvent);
if (nextBlockedOn === null) {
const nativeEvent = queuedEvent.nativeEvent;
const nativeEventClone = new nativeEvent.constructor(
nativeEvent.type,
(nativeEvent: any),
);
setReplayingEvent(nativeEventClone);
nativeEvent.target.dispatchEvent(nativeEventClone);
resetReplayingEvent();
} else {
const fiber = getInstanceFromNode(nextBlockedOn);
if (fiber !== null) {
attemptContinuousHydration(fiber);
}
queuedEvent.blockedOn = nextBlockedOn;
return false;
}
targetContainers.shift();
}
return true;
}
function attemptReplayContinuousQueuedEventInMap(
queuedEvent: QueuedReplayableEvent,
key: number,
map: Map<number, QueuedReplayableEvent>,
): void {
if (attemptReplayContinuousQueuedEvent(queuedEvent)) {
map.delete(key);
}
}
function replayChangeEvent(target: EventTarget): void {
const element: HTMLInputElement | HTMLTextAreaElement | HTMLSelectElement =
(target: any);
if (element.nodeName === 'INPUT') {
if (element.type === 'checkbox' || element.type === 'radio') {
const EventCtr =
typeof PointerEvent === 'function' ? PointerEvent : Event;
target.dispatchEvent(new EventCtr('click', {bubbles: true}));
target.dispatchEvent(new Event('input', {bubbles: true}));
} else {
if (typeof InputEvent === 'function') {
target.dispatchEvent(new InputEvent('input', {bubbles: true}));
}
}
} else if (element.nodeName === 'TEXTAREA') {
if (typeof InputEvent === 'function') {
target.dispatchEvent(new InputEvent('input', {bubbles: true}));
}
}
target.dispatchEvent(new Event('change', {bubbles: true}));
}
function replayUnblockedEvents() {
hasScheduledReplayAttempt = false;
if (queuedFocus !== null && attemptReplayContinuousQueuedEvent(queuedFocus)) {
queuedFocus = null;
}
if (queuedDrag !== null && attemptReplayContinuousQueuedEvent(queuedDrag)) {
queuedDrag = null;
}
if (queuedMouse !== null && attemptReplayContinuousQueuedEvent(queuedMouse)) {
queuedMouse = null;
}
queuedPointers.forEach(attemptReplayContinuousQueuedEventInMap);
queuedPointerCaptures.forEach(attemptReplayContinuousQueuedEventInMap);
if (enableHydrationChangeEvent) {
for (let i = 0; i < queuedChangeEventTargets.length; i++) {
replayChangeEvent(queuedChangeEventTargets[i]);
}
queuedChangeEventTargets.length = 0;
}
}
export function flushEventReplaying(): void {
if (hasScheduledReplayAttempt) {
replayUnblockedEvents();
}
}
export function queueChangeEvent(target: EventTarget): void {
if (enableHydrationChangeEvent) {
queuedChangeEventTargets.push(target);
if (!hasScheduledReplayAttempt) {
hasScheduledReplayAttempt = true;
}
}
}
function scheduleCallbackIfUnblocked(
queuedEvent: QueuedReplayableEvent,
unblocked: Container | SuspenseInstance | ActivityInstance,
) {
if (queuedEvent.blockedOn === unblocked) {
queuedEvent.blockedOn = null;
if (!hasScheduledReplayAttempt) {
hasScheduledReplayAttempt = true;
if (!enableHydrationChangeEvent) {
scheduleCallback(NormalPriority, replayUnblockedEvents);
}
}
}
}
type FormAction = FormData => void | Promise<void>;
type FormReplayingQueue = Array<any>;
let lastScheduledReplayQueue: null | FormReplayingQueue = null;
function replayUnblockedFormActions(formReplayingQueue: FormReplayingQueue) {
if (lastScheduledReplayQueue === formReplayingQueue) {
lastScheduledReplayQueue = null;
}
for (let i = 0; i < formReplayingQueue.length; i += 3) {
const form: HTMLFormElement = formReplayingQueue[i];
const submitterOrAction:
| null
| HTMLInputElement
| HTMLButtonElement
| FormAction = formReplayingQueue[i + 1];
const formData: FormData = formReplayingQueue[i + 2];
if (typeof submitterOrAction !== 'function') {
const blockedOn = findInstanceBlockingTarget(submitterOrAction || form);
if (blockedOn === null) {
continue;
} else {
break;
}
}
const formInst = getInstanceFromNode(form);
if (formInst !== null) {
formReplayingQueue.splice(i, 3);
i -= 3;
dispatchReplayedFormAction(formInst, form, submitterOrAction, formData);
continue;
}
}
}
function scheduleReplayQueueIfNeeded(formReplayingQueue: FormReplayingQueue) {
if (lastScheduledReplayQueue !== formReplayingQueue) {
lastScheduledReplayQueue = formReplayingQueue;
scheduleCallback(NormalPriority, () =>
replayUnblockedFormActions(formReplayingQueue),
);
}
}
export function retryIfBlockedOn(
unblocked: Container | SuspenseInstance | ActivityInstance,
): void {
if (queuedFocus !== null) {
scheduleCallbackIfUnblocked(queuedFocus, unblocked);
}
if (queuedDrag !== null) {
scheduleCallbackIfUnblocked(queuedDrag, unblocked);
}
if (queuedMouse !== null) {
scheduleCallbackIfUnblocked(queuedMouse, unblocked);
}
const unblock = (queuedEvent: QueuedReplayableEvent) =>
scheduleCallbackIfUnblocked(queuedEvent, unblocked);
queuedPointers.forEach(unblock);
queuedPointerCaptures.forEach(unblock);
for (let i = 0; i < queuedExplicitHydrationTargets.length; i++) {
const queuedTarget = queuedExplicitHydrationTargets[i];
if (queuedTarget.blockedOn === unblocked) {
queuedTarget.blockedOn = null;
}
}
while (queuedExplicitHydrationTargets.length > 0) {
const nextExplicitTarget = queuedExplicitHydrationTargets[0];
if (nextExplicitTarget.blockedOn !== null) {
break;
} else {
attemptExplicitHydrationTarget(nextExplicitTarget);
if (nextExplicitTarget.blockedOn === null) {
queuedExplicitHydrationTargets.shift();
}
}
}
const root = unblocked.ownerDocument || unblocked;
const formReplayingQueue: void | FormReplayingQueue = (root: any)
.$$reactFormReplay;
if (formReplayingQueue != null) {
for (let i = 0; i < formReplayingQueue.length; i += 3) {
const form: HTMLFormElement = formReplayingQueue[i];
const submitterOrAction:
| null
| HTMLInputElement
| HTMLButtonElement
| FormAction = formReplayingQueue[i + 1];
const formProps = getFiberCurrentPropsFromNode(form);
if (typeof submitterOrAction === 'function') {
if (!formProps) {
scheduleReplayQueueIfNeeded(formReplayingQueue);
}
continue;
}
let target: Node = form;
if (formProps) {
let action: null | FormAction = null;
const submitter = submitterOrAction;
if (submitter && submitter.hasAttribute('formAction')) {
target = submitter;
const submitterProps = getFiberCurrentPropsFromNode(submitter);
if (submitterProps) {
action = (submitterProps: any).formAction;
} else {
const blockedOn = findInstanceBlockingTarget(target);
if (blockedOn !== null) {
continue;
}
}
} else {
action = (formProps: any).action;
}
if (typeof action === 'function') {
formReplayingQueue[i + 1] = action;
} else {
formReplayingQueue.splice(i, 3);
i -= 3;
}
scheduleReplayQueueIfNeeded(formReplayingQueue);
continue;
}
}
}
}