diff --git a/ReactAndroid/src/main/java/com/facebook/react/fabric/events/EventCategoryDef.java b/ReactAndroid/src/main/java/com/facebook/react/fabric/events/EventCategoryDef.java new file mode 100644 index 00000000000000..f55fa701c02828 --- /dev/null +++ b/ReactAndroid/src/main/java/com/facebook/react/fabric/events/EventCategoryDef.java @@ -0,0 +1,42 @@ +/* + * Copyright (c) Facebook, Inc. and its affiliates. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + */ + +package com.facebook.react.fabric.events; + +import static com.facebook.react.fabric.events.EventCategoryDef.CONTINUOUS; +import static com.facebook.react.fabric.events.EventCategoryDef.CONTINUOUS_END; +import static com.facebook.react.fabric.events.EventCategoryDef.CONTINUOUS_START; +import static com.facebook.react.fabric.events.EventCategoryDef.DISCRETE; +import static com.facebook.react.fabric.events.EventCategoryDef.UNSPECIFIED; + +import androidx.annotation.IntDef; + +/** + * Java specific declaration of the `RawEvent::Category` enum. Keep in sync with + * `renderer/core/RawEvent.h`. + */ +@IntDef(value = {CONTINUOUS_START, CONTINUOUS_END, UNSPECIFIED, DISCRETE, CONTINUOUS}) +public @interface EventCategoryDef { + /** Start of a continuous event. To be used with touchStart. */ + int CONTINUOUS_START = 0; + + /** End of a continuous event. To be used with touchEnd. */ + int CONTINUOUS_END = 1; + + /** + * Priority for this event will be determined from other events in the queue. If it is triggered + * by continuous event, its priority will be default. If it is not triggered by continuous event, + * its priority will be discrete. + */ + int UNSPECIFIED = 2; + + /** Forces discrete type for the event. Regardless if continuous event is ongoing. */ + int DISCRETE = 3; + + /** Forces continuous type for the event. Regardless if continuous event isn't ongoing. */ + int CONTINUOUS = 4; +} diff --git a/ReactAndroid/src/main/java/com/facebook/react/fabric/events/EventEmitterWrapper.java b/ReactAndroid/src/main/java/com/facebook/react/fabric/events/EventEmitterWrapper.java index e1652a4f2bdd45..234744b2cd9e57 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/fabric/events/EventEmitterWrapper.java +++ b/ReactAndroid/src/main/java/com/facebook/react/fabric/events/EventEmitterWrapper.java @@ -36,7 +36,8 @@ private EventEmitterWrapper() { mHybridData = initHybrid(); } - private native void invokeEvent(@NonNull String eventName, @NonNull NativeMap params); + private native void invokeEvent( + @NonNull String eventName, @NonNull NativeMap params, @EventCategoryDef int category); private native void invokeUniqueEvent( @NonNull String eventName, @NonNull NativeMap params, int customCoalesceKey); @@ -52,7 +53,7 @@ public synchronized void invoke(@NonNull String eventName, @Nullable WritableMap return; } NativeMap payload = params == null ? new WritableNativeMap() : (NativeMap) params; - invokeEvent(eventName, payload); + invokeEvent(eventName, payload, EventCategoryDef.UNSPECIFIED); } /** diff --git a/ReactAndroid/src/main/java/com/facebook/react/fabric/jni/EventEmitterWrapper.cpp b/ReactAndroid/src/main/java/com/facebook/react/fabric/jni/EventEmitterWrapper.cpp index b4e9899057505d..af207a64b7c1b1 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/fabric/jni/EventEmitterWrapper.cpp +++ b/ReactAndroid/src/main/java/com/facebook/react/fabric/jni/EventEmitterWrapper.cpp @@ -20,13 +20,17 @@ EventEmitterWrapper::initHybrid(jni::alias_ref) { void EventEmitterWrapper::invokeEvent( std::string eventName, - NativeMap *payload) { + NativeMap *payload, + int category) { // It is marginal, but possible for this to be constructed without a valid // EventEmitter. In those cases, make sure we noop/blackhole events instead of // crashing. if (eventEmitter != nullptr) { eventEmitter->dispatchEvent( - eventName, payload->consume(), EventPriority::AsynchronousBatched); + eventName, + payload->consume(), + EventPriority::AsynchronousBatched, + static_cast(category)); } } diff --git a/ReactAndroid/src/main/java/com/facebook/react/fabric/jni/EventEmitterWrapper.h b/ReactAndroid/src/main/java/com/facebook/react/fabric/jni/EventEmitterWrapper.h index bfcc6059758293..bc2d3cac90a48a 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/fabric/jni/EventEmitterWrapper.h +++ b/ReactAndroid/src/main/java/com/facebook/react/fabric/jni/EventEmitterWrapper.h @@ -25,7 +25,7 @@ class EventEmitterWrapper : public jni::HybridClass { SharedEventEmitter eventEmitter; - void invokeEvent(std::string eventName, NativeMap *params); + void invokeEvent(std::string eventName, NativeMap *params, int category); void invokeUniqueEvent( std::string eventName, NativeMap *params, diff --git a/ReactCommon/react/renderer/core/EventEmitter.cpp b/ReactCommon/react/renderer/core/EventEmitter.cpp index f1752192ce7b69..cbc4dfd29b82f9 100644 --- a/ReactCommon/react/renderer/core/EventEmitter.cpp +++ b/ReactCommon/react/renderer/core/EventEmitter.cpp @@ -52,10 +52,15 @@ EventEmitter::EventEmitter( void EventEmitter::dispatchEvent( const std::string &type, const folly::dynamic &payload, - EventPriority priority) const { - dispatchEvent(type, [payload](jsi::Runtime &runtime) { - return valueFromDynamic(runtime, payload); - }); + EventPriority priority, + RawEvent::Category category) const { + dispatchEvent( + type, + [payload](jsi::Runtime &runtime) { + return valueFromDynamic(runtime, payload); + }, + priority, + category); } void EventEmitter::dispatchUniqueEvent( diff --git a/ReactCommon/react/renderer/core/EventEmitter.h b/ReactCommon/react/renderer/core/EventEmitter.h index cb3caa465d2ba0..f2f484b0d398c3 100644 --- a/ReactCommon/react/renderer/core/EventEmitter.h +++ b/ReactCommon/react/renderer/core/EventEmitter.h @@ -77,7 +77,8 @@ class EventEmitter { void dispatchEvent( const std::string &type, const folly::dynamic &payload, - EventPriority priority = EventPriority::AsynchronousBatched) const; + EventPriority priority = EventPriority::AsynchronousBatched, + RawEvent::Category category = RawEvent::Category::Unspecified) const; void dispatchUniqueEvent( const std::string &type, diff --git a/ReactCommon/react/renderer/core/RawEvent.h b/ReactCommon/react/renderer/core/RawEvent.h index fc3de781d2b8b3..ff50464d4a3e6f 100644 --- a/ReactCommon/react/renderer/core/RawEvent.h +++ b/ReactCommon/react/renderer/core/RawEvent.h @@ -23,17 +23,19 @@ struct RawEvent { /* * Defines category of a native platform event. This is used to deduce types * of events for Concurrent Mode. + * This enum is duplicated for JNI access in `EventCategoryDef.java`, keep in + * sync. */ enum class Category { /* * Start of a continuous event. To be used with touchStart. */ - ContinuousStart, + ContinuousStart = 0, /* * End of a continuous event. To be used with touchEnd. */ - ContinuousEnd, + ContinuousEnd = 1, /* * Priority for this event will be determined from other events in the @@ -41,19 +43,19 @@ struct RawEvent { * default. If it is not triggered by continuous event, its priority will be * discrete. */ - Unspecified, + Unspecified = 2, /* * Forces discrete type for the event. Regardless if continuous event is * ongoing. */ - Discrete, + Discrete = 3, /* * Forces continuous type for the event. Regardless if continuous event * isn't ongoing. */ - Continuous + Continuous = 4 }; RawEvent(