From 93fb6a9fd277e04073276d186d658561a1714f10 Mon Sep 17 00:00:00 2001 From: Ruslan Shestopalyuk Date: Fri, 26 Jul 2024 21:47:17 -0700 Subject: [PATCH 1/3] Migrate ContenstSizeChangeEvent.java->.kt Differential Revision: D60280502 --- .../ReactAndroid/api/ReactAndroid.api | 4 +- .../events/ContentSizeChangeEvent.java | 52 ------------------- .../events/ContentSizeChangeEvent.kt | 37 +++++++++++++ 3 files changed, 38 insertions(+), 55 deletions(-) delete mode 100644 packages/react-native/ReactAndroid/src/main/java/com/facebook/react/uimanager/events/ContentSizeChangeEvent.java create mode 100644 packages/react-native/ReactAndroid/src/main/java/com/facebook/react/uimanager/events/ContentSizeChangeEvent.kt diff --git a/packages/react-native/ReactAndroid/api/ReactAndroid.api b/packages/react-native/ReactAndroid/api/ReactAndroid.api index bfd2cd653aee..ac428f9ee5e0 100644 --- a/packages/react-native/ReactAndroid/api/ReactAndroid.api +++ b/packages/react-native/ReactAndroid/api/ReactAndroid.api @@ -5561,11 +5561,9 @@ public class com/facebook/react/uimanager/events/BlackHoleEventDispatcher : com/ public fun unregisterEventEmitter (I)V } -public class com/facebook/react/uimanager/events/ContentSizeChangeEvent : com/facebook/react/uimanager/events/Event { - public static final field EVENT_NAME Ljava/lang/String; +public final class com/facebook/react/uimanager/events/ContentSizeChangeEvent : com/facebook/react/uimanager/events/Event { public fun (III)V public fun (IIII)V - protected fun getEventData ()Lcom/facebook/react/bridge/WritableMap; public fun getEventName ()Ljava/lang/String; } diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/uimanager/events/ContentSizeChangeEvent.java b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/uimanager/events/ContentSizeChangeEvent.java deleted file mode 100644 index 09870620d04b..000000000000 --- a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/uimanager/events/ContentSizeChangeEvent.java +++ /dev/null @@ -1,52 +0,0 @@ -/* - * 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. - */ - -package com.facebook.react.uimanager.events; - -import com.facebook.infer.annotation.Nullsafe; -import com.facebook.react.bridge.Arguments; -import com.facebook.react.bridge.WritableMap; -import com.facebook.react.uimanager.PixelUtil; - -/** - * Event dispatched when total width or height of a view's children changes. - * - * @deprecated Please define your own event for custom components - */ -@Nullsafe(Nullsafe.Mode.LOCAL) -@Deprecated -public class ContentSizeChangeEvent extends Event { - - public static final String EVENT_NAME = "topContentSizeChange"; - - private final int mWidth; - private final int mHeight; - - @Deprecated - public ContentSizeChangeEvent(int viewTag, int width, int height) { - this(-1, viewTag, width, height); - } - - public ContentSizeChangeEvent(int surfaceId, int viewTag, int width, int height) { - super(surfaceId, viewTag); - mWidth = width; - mHeight = height; - } - - @Override - public String getEventName() { - return EVENT_NAME; - } - - @Override - protected WritableMap getEventData() { - WritableMap data = Arguments.createMap(); - data.putDouble("width", PixelUtil.toDIPFromPixel(mWidth)); - data.putDouble("height", PixelUtil.toDIPFromPixel(mHeight)); - return data; - } -} diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/uimanager/events/ContentSizeChangeEvent.kt b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/uimanager/events/ContentSizeChangeEvent.kt new file mode 100644 index 000000000000..91ec7d2bc6ee --- /dev/null +++ b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/uimanager/events/ContentSizeChangeEvent.kt @@ -0,0 +1,37 @@ +/* + * 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. + */ + +@file:Suppress("DEPRECATION") + +package com.facebook.react.uimanager.events + +import com.facebook.react.bridge.Arguments +import com.facebook.react.bridge.WritableMap +import com.facebook.react.uimanager.PixelUtil.toDIPFromPixel + +/** Event dispatched when total width or height of a view's children changes. */ +@Deprecated("Please define your own event for custom components") +public class ContentSizeChangeEvent( + surfaceId: Int, + viewTag: Int, + private val width: Int, + private val height: Int +) : Event(surfaceId, viewTag) { + @Deprecated( + "Please specify surfaceId explicitly in the constructor.", + ReplaceWith("constructor(surfaceId, viewTag, width, height)")) + public constructor(viewTag: Int, width: Int, height: Int) : this(-1, viewTag, width, height) + + public override fun getEventName(): String = "topContentSizeChange" + + protected override fun getEventData(): WritableMap { + val res = Arguments.createMap() + res.putDouble("width", toDIPFromPixel(width.toFloat()).toDouble()) + res.putDouble("height", toDIPFromPixel(height.toFloat()).toDouble()) + return res + } +} From 877a8a47daf5d8b1278e9fabefef1fa43ce7b4bb Mon Sep 17 00:00:00 2001 From: Ruslan Shestopalyuk Date: Fri, 26 Jul 2024 21:47:17 -0700 Subject: [PATCH 2/3] Executors.java->.kt Differential Revision: D60282843 --- .../runtime/internal/bolts/Executors.java | 53 ------------------- .../react/runtime/internal/bolts/Executors.kt | 44 +++++++++++++++ 2 files changed, 44 insertions(+), 53 deletions(-) delete mode 100644 packages/react-native/ReactAndroid/src/main/java/com/facebook/react/runtime/internal/bolts/Executors.java create mode 100644 packages/react-native/ReactAndroid/src/main/java/com/facebook/react/runtime/internal/bolts/Executors.kt diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/runtime/internal/bolts/Executors.java b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/runtime/internal/bolts/Executors.java deleted file mode 100644 index 3b7ee962a996..000000000000 --- a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/runtime/internal/bolts/Executors.java +++ /dev/null @@ -1,53 +0,0 @@ -/* - * 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. - */ - -package com.facebook.react.runtime.internal.bolts; - -import com.facebook.infer.annotation.Nullsafe; -import com.facebook.react.bridge.UiThreadUtil; -import java.util.concurrent.Executor; - -/** - * This was created because the helper methods in {@link java.util.concurrent.Executors} do not work - * as people would normally expect. - * - *

Normally, you would think that a cached thread pool would create new threads when necessary, - * queue them when the pool is full, and kill threads when they've been inactive for a certain - * period of time. This is not how {@link java.util.concurrent.Executors#newCachedThreadPool()} - * works. - * - *

Instead, {@link java.util.concurrent.Executors#newCachedThreadPool()} executes all tasks on a - * new or cached thread immediately because corePoolSize is 0, SynchronousQueue is a queue with size - * 0 and maxPoolSize is Integer.MAX_VALUE. This is dangerous because it can create an unchecked - * amount of threads. - */ -@Nullsafe(Nullsafe.Mode.LOCAL) -/* package */ -final class Executors { - public static final Executor UI_THREAD = new UIThreadExecutor(); - - public static final Executor IMMEDIATE = new ImmediateExecutor(); - - private static class UIThreadExecutor implements Executor { - @Override - public void execute(Runnable command) { - // Otherwise, post it on the main thread handler - UiThreadUtil.runOnUiThread(command); - } - } - - /** - * An {@link java.util.concurrent.Executor} that schedules tasks to run asynchronously on the UI - * thread. - */ - private static class ImmediateExecutor implements Executor { - @Override - public void execute(Runnable command) { - command.run(); - } - } -} diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/runtime/internal/bolts/Executors.kt b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/runtime/internal/bolts/Executors.kt new file mode 100644 index 000000000000..d6815157eec6 --- /dev/null +++ b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/runtime/internal/bolts/Executors.kt @@ -0,0 +1,44 @@ +/* + * 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. + */ + +package com.facebook.react.runtime.internal.bolts + +import com.facebook.react.bridge.UiThreadUtil +import java.util.concurrent.Executor + +/** + * This was created because the helper methods in [java.util.concurrent.Executors] do not work as + * people would normally expect. + * + * Normally, you would think that a cached thread pool would create new threads when necessary, + * queue them when the pool is full, and kill threads when they've been inactive for a certain + * period of time. This is not how [java.util.concurrent.Executors.newCachedThreadPool] works. + * + * Instead, [java.util.concurrent.Executors.newCachedThreadPool] executes all tasks on a new or + * cached thread immediately because corePoolSize is 0, SynchronousQueue is a queue with size 0 and + * maxPoolSize is Integer.MAX_VALUE. This is dangerous because it can create an unchecked amount of + * threads. + */ +internal object Executors { + @JvmField public val UI_THREAD: Executor = UIThreadExecutor() + @JvmField public val IMMEDIATE: Executor = ImmediateExecutor() + + private class UIThreadExecutor : Executor { + override fun execute(command: Runnable) { + UiThreadUtil.runOnUiThread(command) + } + } + + /** + * An [java.util.concurrent.Executor] that schedules tasks to run asynchronously on the UI thread. + */ + private class ImmediateExecutor : Executor { + override fun execute(command: Runnable) { + command.run() + } + } +} From bcd001c846a7c4815287fc4c1ab9bf3ecb4a037f Mon Sep 17 00:00:00 2001 From: Ruslan Shestopalyuk Date: Fri, 26 Jul 2024 22:00:11 -0700 Subject: [PATCH 3/3] Convert BlackHoleEventDispatcher to Kotlin (#45714) Summary: Pull Request resolved: https://github.com/facebook/react-native/pull/45714 # Changelog: [Internal] - As in the title. Reviewed By: tdn120 Differential Revision: D60283138 --- .../ReactAndroid/api/ReactAndroid.api | 9 ++- .../events/BlackHoleEventDispatcher.java | 62 ------------------- .../events/BlackHoleEventDispatcher.kt | 57 +++++++++++++++++ 3 files changed, 64 insertions(+), 64 deletions(-) delete mode 100644 packages/react-native/ReactAndroid/src/main/java/com/facebook/react/uimanager/events/BlackHoleEventDispatcher.java create mode 100644 packages/react-native/ReactAndroid/src/main/java/com/facebook/react/uimanager/events/BlackHoleEventDispatcher.kt diff --git a/packages/react-native/ReactAndroid/api/ReactAndroid.api b/packages/react-native/ReactAndroid/api/ReactAndroid.api index ac428f9ee5e0..5c6c18daa648 100644 --- a/packages/react-native/ReactAndroid/api/ReactAndroid.api +++ b/packages/react-native/ReactAndroid/api/ReactAndroid.api @@ -5547,12 +5547,13 @@ public abstract interface class com/facebook/react/uimanager/events/BatchEventDi public abstract fun onBatchEventDispatched ()V } -public class com/facebook/react/uimanager/events/BlackHoleEventDispatcher : com/facebook/react/uimanager/events/EventDispatcher { +public final class com/facebook/react/uimanager/events/BlackHoleEventDispatcher : com/facebook/react/uimanager/events/EventDispatcher { + public static final field Companion Lcom/facebook/react/uimanager/events/BlackHoleEventDispatcher$Companion; public fun addBatchEventDispatchedListener (Lcom/facebook/react/uimanager/events/BatchEventDispatchedListener;)V public fun addListener (Lcom/facebook/react/uimanager/events/EventDispatcherListener;)V public fun dispatchAllEvents ()V public fun dispatchEvent (Lcom/facebook/react/uimanager/events/Event;)V - public static fun get ()Lcom/facebook/react/uimanager/events/EventDispatcher; + public static final fun get ()Lcom/facebook/react/uimanager/events/EventDispatcher; public fun onCatalystInstanceDestroyed ()V public fun registerEventEmitter (ILcom/facebook/react/uimanager/events/RCTEventEmitter;)V public fun registerEventEmitter (ILcom/facebook/react/uimanager/events/RCTModernEventEmitter;)V @@ -5561,6 +5562,10 @@ public class com/facebook/react/uimanager/events/BlackHoleEventDispatcher : com/ public fun unregisterEventEmitter (I)V } +public final class com/facebook/react/uimanager/events/BlackHoleEventDispatcher$Companion { + public final fun get ()Lcom/facebook/react/uimanager/events/EventDispatcher; +} + public final class com/facebook/react/uimanager/events/ContentSizeChangeEvent : com/facebook/react/uimanager/events/Event { public fun (III)V public fun (IIII)V diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/uimanager/events/BlackHoleEventDispatcher.java b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/uimanager/events/BlackHoleEventDispatcher.java deleted file mode 100644 index 8b7a36d85b5f..000000000000 --- a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/uimanager/events/BlackHoleEventDispatcher.java +++ /dev/null @@ -1,62 +0,0 @@ -/* - * 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. - */ - -package com.facebook.react.uimanager.events; - -import com.facebook.common.logging.FLog; -import com.facebook.infer.annotation.Nullsafe; - -/** - * A singleton class that overrides {@link EventDispatcher} with no-op methods, to be used by - * callers that expect an EventDispatcher when the instance doesn't exist. - */ -@Nullsafe(Nullsafe.Mode.LOCAL) -public class BlackHoleEventDispatcher implements EventDispatcher { - - private static final EventDispatcher sEventDispatcher = new BlackHoleEventDispatcher(); - - public static EventDispatcher get() { - return sEventDispatcher; - } - - private BlackHoleEventDispatcher() {} - - @Override - public void dispatchEvent(Event event) { - FLog.d( - getClass().getSimpleName(), - "Trying to emit event to JS, but the React instance isn't ready. Event: " - + event.getEventName()); - } - - @Override - public void dispatchAllEvents() {} - - @Override - public void addListener(EventDispatcherListener listener) {} - - @Override - public void removeListener(EventDispatcherListener listener) {} - - @Override - public void addBatchEventDispatchedListener(BatchEventDispatchedListener listener) {} - - @Override - public void removeBatchEventDispatchedListener(BatchEventDispatchedListener listener) {} - - @Override - public void registerEventEmitter(int uiManagerType, RCTEventEmitter eventEmitter) {} - - @Override - public void registerEventEmitter(int uiManagerType, RCTModernEventEmitter eventEmitter) {} - - @Override - public void unregisterEventEmitter(int uiManagerType) {} - - @Override - public void onCatalystInstanceDestroyed() {} -} diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/uimanager/events/BlackHoleEventDispatcher.kt b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/uimanager/events/BlackHoleEventDispatcher.kt new file mode 100644 index 000000000000..e25e980eb409 --- /dev/null +++ b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/uimanager/events/BlackHoleEventDispatcher.kt @@ -0,0 +1,57 @@ +/* + * 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. + */ + +package com.facebook.react.uimanager.events + +import com.facebook.common.logging.FLog + +/** + * A singleton class that overrides [EventDispatcher] with no-op methods, to be used by callers that + * expect an EventDispatcher when the instance doesn't exist. + */ +public class BlackHoleEventDispatcher private constructor() : EventDispatcher { + public override fun dispatchEvent(event: Event<*>) { + FLog.d( + "BlackHoleEventDispatcher", + "Trying to emit event to JS, but the React instance isn't ready. Event: ${event.eventName}") + } + + public override fun dispatchAllEvents(): Unit = Unit + + public override fun addListener(listener: EventDispatcherListener): Unit = Unit + + public override fun removeListener(listener: EventDispatcherListener): Unit = Unit + + public override fun addBatchEventDispatchedListener( + listener: BatchEventDispatchedListener + ): Unit = Unit + + public override fun removeBatchEventDispatchedListener( + listener: BatchEventDispatchedListener + ): Unit = Unit + + @Suppress("DEPRECATION") + public override fun registerEventEmitter( + uiManagerType: Int, + eventEmitter: RCTEventEmitter + ): Unit = Unit + + public override fun registerEventEmitter( + uiManagerType: Int, + eventEmitter: RCTModernEventEmitter + ): Unit = Unit + + public override fun unregisterEventEmitter(uiManagerType: Int): Unit = Unit + + public override fun onCatalystInstanceDestroyed(): Unit = Unit + + public companion object { + private val eventDispatcher: EventDispatcher = BlackHoleEventDispatcher() + + public @JvmStatic fun get(): EventDispatcher = eventDispatcher + } +}