mirror of
https://github.com/zebrajr/react.git
synced 2026-01-15 12:15:22 +00:00
* shared/src -> shared It's not a real package and doesn't even have package.json. This will also make importing less weird if we drop Haste. * Get rid of shared/utils Moved event-specific into shared/event. Moved rest to the root since distinction has always been pretty arbitrary. * Fix references to old shared/src paths
235 lines
6.6 KiB
JavaScript
235 lines
6.6 KiB
JavaScript
/**
|
|
* Copyright (c) 2013-present, Facebook, Inc.
|
|
*
|
|
* This source code is licensed under the MIT license found in the
|
|
* LICENSE file in the root directory of this source tree.
|
|
*
|
|
* @providesModule EventPluginUtils
|
|
*/
|
|
|
|
'use strict';
|
|
|
|
var ReactErrorUtils = require('ReactErrorUtils');
|
|
|
|
var invariant = require('fbjs/lib/invariant');
|
|
|
|
if (__DEV__) {
|
|
var warning = require('fbjs/lib/warning');
|
|
}
|
|
|
|
/**
|
|
* Injected dependencies:
|
|
*/
|
|
|
|
/**
|
|
* - `ComponentTree`: [required] Module that can convert between React instances
|
|
* and actual node references.
|
|
*/
|
|
var ComponentTree;
|
|
var injection = {
|
|
injectComponentTree: function(Injected) {
|
|
ComponentTree = Injected;
|
|
if (__DEV__) {
|
|
warning(
|
|
Injected &&
|
|
Injected.getNodeFromInstance &&
|
|
Injected.getInstanceFromNode,
|
|
'EventPluginUtils.injection.injectComponentTree(...): Injected ' +
|
|
'module is missing getNodeFromInstance or getInstanceFromNode.',
|
|
);
|
|
}
|
|
},
|
|
};
|
|
|
|
function isEndish(topLevelType) {
|
|
return (
|
|
topLevelType === 'topMouseUp' ||
|
|
topLevelType === 'topTouchEnd' ||
|
|
topLevelType === 'topTouchCancel'
|
|
);
|
|
}
|
|
|
|
function isMoveish(topLevelType) {
|
|
return topLevelType === 'topMouseMove' || topLevelType === 'topTouchMove';
|
|
}
|
|
function isStartish(topLevelType) {
|
|
return topLevelType === 'topMouseDown' || topLevelType === 'topTouchStart';
|
|
}
|
|
|
|
var validateEventDispatches;
|
|
if (__DEV__) {
|
|
validateEventDispatches = function(event) {
|
|
var dispatchListeners = event._dispatchListeners;
|
|
var dispatchInstances = event._dispatchInstances;
|
|
|
|
var listenersIsArr = Array.isArray(dispatchListeners);
|
|
var listenersLen = listenersIsArr
|
|
? dispatchListeners.length
|
|
: dispatchListeners ? 1 : 0;
|
|
|
|
var instancesIsArr = Array.isArray(dispatchInstances);
|
|
var instancesLen = instancesIsArr
|
|
? dispatchInstances.length
|
|
: dispatchInstances ? 1 : 0;
|
|
|
|
warning(
|
|
instancesIsArr === listenersIsArr && instancesLen === listenersLen,
|
|
'EventPluginUtils: Invalid `event`.',
|
|
);
|
|
};
|
|
}
|
|
|
|
/**
|
|
* Dispatch the event to the listener.
|
|
* @param {SyntheticEvent} event SyntheticEvent to handle
|
|
* @param {boolean} simulated If the event is simulated (changes exn behavior)
|
|
* @param {function} listener Application-level callback
|
|
* @param {*} inst Internal component instance
|
|
*/
|
|
function executeDispatch(event, simulated, listener, inst) {
|
|
var type = event.type || 'unknown-event';
|
|
event.currentTarget = EventPluginUtils.getNodeFromInstance(inst);
|
|
ReactErrorUtils.invokeGuardedCallbackAndCatchFirstError(
|
|
type,
|
|
listener,
|
|
undefined,
|
|
event,
|
|
);
|
|
event.currentTarget = null;
|
|
}
|
|
|
|
/**
|
|
* Standard/simple iteration through an event's collected dispatches.
|
|
*/
|
|
function executeDispatchesInOrder(event, simulated) {
|
|
var dispatchListeners = event._dispatchListeners;
|
|
var dispatchInstances = event._dispatchInstances;
|
|
if (__DEV__) {
|
|
validateEventDispatches(event);
|
|
}
|
|
if (Array.isArray(dispatchListeners)) {
|
|
for (var i = 0; i < dispatchListeners.length; i++) {
|
|
if (event.isPropagationStopped()) {
|
|
break;
|
|
}
|
|
// Listeners and Instances are two parallel arrays that are always in sync.
|
|
executeDispatch(
|
|
event,
|
|
simulated,
|
|
dispatchListeners[i],
|
|
dispatchInstances[i],
|
|
);
|
|
}
|
|
} else if (dispatchListeners) {
|
|
executeDispatch(event, simulated, dispatchListeners, dispatchInstances);
|
|
}
|
|
event._dispatchListeners = null;
|
|
event._dispatchInstances = null;
|
|
}
|
|
|
|
/**
|
|
* Standard/simple iteration through an event's collected dispatches, but stops
|
|
* at the first dispatch execution returning true, and returns that id.
|
|
*
|
|
* @return {?string} id of the first dispatch execution who's listener returns
|
|
* true, or null if no listener returned true.
|
|
*/
|
|
function executeDispatchesInOrderStopAtTrueImpl(event) {
|
|
var dispatchListeners = event._dispatchListeners;
|
|
var dispatchInstances = event._dispatchInstances;
|
|
if (__DEV__) {
|
|
validateEventDispatches(event);
|
|
}
|
|
if (Array.isArray(dispatchListeners)) {
|
|
for (var i = 0; i < dispatchListeners.length; i++) {
|
|
if (event.isPropagationStopped()) {
|
|
break;
|
|
}
|
|
// Listeners and Instances are two parallel arrays that are always in sync.
|
|
if (dispatchListeners[i](event, dispatchInstances[i])) {
|
|
return dispatchInstances[i];
|
|
}
|
|
}
|
|
} else if (dispatchListeners) {
|
|
if (dispatchListeners(event, dispatchInstances)) {
|
|
return dispatchInstances;
|
|
}
|
|
}
|
|
return null;
|
|
}
|
|
|
|
/**
|
|
* @see executeDispatchesInOrderStopAtTrueImpl
|
|
*/
|
|
function executeDispatchesInOrderStopAtTrue(event) {
|
|
var ret = executeDispatchesInOrderStopAtTrueImpl(event);
|
|
event._dispatchInstances = null;
|
|
event._dispatchListeners = null;
|
|
return ret;
|
|
}
|
|
|
|
/**
|
|
* Execution of a "direct" dispatch - there must be at most one dispatch
|
|
* accumulated on the event or it is considered an error. It doesn't really make
|
|
* sense for an event with multiple dispatches (bubbled) to keep track of the
|
|
* return values at each dispatch execution, but it does tend to make sense when
|
|
* dealing with "direct" dispatches.
|
|
*
|
|
* @return {*} The return value of executing the single dispatch.
|
|
*/
|
|
function executeDirectDispatch(event) {
|
|
if (__DEV__) {
|
|
validateEventDispatches(event);
|
|
}
|
|
var dispatchListener = event._dispatchListeners;
|
|
var dispatchInstance = event._dispatchInstances;
|
|
invariant(
|
|
!Array.isArray(dispatchListener),
|
|
'executeDirectDispatch(...): Invalid `event`.',
|
|
);
|
|
event.currentTarget = dispatchListener
|
|
? EventPluginUtils.getNodeFromInstance(dispatchInstance)
|
|
: null;
|
|
var res = dispatchListener ? dispatchListener(event) : null;
|
|
event.currentTarget = null;
|
|
event._dispatchListeners = null;
|
|
event._dispatchInstances = null;
|
|
return res;
|
|
}
|
|
|
|
/**
|
|
* @param {SyntheticEvent} event
|
|
* @return {boolean} True iff number of dispatches accumulated is greater than 0.
|
|
*/
|
|
function hasDispatches(event) {
|
|
return !!event._dispatchListeners;
|
|
}
|
|
|
|
/**
|
|
* General utilities that are useful in creating custom Event Plugins.
|
|
*/
|
|
var EventPluginUtils = {
|
|
isEndish: isEndish,
|
|
isMoveish: isMoveish,
|
|
isStartish: isStartish,
|
|
|
|
executeDirectDispatch: executeDirectDispatch,
|
|
executeDispatchesInOrder: executeDispatchesInOrder,
|
|
executeDispatchesInOrderStopAtTrue: executeDispatchesInOrderStopAtTrue,
|
|
hasDispatches: hasDispatches,
|
|
|
|
getFiberCurrentPropsFromNode: function(node) {
|
|
return ComponentTree.getFiberCurrentPropsFromNode(node);
|
|
},
|
|
getInstanceFromNode: function(node) {
|
|
return ComponentTree.getInstanceFromNode(node);
|
|
},
|
|
getNodeFromInstance: function(node) {
|
|
return ComponentTree.getNodeFromInstance(node);
|
|
},
|
|
|
|
injection: injection,
|
|
};
|
|
|
|
module.exports = EventPluginUtils;
|