mirror of
https://github.com/facebook/react-native.git
synced 2025-11-01 09:14:26 +00:00
Summary: [Changelog][Internal] This adds a method, `emitDeviceEvent` to the C++ API of TurboModules, which allows to make calls to JS's `RCTDeviceEventEmitter.emit` from a C++ TurboModules. This is a very common pattern, specifically for the VR apps, but not only for them - e.g. Desktop fork also has a [custom implementation for this](https://www.internalfb.com/code/fbsource/third-party/microsoft-fork-of-react-native/react-native-utils/RCTEventEmitter.cpp). Note that my original intent was to actually backport the latter, however there are some complications with wiring things in a robust way, without exposing too much stuff and relying on singletons or folly::dynamic. So I ended up adding it to the TurboModule API itself and use the scheduler/JSI facilities instead. This approach is arguably well self-contained, uses high level APIs, and shouldn't be abusable much. Since I was trying to avoid usage of folly::dynamic in this case, I used a kind of "value factory" pattern instead in order to send the arguments to the JS thread in a thread safe way (see [the discussion here](https://fb.workplace.com/groups/rn.fabric/permalink/1398711453593610/)). Reviewed By: christophpurrer Differential Revision: D43466326 fbshipit-source-id: a3cb8359d08a46421559edd0f854772863cb5c39
33 lines
947 B
JavaScript
33 lines
947 B
JavaScript
/**
|
|
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
*
|
|
* This source code is licensed under the MIT license found in the
|
|
* LICENSE file in the root directory of this source tree.
|
|
*
|
|
* @flow strict
|
|
* @format
|
|
*/
|
|
|
|
import type {IEventEmitter} from '../vendor/emitter/EventEmitter';
|
|
|
|
import EventEmitter from '../vendor/emitter/EventEmitter';
|
|
|
|
// FIXME: use typed events
|
|
type RCTDeviceEventDefinitions = $FlowFixMe;
|
|
|
|
/**
|
|
* Global EventEmitter used by the native platform to emit events to JavaScript.
|
|
* Events are identified by globally unique event names.
|
|
*
|
|
* NativeModules that emit events should instead subclass `NativeEventEmitter`.
|
|
*/
|
|
const RCTDeviceEventEmitter: IEventEmitter<RCTDeviceEventDefinitions> =
|
|
new EventEmitter();
|
|
|
|
Object.defineProperty(global, '__rctDeviceEventEmitter', {
|
|
configurable: true,
|
|
value: RCTDeviceEventEmitter,
|
|
});
|
|
|
|
export default (RCTDeviceEventEmitter: IEventEmitter<RCTDeviceEventDefinitions>);
|