Expose RawEvent::Category to Java callsites

Summary:
For iOS, event category deduction is done from the C++ code, but the touch events are handled on Java layer in Android. This change exposes the category parameter through the `EventEmitterWrapper` called from Java, allowing to define category for events in the future.

Changelog:
[Internal] - Expose event category through JNI

Reviewed By: mdvacca

Differential Revision: D31205587

fbshipit-source-id: f2373ce18464b01ac08eb87df8f421b33d100be2
This commit is contained in:
Andrei Shikov
2021-09-29 06:52:14 -07:00
committed by Facebook GitHub Bot
parent a0c3c85879
commit bf4c6b3606
7 changed files with 70 additions and 15 deletions
@@ -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;
}
@@ -36,7 +36,8 @@ public class 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 class EventEmitterWrapper {
return;
}
NativeMap payload = params == null ? new WritableNativeMap() : (NativeMap) params;
invokeEvent(eventName, payload);
invokeEvent(eventName, payload, EventCategoryDef.UNSPECIFIED);
}
/**
@@ -20,13 +20,17 @@ EventEmitterWrapper::initHybrid(jni::alias_ref<jclass>) {
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<RawEvent::Category>(category));
}
}
@@ -25,7 +25,7 @@ class EventEmitterWrapper : public jni::HybridClass<EventEmitterWrapper> {
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,