Skip to content

Commit

Permalink
Further pre-requisite changes to plugin event system
Browse files Browse the repository at this point in the history
fix
  • Loading branch information
trueadm committed Feb 20, 2020
1 parent b6c94d6 commit 6c8bbdf
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 36 deletions.
1 change: 1 addition & 0 deletions packages/legacy-events/PluginModuleType.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ export type PluginModule<NativeEvent> = {
nativeTarget: NativeEvent,
nativeEventTarget: null | EventTarget,
eventSystemFlags: EventSystemFlags,
container?: null | Document | Element | Node,
) => ?ReactSyntheticEvent,
tapMoveThreshold?: number,
...
Expand Down
2 changes: 1 addition & 1 deletion packages/react-dom/src/client/ReactDOMRoot.js
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ function createRootImpl(
container.nodeType === DOCUMENT_NODE
? container
: container.ownerDocument;
eagerlyTrapReplayableEvents(doc);
eagerlyTrapReplayableEvents(container, doc);
}
return root;
}
Expand Down
7 changes: 4 additions & 3 deletions packages/react-dom/src/events/DOMLegacyEventPluginSystem.js
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,7 @@ function extractPluginEvents(
nativeEvent,
nativeEventTarget,
eventSystemFlags,
null,
);
if (extractedEvents) {
events = accumulateInto(events, extractedEvents);
Expand Down Expand Up @@ -264,7 +265,7 @@ function handleTopLevel(bookKeeping: BookKeepingInstance) {
}
}

