Skip to content

Commit 93efaeb

Browse files
Vin-Xifacebook-github-bot
authored andcommitted
refactor: rewrite Inspector from Java to Kotlin (#50947)
Summary: Rewrite of the Inspector class from Java to Kotlin in scope of #50513 ## Changelog: [ANDROID] [CHANGED] - Migrated Inspector to Kotlin Pull Request resolved: #50947 Test Plan: Tested using RNTester app, on both old and new arch, and tested by navigating to multiple pages Reviewed By: cortinico Differential Revision: D73767386 Pulled By: javache fbshipit-source-id: e0098568aa0ed9863503e206a88d3b171c8f9966
1 parent 0e963aa commit 93efaeb

File tree

3 files changed

+94
-122
lines changed

3 files changed

+94
-122
lines changed

packages/react-native/ReactAndroid/api/ReactAndroid.api

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -737,20 +737,26 @@ public abstract class com/facebook/react/bridge/GuardedRunnable : java/lang/Runn
737737
public abstract fun runGuarded ()V
738738
}
739739

740-
public class com/facebook/react/bridge/Inspector {
741-
public static fun connect (ILcom/facebook/react/bridge/Inspector$RemoteConnection;)Lcom/facebook/react/bridge/Inspector$LocalConnection;
742-
public static fun getPages ()Ljava/util/List;
740+
public final class com/facebook/react/bridge/Inspector {
741+
public static final field Companion Lcom/facebook/react/bridge/Inspector$Companion;
742+
public static final fun connect (ILcom/facebook/react/bridge/Inspector$RemoteConnection;)Lcom/facebook/react/bridge/Inspector$LocalConnection;
743+
public static final fun getPages ()Ljava/util/List;
743744
}
744745

745-
public class com/facebook/react/bridge/Inspector$LocalConnection {
746-
public fun disconnect ()V
747-
public fun sendMessage (Ljava/lang/String;)V
746+
public final class com/facebook/react/bridge/Inspector$Companion {
747+
public final fun connect (ILcom/facebook/react/bridge/Inspector$RemoteConnection;)Lcom/facebook/react/bridge/Inspector$LocalConnection;
748+
public final fun getPages ()Ljava/util/List;
748749
}
749750

750-
public class com/facebook/react/bridge/Inspector$Page {
751-
public fun getId ()I
752-
public fun getTitle ()Ljava/lang/String;
753-
public fun getVM ()Ljava/lang/String;
751+
public final class com/facebook/react/bridge/Inspector$LocalConnection {
752+
public final fun disconnect ()V
753+
public final fun sendMessage (Ljava/lang/String;)V
754+
}
755+
756+
public final class com/facebook/react/bridge/Inspector$Page {
757+
public final fun getId ()I
758+
public final fun getTitle ()Ljava/lang/String;
759+
public final fun getVM ()Ljava/lang/String;
754760
public fun toString ()Ljava/lang/String;
755761
}
756762

packages/react-native/ReactAndroid/src/main/java/com/facebook/react/bridge/Inspector.java

Lines changed: 0 additions & 112 deletions
This file was deleted.
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
/*
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
8+
package com.facebook.react.bridge
9+
10+
import com.facebook.common.logging.FLog
11+
import com.facebook.jni.HybridData
12+
import com.facebook.proguard.annotations.DoNotStrip
13+
import com.facebook.react.common.ReactConstants
14+
15+
@DoNotStrip
16+
public class Inspector
17+
private constructor(@Suppress("NoHungarianNotation") private val mHybridData: HybridData) {
18+
19+
private external fun getPagesNative(): Array<Page>
20+
21+
private external fun connectNative(pageId: Int, remote: RemoteConnection): LocalConnection?
22+
23+
@DoNotStrip
24+
public class Page
25+
private constructor(private val id: Int, private val title: String, private val vm: String) {
26+
public fun getId(): Int = id
27+
28+
public fun getTitle(): String = title
29+
30+
public fun getVM(): String = vm
31+
32+
override fun toString(): String = "Page{id=$id, title='$title'}"
33+
}
34+
35+
@DoNotStrip
36+
public interface RemoteConnection {
37+
@DoNotStrip public fun onMessage(message: String)
38+
39+
@DoNotStrip public fun onDisconnect()
40+
}
41+
42+
@DoNotStrip
43+
public class LocalConnection
44+
private constructor(@Suppress("NoHungarianNotation") private val mHybridData: HybridData) {
45+
public external fun sendMessage(message: String)
46+
47+
public external fun disconnect()
48+
}
49+
50+
public companion object {
51+
init {
52+
BridgeSoLoader.staticInit()
53+
}
54+
55+
@JvmStatic
56+
public fun getPages(): List<Page> {
57+
return try {
58+
instance().getPagesNative().toList()
59+
} catch (e: UnsatisfiedLinkError) {
60+
FLog.e(ReactConstants.TAG, "Inspector doesn't work in open source yet", e)
61+
emptyList()
62+
}
63+
}
64+
65+
@JvmStatic
66+
public fun connect(pageId: Int, remote: RemoteConnection): LocalConnection {
67+
return try {
68+
instance().connectNative(pageId, remote)
69+
?: throw IllegalStateException("Can't open failed connection")
70+
} catch (e: UnsatisfiedLinkError) {
71+
FLog.e(ReactConstants.TAG, "Inspector doesn't work in open source yet", e)
72+
throw RuntimeException(e)
73+
}
74+
}
75+
76+
@JvmStatic private external fun instance(): Inspector
77+
}
78+
}

0 commit comments

Comments
 (0)