Skip to content

Commit

Permalink
Expose RawEvent::Category to Java callsites
Browse files Browse the repository at this point in the history
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
  • Loading branch information
Andrei Shikov authored and facebook-github-bot committed Sep 29, 2021
1 parent a0c3c85 commit bf4c6b3
Show file tree
Hide file tree
Showing 7 changed files with 70 additions and 15 deletions.
Original file line number Diff line number Diff line change
@@ -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;
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
13 changes: 9 additions & 4 deletions ReactCommon/react/renderer/core/EventEmitter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
3 changes: 2 additions & 1 deletion ReactCommon/react/renderer/core/EventEmitter.h
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
12 changes: 7 additions & 5 deletions ReactCommon/react/renderer/core/RawEvent.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,37 +23,39 @@ 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
* 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.
*/
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(
Expand Down

0 comments on commit bf4c6b3

Please sign in to comment.