export function dispatchEventForPluginEventSystem(
export function dispatchEventForLegacyPluginEventSystem(
topLevelType: DOMTopLevelEventType,
eventSystemFlags: EventSystemFlags,
nativeEvent: AnyNativeEvent,
Expand Down Expand Up @@ -311,12 +312,12 @@ export function legacyListenToEvent(
registrationName: string,
mountAt: Document | Element | Node,
): void {
const listeningSet = getListenerMapForElement(mountAt);
const listenerMap = getListenerMapForElement(mountAt);
const dependencies = registrationNameDependencies[registrationName];

for (let i = 0; i < dependencies.length; i++) {
const dependency = dependencies[i];
listenToTopLevelEvent(dependency, mountAt, listeningSet);
listenToTopLevelEvent(dependency, mountAt, listenerMap);
}
}

Expand Down
20 changes: 10 additions & 10 deletions packages/react-dom/src/events/ReactDOMEventListener.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ import {
DiscreteEvent,
} from 'shared/ReactTypes';
import {getEventPriorityForPluginSystem} from './DOMEventProperties';
import {dispatchEventForPluginEventSystem} from './DOMLegacyEventPluginSystem';
import {dispatchEventForLegacyPluginEventSystem} from './DOMLegacyEventPluginSystem';

const {
unstable_UserBlockingPriority: UserBlockingPriority,
Expand Down Expand Up @@ -242,17 +242,17 @@ export function dispatchEvent(
null, // Flags that we're not actually blocked on anything as far as we know.
topLevelType,
eventSystemFlags,
nativeEvent,
container,
nativeEvent,
);
return;
}

const blockedOn = attemptToDispatchEvent(
topLevelType,
eventSystemFlags,
nativeEvent,
container,
nativeEvent,
);

if (blockedOn === null) {
Expand All @@ -267,8 +267,8 @@ export function dispatchEvent(
blockedOn,
topLevelType,
eventSystemFlags,
nativeEvent,
container,
nativeEvent,
);
return;
}
Expand All @@ -278,8 +278,8 @@ export function dispatchEvent(
blockedOn,
topLevelType,
eventSystemFlags,
nativeEvent,
container,
nativeEvent,
)
) {
return;
Expand All @@ -293,7 +293,7 @@ export function dispatchEvent(
// in case the event system needs to trace it.
if (enableDeprecatedFlareAPI) {
if (eventSystemFlags & PLUGIN_EVENT_SYSTEM) {
dispatchEventForPluginEventSystem(
dispatchEventForLegacyPluginEventSystem(
topLevelType,
eventSystemFlags,
nativeEvent,
Expand All @@ -311,7 +311,7 @@ export function dispatchEvent(
);
}
} else {
dispatchEventForPluginEventSystem(
dispatchEventForLegacyPluginEventSystem(
topLevelType,
eventSystemFlags,
nativeEvent,
Expand All @@ -324,8 +324,8 @@ export function dispatchEvent(
export function attemptToDispatchEvent(
topLevelType: DOMTopLevelEventType,
eventSystemFlags: EventSystemFlags,
nativeEvent: AnyNativeEvent,
container: Document | Element | Node,
nativeEvent: AnyNativeEvent,
): null | Container | SuspenseInstance {
// TODO: Warn if _enabled is false.

Expand Down Expand Up @@ -372,7 +372,7 @@ export function attemptToDispatchEvent(

if (enableDeprecatedFlareAPI) {
if (eventSystemFlags & PLUGIN_EVENT_SYSTEM) {
dispatchEventForPluginEventSystem(
dispatchEventForLegacyPluginEventSystem(
topLevelType,
eventSystemFlags,
nativeEvent,
Expand All @@ -390,7 +390,7 @@ export function attemptToDispatchEvent(
);
}
} else {
dispatchEventForPluginEventSystem(
dispatchEventForLegacyPluginEventSystem(
topLevelType,
eventSystemFlags,
nativeEvent,
Expand Down
44 changes: 24 additions & 20 deletions packages/react-dom/src/events/ReactDOMEventReplaying.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import type {Container, SuspenseInstance} from '../client/ReactDOMHostConfig';
import type {DOMTopLevelEventType} from 'legacy-events/TopLevelEventTypes';
import type {EventSystemFlags} from 'legacy-events/EventSystemFlags';
import type {FiberRoot} from 'react-reconciler/src/ReactFiberRoot';
import type {DOMContainer} from '../client/ReactDOM';

import {
enableDeprecatedFlareAPI,
Expand Down Expand Up @@ -117,7 +118,7 @@ import {
TOP_BLUR,
} from './DOMTopLevelEventTypes';
import {IS_REPLAYED} from 'legacy-events/EventSystemFlags';
import {listenToTopLevelEvent} from './DOMLegacyEventPluginSystem';
import {listenToTopLevelEvent as legacyListenToTopLevelEvent} from './DOMLegacyEventPluginSystem';

type QueuedReplayableEvent = {|
blockedOn: null | Container | SuspenseInstance,
Expand Down Expand Up @@ -211,12 +212,12 @@ export function isReplayableDiscreteEvent(
return discreteReplayableEvents.indexOf(eventType) > -1;
}

function trapReplayableEvent(
function trapReplayableEventForDocument(
topLevelType: DOMTopLevelEventType,
document: Document,
listenerMap: Map<DOMTopLevelEventType | string, null | (any => void)>,
) {
listenToTopLevelEvent(topLevelType, document, listenerMap);
legacyListenToTopLevelEvent(topLevelType, document, listenerMap);
if (enableDeprecatedFlareAPI) {
// Trap events for the responder system.
const topLevelTypeString = unsafeCastDOMTopLevelTypeToString(topLevelType);
Expand All @@ -236,24 +237,27 @@ function trapReplayableEvent(
}
}

export function eagerlyTrapReplayableEvents(document: Document) {
const listenerMap = getListenerMapForElement(document);
export function eagerlyTrapReplayableEvents(
container: DOMContainer,
document: Document,
) {
const listenerMapForDoc = getListenerMapForElement(document);
// Discrete
discreteReplayableEvents.forEach(topLevelType => {
trapReplayableEvent(topLevelType, document, listenerMap);
trapReplayableEventForDocument(topLevelType, document, listenerMapForDoc);
});
// Continuous
continuousReplayableEvents.forEach(topLevelType => {
trapReplayableEvent(topLevelType, document, listenerMap);
trapReplayableEventForDocument(topLevelType, document, listenerMapForDoc);
});
}

function createQueuedReplayableEvent(
blockedOn: null | Container | SuspenseInstance,
topLevelType: DOMTopLevelEventType,
eventSystemFlags: EventSystemFlags,
nativeEvent: AnyNativeEvent,
container: Document | Element | Node,
nativeEvent: AnyNativeEvent,
): QueuedReplayableEvent {
return {
blockedOn,
Expand All @@ -268,15 +272,15 @@ export function queueDiscreteEvent(
blockedOn: null | Container | SuspenseInstance,
topLevelType: DOMTopLevelEventType,
eventSystemFlags: EventSystemFlags,
nativeEvent: AnyNativeEvent,
container: Document | Element | Node,
nativeEvent: AnyNativeEvent,
): void {
const queuedEvent = createQueuedReplayableEvent(
blockedOn,
topLevelType,
eventSystemFlags,
nativeEvent,
container,
nativeEvent,
);
queuedDiscreteEvents.push(queuedEvent);
if (enableSelectiveHydration) {
Expand Down Expand Up @@ -343,8 +347,8 @@ function accumulateOrCreateContinuousQueuedReplayableEvent(
blockedOn: null | Container | SuspenseInstance,
topLevelType: DOMTopLevelEventType,
eventSystemFlags: EventSystemFlags,
nativeEvent: AnyNativeEvent,
container: Document | Element | Node,
nativeEvent: AnyNativeEvent,
): QueuedReplayableEvent {
if (
existingQueuedEvent === null ||
Expand All @@ -354,8 +358,8 @@ function accumulateOrCreateContinuousQueuedReplayableEvent(
blockedOn,
topLevelType,
eventSystemFlags,
nativeEvent,
container,
nativeEvent,
);
if (blockedOn !== null) {
let fiber = getInstanceFromNode(blockedOn);
Expand All @@ -378,8 +382,8 @@ export function queueIfContinuousEvent(
blockedOn: null | Container | SuspenseInstance,
topLevelType: DOMTopLevelEventType,
eventSystemFlags: EventSystemFlags,
nativeEvent: AnyNativeEvent,
container: Document | Element | Node,
nativeEvent: AnyNativeEvent,
): boolean {
// These set relatedTarget to null because the replayed event will be treated as if we
// moved from outside the window (no target) onto the target once it hydrates.
Expand All @@ -392,8 +396,8 @@ export function queueIfContinuousEvent(
blockedOn,
topLevelType,
eventSystemFlags,
focusEvent,
container,
focusEvent,
);
return true;
}
Expand All @@ -404,8 +408,8 @@ export function queueIfContinuousEvent(
blockedOn,
topLevelType,
eventSystemFlags,
dragEvent,
container,
dragEvent,
);
return true;
}
Expand All @@ -416,8 +420,8 @@ export function queueIfContinuousEvent(
blockedOn,
topLevelType,
eventSystemFlags,
mouseEvent,
container,
mouseEvent,
);
return true;
}
Expand All @@ -431,8 +435,8 @@ export function queueIfContinuousEvent(
blockedOn,
topLevelType,
eventSystemFlags,
pointerEvent,
container,
pointerEvent,
),
);
return true;
Expand All @@ -447,8 +451,8 @@ export function queueIfContinuousEvent(
blockedOn,
topLevelType,
eventSystemFlags,
pointerEvent,
container,
pointerEvent,
),
);
return true;
Expand Down Expand Up @@ -524,8 +528,8 @@ function attemptReplayContinuousQueuedEvent(
let nextBlockedOn = attemptToDispatchEvent(
queuedEvent.topLevelType,
queuedEvent.eventSystemFlags,
queuedEvent.nativeEvent,
queuedEvent.container,
queuedEvent.nativeEvent,
);
if (nextBlockedOn !== null) {
// We're still blocked. Try again later.
Expand Down Expand Up @@ -567,8 +571,8 @@ function replayUnblockedEvents() {
let nextBlockedOn = attemptToDispatchEvent(
nextDiscreteEvent.topLevelType,
nextDiscreteEvent.eventSystemFlags,
nextDiscreteEvent.nativeEvent,
nextDiscreteEvent.container,
nextDiscreteEvent.nativeEvent,
);
if (nextBlockedOn !== null) {
// We're still blocked. Try again later.
Expand Down
9 changes: 7 additions & 2 deletions packages/react-dom/src/events/SelectEventPlugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -166,11 +166,16 @@ const SelectEventPlugin = {
nativeEvent,
nativeEventTarget,
eventSystemFlags,
container,
) {
const doc = getEventTargetDocument(nativeEventTarget);
const containerOrDoc =
container || getEventTargetDocument(nativeEventTarget);
// Track whether all listeners exists for this plugin. If none exist, we do
// not extract events. See #3639.
if (!doc || !isListeningToAllDependencies('onSelect', doc)) {
if (
!containerOrDoc ||
!isListeningToAllDependencies('onSelect', containerOrDoc)
) {
return null;
}

Expand Down

0 comments on commit 6c8bbdf

Please sign in to comment.