2016-03-25 06:40:52 +08:00
|
|
|
/**
|
2022-10-18 23:19:24 +08:00
|
|
|
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
2016-03-25 06:40:52 +08:00
|
|
|
*
|
2017-09-25 04:48:13 +08:00
|
|
|
* This source code is licensed under the MIT license found in the
|
|
|
|
|
* LICENSE file in the root directory of this source tree.
|
2016-03-25 06:40:52 +08:00
|
|
|
*
|
|
|
|
|
* @flow
|
|
|
|
|
*/
|
|
|
|
|
|
2022-10-04 05:03:33 +08:00
|
|
|
import type {
|
|
|
|
|
AnyNativeEvent,
|
|
|
|
|
EventTypes,
|
|
|
|
|
} from './legacy-events/PluginModuleType';
|
2020-07-01 23:37:13 +08:00
|
|
|
import type {TopLevelType} from './legacy-events/TopLevelEventTypes';
|
|
|
|
|
import SyntheticEvent from './legacy-events/SyntheticEvent';
|
2019-05-23 15:23:54 +08:00
|
|
|
|
|
|
|
|
// Module provided by RN:
|
|
|
|
|
import {ReactNativeViewConfigRegistry} from 'react-native/Libraries/ReactPrivate/ReactNativePrivateInterface';
|
2020-07-01 23:37:13 +08:00
|
|
|
import accumulateInto from './legacy-events/accumulateInto';
|
2023-03-02 23:54:51 +08:00
|
|
|
import getListener from './ReactNativeGetListener';
|
2020-07-01 23:37:13 +08:00
|
|
|
import forEachAccumulated from './legacy-events/forEachAccumulated';
|
2020-04-02 18:07:20 +08:00
|
|
|
import {HostComponent} from 'react-reconciler/src/ReactWorkTags';
|
2019-05-23 15:23:54 +08:00
|
|
|
|
2023-01-31 21:25:05 +08:00
|
|
|
const {customBubblingEventTypes, customDirectEventTypes} =
|
|
|
|
|
ReactNativeViewConfigRegistry;
|
2016-03-25 06:40:52 +08:00
|
|
|
|
2020-03-13 05:48:29 +08:00
|
|
|
// Start of inline: the below functions were inlined from
|
|
|
|
|
// EventPropagator.js, as they deviated from ReactDOM's newer
|
|
|
|
|
// implementations.
|
2023-01-10 04:46:48 +08:00
|
|
|
// $FlowFixMe[missing-local-annot]
|
2023-03-02 23:54:51 +08:00
|
|
|
function listenerAtPhase(inst, event, propagationPhase: PropagationPhases) {
|
2020-03-13 05:48:29 +08:00
|
|
|
const registrationName =
|
|
|
|
|
event.dispatchConfig.phasedRegistrationNames[propagationPhase];
|
2023-03-02 23:54:51 +08:00
|
|
|
return getListener(inst, registrationName);
|
2020-03-13 05:48:29 +08:00
|
|
|
}
|
|
|
|
|
|
2023-01-10 04:46:48 +08:00
|
|
|
// $FlowFixMe[missing-local-annot]
|
2020-03-13 05:48:29 +08:00
|
|
|
function accumulateDirectionalDispatches(inst, phase, event) {
|
|
|
|
|
if (__DEV__) {
|
|
|
|
|
if (!inst) {
|
|
|
|
|
console.error('Dispatching inst must not be null');
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-03-02 23:54:51 +08:00
|
|
|
const listener = listenerAtPhase(inst, event, phase);
|
|
|
|
|
if (listener) {
|
|
|
|
|
event._dispatchListeners = accumulateInto(
|
|
|
|
|
event._dispatchListeners,
|
|
|
|
|
listener,
|
|
|
|
|
);
|
|
|
|
|
event._dispatchInstances = accumulateInto(event._dispatchInstances, inst);
|
|
|
|
|
}
|
2020-03-13 05:48:29 +08:00
|
|
|
}
|
|
|
|
|
|
2023-01-10 04:46:48 +08:00
|
|
|
// $FlowFixMe[missing-local-annot]
|
2020-04-02 18:07:20 +08:00
|
|
|
function getParent(inst) {
|
|
|
|
|
do {
|
|
|
|
|
inst = inst.return;
|
|
|
|
|
// TODO: If this is a HostRoot we might want to bail out.
|
|
|
|
|
// That is depending on if we want nested subtrees (layers) to bubble
|
|
|
|
|
// events to their parent. We could also go through parentNode on the
|
|
|
|
|
// host node but that wouldn't work for React Native and doesn't let us
|
|
|
|
|
// do the portal feature.
|
|
|
|
|
} while (inst && inst.tag !== HostComponent);
|
|
|
|
|
if (inst) {
|
|
|
|
|
return inst;
|
|
|
|
|
}
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Simulates the traversal of a two-phase, capture/bubble event dispatch.
|
|
|
|
|
*/
|
2022-03-15 01:59:21 +08:00
|
|
|
export function traverseTwoPhase(
|
|
|
|
|
inst: Object,
|
|
|
|
|
fn: Function,
|
|
|
|
|
arg: Function,
|
|
|
|
|
skipBubbling: boolean,
|
|
|
|
|
) {
|
2020-04-02 18:07:20 +08:00
|
|
|
const path = [];
|
|
|
|
|
while (inst) {
|
|
|
|
|
path.push(inst);
|
|
|
|
|
inst = getParent(inst);
|
|
|
|
|
}
|
|
|
|
|
let i;
|
|
|
|
|
for (i = path.length; i-- > 0; ) {
|
|
|
|
|
fn(path[i], 'captured', arg);
|
|
|
|
|
}
|
2022-03-15 01:59:21 +08:00
|
|
|
if (skipBubbling) {
|
|
|
|
|
// Dispatch on target only
|
|
|
|
|
fn(path[0], 'bubbled', arg);
|
|
|
|
|
} else {
|
|
|
|
|
for (i = 0; i < path.length; i++) {
|
|
|
|
|
fn(path[i], 'bubbled', arg);
|
|
|
|
|
}
|
2020-04-02 18:07:20 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-10 04:46:48 +08:00
|
|
|
// $FlowFixMe[missing-local-annot]
|
2020-03-13 05:48:29 +08:00
|
|
|
function accumulateTwoPhaseDispatchesSingle(event) {
|
|
|
|
|
if (event && event.dispatchConfig.phasedRegistrationNames) {
|
2022-03-15 01:59:21 +08:00
|
|
|
traverseTwoPhase(
|
|
|
|
|
event._targetInst,
|
|
|
|
|
accumulateDirectionalDispatches,
|
|
|
|
|
event,
|
|
|
|
|
false,
|
|
|
|
|
);
|
2020-03-13 05:48:29 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-10 04:46:48 +08:00
|
|
|
// $FlowFixMe[missing-local-annot]
|
2020-03-13 05:48:29 +08:00
|
|
|
function accumulateTwoPhaseDispatches(events) {
|
|
|
|
|
forEachAccumulated(events, accumulateTwoPhaseDispatchesSingle);
|
|
|
|
|
}
|
2020-04-02 18:07:20 +08:00
|
|
|
|
2023-01-10 04:46:48 +08:00
|
|
|
// $FlowFixMe[missing-local-annot]
|
2022-03-15 01:59:21 +08:00
|
|
|
function accumulateCapturePhaseDispatches(event) {
|
|
|
|
|
if (event && event.dispatchConfig.phasedRegistrationNames) {
|
|
|
|
|
traverseTwoPhase(
|
|
|
|
|
event._targetInst,
|
|
|
|
|
accumulateDirectionalDispatches,
|
|
|
|
|
event,
|
|
|
|
|
true,
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-02 18:07:20 +08:00
|
|
|
/**
|
|
|
|
|
* Accumulates without regard to direction, does not look for phased
|
|
|
|
|
* registration names. Same as `accumulateDirectDispatchesSingle` but without
|
|
|
|
|
* requiring that the `dispatchMarker` be the same as the dispatched ID.
|
|
|
|
|
*/
|
|
|
|
|
function accumulateDispatches(
|
|
|
|
|
inst: Object,
|
|
|
|
|
ignoredDirection: ?boolean,
|
|
|
|
|
event: Object,
|
|
|
|
|
): void {
|
|
|
|
|
if (inst && event && event.dispatchConfig.registrationName) {
|
|
|
|
|
const registrationName = event.dispatchConfig.registrationName;
|
2023-03-02 23:54:51 +08:00
|
|
|
const listener = getListener(inst, registrationName);
|
|
|
|
|
if (listener) {
|
|
|
|
|
event._dispatchListeners = accumulateInto(
|
|
|
|
|
event._dispatchListeners,
|
|
|
|
|
listener,
|
|
|
|
|
);
|
|
|
|
|
event._dispatchInstances = accumulateInto(event._dispatchInstances, inst);
|
|
|
|
|
}
|
2020-04-02 18:07:20 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Accumulates dispatches on an `SyntheticEvent`, but only for the
|
|
|
|
|
* `dispatchMarker`.
|
|
|
|
|
* @param {SyntheticEvent} event
|
|
|
|
|
*/
|
|
|
|
|
function accumulateDirectDispatchesSingle(event: Object) {
|
|
|
|
|
if (event && event.dispatchConfig.registrationName) {
|
|
|
|
|
accumulateDispatches(event._targetInst, null, event);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function accumulateDirectDispatches(events: ?(Array<Object> | Object)) {
|
|
|
|
|
forEachAccumulated(events, accumulateDirectDispatchesSingle);
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-13 05:48:29 +08:00
|
|
|
// End of inline
|
2023-03-02 23:54:51 +08:00
|
|
|
type PropagationPhases = 'bubbled' | 'captured';
|
2020-03-13 05:48:29 +08:00
|
|
|
|
2017-09-15 05:55:32 +08:00
|
|
|
const ReactNativeBridgeEventPlugin = {
|
2022-10-04 05:03:33 +08:00
|
|
|
eventTypes: ({}: EventTypes),
|
2016-03-25 06:40:52 +08:00
|
|
|
|
2023-01-31 21:25:05 +08:00
|
|
|
extractEvents: function (
|
2018-05-15 17:38:50 +08:00
|
|
|
topLevelType: TopLevelType,
|
2018-06-30 03:51:48 +08:00
|
|
|
targetInst: null | Object,
|
2017-11-15 09:48:40 +08:00
|
|
|
nativeEvent: AnyNativeEvent,
|
2019-11-12 03:22:41 +08:00
|
|
|
nativeEventTarget: null | Object,
|
2016-03-25 06:40:52 +08:00
|
|
|
): ?Object {
|
2018-02-22 05:47:17 +08:00
|
|
|
if (targetInst == null) {
|
|
|
|
|
// Probably a node belonging to another renderer's tree.
|
|
|
|
|
return null;
|
|
|
|
|
}
|
2017-09-15 05:55:32 +08:00
|
|
|
const bubbleDispatchConfig = customBubblingEventTypes[topLevelType];
|
|
|
|
|
const directDispatchConfig = customDirectEventTypes[topLevelType];
|
[RFC] Codemod invariant -> throw new Error (#22435)
* Hoist error codes import to module scope
When this code was written, the error codes map (`codes.json`) was
created on-the-fly, so we had to lazily require from inside the visitor.
Because `codes.json` is now checked into source, we can import it a
single time in module scope.
* Minify error constructors in production
We use a script to minify our error messages in production. Each message
is assigned an error code, defined in `scripts/error-codes/codes.json`.
Then our build script replaces the messages with a link to our
error decoder page, e.g. https://reactjs.org/docs/error-decoder.html/?invariant=92
This enables us to write helpful error messages without increasing the
bundle size.
Right now, the script only works for `invariant` calls. It does not work
if you throw an Error object. This is an old Facebookism that we don't
really need, other than the fact that our error minification script
relies on it.
So, I've updated the script to minify error constructors, too:
Input:
Error(`A ${adj} message that contains ${noun}`);
Output:
Error(formatProdErrorMessage(ERR_CODE, adj, noun));
It only works for constructors that are literally named Error, though we
could add support for other names, too.
As a next step, I will add a lint rule to enforce that errors written
this way must have a corresponding error code.
* Minify "no fallback UI specified" error in prod
This error message wasn't being minified because it doesn't use
invariant. The reason it didn't use invariant is because this particular
error is created without begin thrown — it doesn't need to be thrown
because it's located inside the error handling part of the runtime.
Now that the error minification script supports Error constructors, we
can minify it by assigning it a production error code in
`scripts/error-codes/codes.json`.
To support the use of Error constructors more generally, I will add a
lint rule that enforces each message has a corresponding error code.
* Lint rule to detect unminified errors
Adds a lint rule that detects when an Error constructor is used without
a corresponding production error code.
We already have this for `invariant`, but not for regular errors, i.e.
`throw new Error(msg)`. There's also nothing that enforces the use of
`invariant` besides convention.
There are some packages where we don't care to minify errors. These are
packages that run in environments where bundle size is not a concern,
like react-pg. I added an override in the ESLint config to ignore these.
* Temporarily add invariant codemod script
I'm adding this codemod to the repo temporarily, but I'll revert it
in the same PR. That way we don't have to check it in but it's still
accessible (via the PR) if we need it later.
* [Automated] Codemod invariant -> Error
This commit contains only automated changes:
npx jscodeshift -t scripts/codemod-invariant.js packages --ignore-pattern="node_modules/**/*"
yarn linc --fix
yarn prettier
I will do any manual touch ups in separate commits so they're easier
to review.
* Remove temporary codemod script
This reverts the codemod script and ESLint config I added temporarily
in order to perform the invariant codemod.
* Manual touch ups
A few manual changes I made after the codemod ran.
* Enable error code transform per package
Currently we're not consistent about which packages should have their
errors minified in production and which ones should.
This adds a field to the bundle configuration to control whether to
apply the transform. We should decide what the criteria is going
forward. I think it's probably a good idea to minify any package that
gets sent over the network. So yes to modules that run in the browser,
and no to modules that run on the server and during development only.
2021-10-01 03:01:28 +08:00
|
|
|
|
|
|
|
|
if (!bubbleDispatchConfig && !directDispatchConfig) {
|
|
|
|
|
throw new Error(
|
2023-03-27 19:43:04 +08:00
|
|
|
// $FlowFixMe[incompatible-type] - Flow doesn't like this string coercion because DOMTopLevelEventType is opaque
|
[RFC] Codemod invariant -> throw new Error (#22435)
* Hoist error codes import to module scope
When this code was written, the error codes map (`codes.json`) was
created on-the-fly, so we had to lazily require from inside the visitor.
Because `codes.json` is now checked into source, we can import it a
single time in module scope.
* Minify error constructors in production
We use a script to minify our error messages in production. Each message
is assigned an error code, defined in `scripts/error-codes/codes.json`.
Then our build script replaces the messages with a link to our
error decoder page, e.g. https://reactjs.org/docs/error-decoder.html/?invariant=92
This enables us to write helpful error messages without increasing the
bundle size.
Right now, the script only works for `invariant` calls. It does not work
if you throw an Error object. This is an old Facebookism that we don't
really need, other than the fact that our error minification script
relies on it.
So, I've updated the script to minify error constructors, too:
Input:
Error(`A ${adj} message that contains ${noun}`);
Output:
Error(formatProdErrorMessage(ERR_CODE, adj, noun));
It only works for constructors that are literally named Error, though we
could add support for other names, too.
As a next step, I will add a lint rule to enforce that errors written
this way must have a corresponding error code.
* Minify "no fallback UI specified" error in prod
This error message wasn't being minified because it doesn't use
invariant. The reason it didn't use invariant is because this particular
error is created without begin thrown — it doesn't need to be thrown
because it's located inside the error handling part of the runtime.
Now that the error minification script supports Error constructors, we
can minify it by assigning it a production error code in
`scripts/error-codes/codes.json`.
To support the use of Error constructors more generally, I will add a
lint rule that enforces each message has a corresponding error code.
* Lint rule to detect unminified errors
Adds a lint rule that detects when an Error constructor is used without
a corresponding production error code.
We already have this for `invariant`, but not for regular errors, i.e.
`throw new Error(msg)`. There's also nothing that enforces the use of
`invariant` besides convention.
There are some packages where we don't care to minify errors. These are
packages that run in environments where bundle size is not a concern,
like react-pg. I added an override in the ESLint config to ignore these.
* Temporarily add invariant codemod script
I'm adding this codemod to the repo temporarily, but I'll revert it
in the same PR. That way we don't have to check it in but it's still
accessible (via the PR) if we need it later.
* [Automated] Codemod invariant -> Error
This commit contains only automated changes:
npx jscodeshift -t scripts/codemod-invariant.js packages --ignore-pattern="node_modules/**/*"
yarn linc --fix
yarn prettier
I will do any manual touch ups in separate commits so they're easier
to review.
* Remove temporary codemod script
This reverts the codemod script and ESLint config I added temporarily
in order to perform the invariant codemod.
* Manual touch ups
A few manual changes I made after the codemod ran.
* Enable error code transform per package
Currently we're not consistent about which packages should have their
errors minified in production and which ones should.
This adds a field to the bundle configuration to control whether to
apply the transform. We should decide what the criteria is going
forward. I think it's probably a good idea to minify any package that
gets sent over the network. So yes to modules that run in the browser,
and no to modules that run on the server and during development only.
2021-10-01 03:01:28 +08:00
|
|
|
`Unsupported top level event type "${topLevelType}" dispatched`,
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2017-09-15 05:55:32 +08:00
|
|
|
const event = SyntheticEvent.getPooled(
|
2016-03-25 06:40:52 +08:00
|
|
|
bubbleDispatchConfig || directDispatchConfig,
|
2016-03-31 05:35:46 +08:00
|
|
|
targetInst,
|
|
|
|
|
nativeEvent,
|
2017-03-14 08:05:18 +08:00
|
|
|
nativeEventTarget,
|
2016-03-25 06:40:52 +08:00
|
|
|
);
|
|
|
|
|
if (bubbleDispatchConfig) {
|
2022-03-15 01:59:21 +08:00
|
|
|
const skipBubbling =
|
|
|
|
|
event != null &&
|
|
|
|
|
event.dispatchConfig.phasedRegistrationNames != null &&
|
|
|
|
|
event.dispatchConfig.phasedRegistrationNames.skipBubbling;
|
|
|
|
|
if (skipBubbling) {
|
|
|
|
|
accumulateCapturePhaseDispatches(event);
|
|
|
|
|
} else {
|
|
|
|
|
accumulateTwoPhaseDispatches(event);
|
|
|
|
|
}
|
2016-03-25 06:40:52 +08:00
|
|
|
} else if (directDispatchConfig) {
|
2017-11-05 19:58:36 +08:00
|
|
|
accumulateDirectDispatches(event);
|
2016-03-25 06:40:52 +08:00
|
|
|
} else {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
return event;
|
2016-04-19 07:11:31 +08:00
|
|
|
},
|
2016-03-25 06:40:52 +08:00
|
|
|
};
|
|
|
|
|
|
2017-11-03 03:50:03 +08:00
|
|
|
export default ReactNativeBridgeEventPlugin;
|