Skip to content

Commit e8d4f76

Browse files
committed
fix: format and flow issues
1 parent 2146a6e commit e8d4f76

8 files changed

Lines changed: 86 additions & 65 deletions

File tree

‎packages/react-native/ReactAndroid/src/main/java/com/facebook/react/activityresult/DeferredActivityResultLauncher.kt‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ import com.facebook.react.common.ReactConstants
2121
* unbound (fired on bind), and can be [unbind]-ed and rebound against a new host's registry.
2222
*
2323
* [delegate] and [pendingLaunch] are only touched on the UI thread; [launch] and [unregister] get
24-
* there via [onUiThread]. [launch] decides between delegating and queueing *on* the UI thread, so
25-
* a concurrent [unbind] cannot leave it pointed at a dead registry.
24+
* there via [onUiThread]. [launch] decides between delegating and queueing *on* the UI thread, so a
25+
* concurrent [unbind] cannot leave it pointed at a dead registry.
2626
*/
2727
internal class DeferredActivityResultLauncher<I>(
2828
private val key: String,
@@ -48,7 +48,8 @@ internal class DeferredActivityResultLauncher<I>(
4848
FLog.w(
4949
ReactConstants.TAG,
5050
"Launcher for '$key' was launched again before an Activity was available; " +
51-
"replacing the previously queued launch.")
51+
"replacing the previously queued launch.",
52+
)
5253
}
5354
pendingLaunch = PendingLaunch(input, options)
5455
}

‎packages/react-native/ReactAndroid/src/main/java/com/facebook/react/activityresult/ReactActivityResultCaller.kt‎

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,18 +20,17 @@ import androidx.activity.result.contract.ActivityResultContract
2020
* Every registration carries a key that must be unique within the `ReactContext` and stable across
2121
* process death (AndroidX replays a restored result to whichever registration reproduces the same
2222
* key). The default key `"<owner class>:<contract class>"` lets unrelated libraries register the
23-
* same stock contract without colliding; a collision throws [IllegalStateException] at
24-
* registration time, and the keyed overload (which appends to that scope, not replaces it)
25-
* resolves it.
23+
* same stock contract without colliding; a collision throws [IllegalStateException] at registration
24+
* time, and the keyed overload (which appends to that scope, not replaces it) resolves it.
2625
*/
2726
internal interface ReactActivityResultCaller {
2827

2928
/**
3029
* Registers [contract] under the key `"<owner class>:<contract class>"` and returns a launcher
3130
* for it. [owner] must be an instance of a named class — typically the native module itself.
3231
* Anonymous classes are rejected because their generated names can change between builds, which
33-
* breaks result delivery after the process is killed and restored. For the same reason, apps
34-
* that minify class names (R8/ProGuard) should keep the owner class's name, since the key is not
32+
* breaks result delivery after the process is killed and restored. For the same reason, apps that
33+
* minify class names (R8/ProGuard) should keep the owner class's name, since the key is not
3534
* guaranteed to be stable between builds otherwise.
3635
*
3736
* @throws IllegalArgumentException if [owner] is an instance of an anonymous class
@@ -44,11 +43,11 @@ internal interface ReactActivityResultCaller {
4443
): ActivityResultLauncher<I>
4544

4645
/**
47-
* Registers [contract] under the key `"<owner class>:<contract class>:<key>"`. Use this when
48-
* one owner needs several launchers of the same contract class. [key] only has to be unique
49-
* among those, but must stay the same across process restarts, so derive it from a constant.
50-
* [owner] carries the same requirements as the two-argument overload: it must be an instance of
51-
* a named class.
46+
* Registers [contract] under the key `"<owner class>:<contract class>:<key>"`. Use this when one
47+
* owner needs several launchers of the same contract class. [key] only has to be unique among
48+
* those, but must stay the same across process restarts, so derive it from a constant. [owner]
49+
* carries the same requirements as the two-argument overload: it must be an instance of a named
50+
* class.
5251
*
5352
* @throws IllegalArgumentException if [owner] is an instance of an anonymous class
5453
* @throws IllegalStateException if [owner] already registered this contract class under [key]

‎packages/react-native/ReactAndroid/src/main/java/com/facebook/react/activityresult/ReactActivityResultCallerImpl.kt‎

Lines changed: 23 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -35,15 +35,15 @@ internal fun onUiThread(block: () -> Unit) {
3535
* [ActivityResultRegistry] immediately or on the next `onHostResume`. They outlive any single
3636
* Activity: keys stay stable so AndroidX can re-associate a result after Activity recreation.
3737
*
38-
* Every `onHostResume` checks each launcher against the *current* registry, not just "already
39-
* bound to something": with multi-Activity navigation the new Activity resumes before the old one
40-
* is destroyed (whose onHostDestroy is dropped once `currentActivity` moves on), so a bound-only
41-
* check would leave launchers attached to the previous Activity's dead registry.
38+
* Every `onHostResume` checks each launcher against the *current* registry, not just "already bound
39+
* to something": with multi-Activity navigation the new Activity resumes before the old one is
40+
* destroyed (whose onHostDestroy is dropped once `currentActivity` moves on), so a bound-only check
41+
* would leave launchers attached to the previous Activity's dead registry.
4242
*
4343
* Threading: [entries] is concurrent and reachable from any thread; everything touching the
4444
* registry goes through [onUiThread]. Registration stays on the caller's thread so the launcher
45-
* returns immediately and a duplicate key throws at the causing frame. Only the registry call
46-
* moves to the UI thread.
45+
* returns immediately and a duplicate key throws at the causing frame. Only the registry call moves
46+
* to the UI thread.
4747
*/
4848
internal class ReactActivityResultCallerImpl(private val reactContext: ReactContext) :
4949
ReactActivityResultCaller, LifecycleEventListener {
@@ -78,10 +78,10 @@ internal class ReactActivityResultCallerImpl(private val reactContext: ReactCont
7878
contract: ActivityResultContract<I, O>,
7979
callback: ActivityResultCallback<O>,
8080
): ActivityResultLauncher<I> {
81-
if(owner::class.java.isAnonymousClass) {
81+
if (owner::class.java.isAnonymousClass) {
8282
throw IllegalArgumentException(
83-
"ActivityResult owner must be a named class, but got an anonymous class. " +
84-
"Pass an instance of a named class instead."
83+
"ActivityResult owner must be a named class, but got an anonymous class. " +
84+
"Pass an instance of a named class instead."
8585
)
8686
}
8787

@@ -91,7 +91,8 @@ internal class ReactActivityResultCallerImpl(private val reactContext: ReactCont
9191
"Register once and reuse the launcher, or pass a distinct key per launcher: " +
9292
"registerForActivityResult(owner, \"someName\", contract, callback).",
9393
contract = contract,
94-
callback = callback)
94+
callback = callback,
95+
)
9596
}
9697

9798
override fun <I, O> registerForActivityResult(
@@ -100,18 +101,19 @@ internal class ReactActivityResultCallerImpl(private val reactContext: ReactCont
100101
contract: ActivityResultContract<I, O>,
101102
callback: ActivityResultCallback<O>,
102103
): ActivityResultLauncher<I> {
103-
if(owner::class.java.isAnonymousClass) {
104+
if (owner::class.java.isAnonymousClass) {
104105
throw IllegalArgumentException(
105-
"ActivityResult owner must be a named class, but got an anonymous class. " +
106-
"Pass an instance of a named class instead."
106+
"ActivityResult owner must be a named class, but got an anonymous class. " +
107+
"Pass an instance of a named class instead."
107108
)
108109
}
109110

110111
return register(
111112
key = "${owner.javaClass.name}:${contract.javaClass.name}:$key",
112113
collisionHint = "Pass a key that is unique among this owner's launchers of this contract.",
113114
contract = contract,
114-
callback = callback)
115+
callback = callback,
116+
)
115117
}
116118

117119
private fun <I, O> register(
@@ -123,8 +125,7 @@ internal class ReactActivityResultCallerImpl(private val reactContext: ReactCont
123125
val launcher = DeferredActivityResultLauncher(key, contract) { entries.remove(key) }
124126
val entry = Entry(key, contract, callback, launcher)
125127
if (entries.putIfAbsent(key, entry) != null) {
126-
throw IllegalStateException(
127-
"A launcher is already registered for key '$key'. $collisionHint")
128+
throw IllegalStateException("A launcher is already registered for key '$key'. $collisionHint")
128129
}
129130
onUiThread { currentRegistry()?.let { registry -> entry.bindTo(registry) } }
130131
return launcher
@@ -145,11 +146,12 @@ internal class ReactActivityResultCallerImpl(private val reactContext: ReactCont
145146

146147
private fun currentRegistry(): ActivityResultRegistry? {
147148
val activity = reactContext.currentActivity ?: return null
148-
val owner = activity as? ActivityResultRegistryOwner
149-
?: throw IllegalStateException(
150-
"Current Activity ${activity.javaClass.name} is not an ActivityResultRegistryOwner; " +
151-
"ActivityResultContract launchers cannot be registered."
152-
)
149+
val owner =
150+
activity as? ActivityResultRegistryOwner
151+
?: throw IllegalStateException(
152+
"Current Activity ${activity.javaClass.name} is not an ActivityResultRegistryOwner; " +
153+
"ActivityResultContract launchers cannot be registered."
154+
)
153155
return owner.activityResultRegistry
154156
}
155157
}

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

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,11 @@
1616
import android.os.Bundle;
1717
import android.view.LayoutInflater;
1818
import android.view.Window;
19-
2019
import androidx.activity.result.ActivityResultCallback;
2120
import androidx.activity.result.ActivityResultLauncher;
2221
import androidx.activity.result.contract.ActivityResultContract;
2322
import androidx.annotation.NonNull;
2423
import androidx.annotation.Nullable;
25-
2624
import com.facebook.common.logging.FLog;
2725
import com.facebook.infer.annotation.Assertions;
2826
import com.facebook.infer.annotation.ThreadConfined;
@@ -35,7 +33,6 @@
3533
import com.facebook.react.common.build.ReactBuildConfig;
3634
import com.facebook.react.interfaces.ExtraWindowEventListener;
3735
import com.facebook.react.turbomodule.core.interfaces.CallInvokerHolder;
38-
3936
import java.lang.ref.WeakReference;
4037
import java.util.Collection;
4138
import java.util.concurrent.CopyOnWriteArraySet;
@@ -553,12 +550,11 @@ private synchronized ReactActivityResultCallerImpl getActivityResultCaller() {
553550
* consumer's {@code MainActivity}. Registration is legal at any time; the launcher binds lazily
554551
* once an Activity is available, queueing a {@code launch} issued while unbound.
555552
*
556-
* <p>The registration key is {@code "<owner class>:<contract class>"}, so {@code owner} should
557-
* be a stable, long-lived object (typically the native module itself): the key must be
558-
* reproducible after the process is killed and restored. Registering the same contract class
559-
* twice from one owner
560-
* throws {@link IllegalStateException}; use {@link #registerForActivityResult(Object, String,
561-
* ActivityResultContract, ActivityResultCallback)} in that case.
553+
* <p>The registration key is {@code "<owner class>:<contract class>"}, so {@code owner} should be
554+
* a stable, long-lived object (typically the native module itself): the key must be reproducible
555+
* after the process is killed and restored. Registering the same contract class twice from one
556+
* owner throws {@link IllegalStateException}; use {@link #registerForActivityResult(Object,
557+
* String, ActivityResultContract, ActivityResultCallback)} in that case.
562558
*/
563559
public <I, O> ActivityResultLauncher<I> registerForActivityResult(
564560
Object owner, ActivityResultContract<I, O> contract, ActivityResultCallback<O> callback) {

‎packages/react-native/ReactAndroid/src/test/java/com/facebook/react/activityresult/ReactActivityResultCallerImplTest.kt‎

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@ import org.robolectric.RobolectricTestRunner
2828

2929
/**
3030
* Covers the registration keying scheme: owner-scoped by default so two independent modules can use
31-
* the same stock contract, with an extra-key overload -- appended to that scope, not replacing it --
32-
* for one owner needing several launchers of the same contract class.
31+
* the same stock contract, with an extra-key overload -- appended to that scope, not replacing it
32+
* -- for one owner needing several launchers of the same contract class.
3333
*/
3434
@RunWith(RobolectricTestRunner::class)
3535
class ReactActivityResultCallerImplTest {
@@ -87,8 +87,7 @@ class ReactActivityResultCallerImplTest {
8787
caller.registerForActivityResult(moduleB, GetContent()) {}
8888

8989
assertThat(registry.registeredKeys)
90-
.containsExactlyInAnyOrder(
91-
"$moduleAName:$getContentName", "$moduleBName:$getContentName")
90+
.containsExactlyInAnyOrder("$moduleAName:$getContentName", "$moduleBName:$getContentName")
9291
}
9392

9493
@Test
@@ -107,7 +106,9 @@ class ReactActivityResultCallerImplTest {
107106

108107
assertThat(registry.registeredKeys)
109108
.containsExactlyInAnyOrder(
110-
"$moduleAName:$getContentName", "$moduleAName:${RequestPermission::class.java.name}")
109+
"$moduleAName:$getContentName",
110+
"$moduleAName:${RequestPermission::class.java.name}",
111+
)
111112
}
112113

113114
@Test
@@ -117,7 +118,9 @@ class ReactActivityResultCallerImplTest {
117118

118119
assertThat(registry.registeredKeys)
119120
.containsExactlyInAnyOrder(
120-
"$moduleAName:$getContentName:avatar", "$moduleAName:$getContentName:banner")
121+
"$moduleAName:$getContentName:avatar",
122+
"$moduleAName:$getContentName:banner",
123+
)
121124
}
122125

123126
/** The owner-and-contract scope is still applied, so a shared key across owners is safe. */
@@ -128,7 +131,9 @@ class ReactActivityResultCallerImplTest {
128131

129132
assertThat(registry.registeredKeys)
130133
.containsExactlyInAnyOrder(
131-
"$moduleAName:$getContentName:pick", "$moduleBName:$getContentName:pick")
134+
"$moduleAName:$getContentName:pick",
135+
"$moduleBName:$getContentName:pick",
136+
)
132137
}
133138

134139
@Test

‎packages/react-native/ReactAndroid/src/test/java/com/facebook/react/activityresult/ReactActivityResultCallerThreadingTest.kt‎

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,9 @@ class ReactActivityResultCallerThreadingTest {
127127

128128
var thrown: Throwable? = null
129129
onBackgroundThread {
130-
thrown = runCatching { caller.registerForActivityResult(moduleA, GetContent()) {} }.exceptionOrNull()
130+
thrown =
131+
runCatching { caller.registerForActivityResult(moduleA, GetContent()) {} }
132+
.exceptionOrNull()
131133
}
132134

133135
// Not surfaced later on the UI thread, where it would be unattributable.
@@ -151,9 +153,9 @@ class ReactActivityResultCallerThreadingTest {
151153
}
152154

153155
/**
154-
* Multi-Activity navigation: B resumes while A is still alive, and `ReactHostImpl` then drops
155-
* A's `onHostDestroy` because `currentActivity` has already moved to B. So no unbind ever runs
156-
* for A -- `onHostResume` alone has to move the launcher across.
156+
* Multi-Activity navigation: B resumes while A is still alive, and `ReactHostImpl` then drops A's
157+
* `onHostDestroy` because `currentActivity` has already moved to B. So no unbind ever runs for A
158+
* -- `onHostResume` alone has to move the launcher across.
157159
*/
158160
@Test
159161
fun `resuming a second activity rebinds to its registry without any onHostDestroy`() {

‎packages/react-native/ReactCommon/react/nativemodule/samples/platform/android/SampleTurboModule.kt‎

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -385,7 +385,8 @@ public class SampleTurboModule(private val context: ReactApplicationContext) :
385385
}
386386
pendingPickMultipleMediaPromise = promise
387387
pickMultipleMediaLauncher.launch(
388-
PickUpToMedia.Request(limit, PickVisualMediaRequest(visualMediaType(mimeType))))
388+
PickUpToMedia.Request(limit, PickVisualMediaRequest(visualMediaType(mimeType)))
389+
)
389390
}
390391

391392
/**
@@ -429,15 +430,21 @@ public class SampleTurboModule(private val context: ReactApplicationContext) :
429430
// Reject anything still in flight: the JS context that made these calls is going away.
430431
// Clearing the fields also lets the still-registered callbacks tolerate a late result.
431432
pendingPermissionPromise?.reject(
432-
"E_MODULE_INVALIDATED", "Permission request cancelled: SampleTurboModule was invalidated")
433+
"E_MODULE_INVALIDATED",
434+
"Permission request cancelled: SampleTurboModule was invalidated",
435+
)
433436
pendingPermissionPromise = null
434437

435438
pendingPickMediaPromise?.reject(
436-
"E_MODULE_INVALIDATED", "Media pick cancelled: SampleTurboModule was invalidated")
439+
"E_MODULE_INVALIDATED",
440+
"Media pick cancelled: SampleTurboModule was invalidated",
441+
)
437442
pendingPickMediaPromise = null
438443

439444
pendingPickMultipleMediaPromise?.reject(
440-
"E_MODULE_INVALIDATED", "Multiple media pick cancelled: SampleTurboModule was invalidated")
445+
"E_MODULE_INVALIDATED",
446+
"Multiple media pick cancelled: SampleTurboModule was invalidated",
447+
)
441448
pendingPickMultipleMediaPromise = null
442449
super.invalidate()
443450
}

‎packages/rn-tester/js/examples/TurboModule/SampleTurboModuleExample.js‎

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,15 @@ import type {EventSubscription, RootTag} from 'react-native';
1212

1313
import RNTesterText from '../../components/RNTesterText';
1414
import styles from './TurboModuleExampleCommon';
15+
1516
import * as React from 'react';
16-
import {FlatList, RootTagContext, TouchableOpacity, View} from 'react-native';
17+
import {
18+
FlatList,
19+
Platform,
20+
RootTagContext,
21+
TouchableOpacity,
22+
View,
23+
} from 'react-native';
1724
import NativeSampleTurboModule, {
1825
EnumInt,
1926
} from 'react-native/Libraries/TurboModule/samples/NativeSampleTurboModule';
@@ -178,15 +185,16 @@ class SampleTurboModuleExample extends React.Component<{}, State> {
178185
installJSIBindings: () => global.__SampleTurboModuleJSIBindings,
179186
};
180187

181-
// Kept out of `_tests` so that "Run all tests" does not raise a system permission dialog.
182188
// $FlowFixMe[missing-local-annot]
183189
_androidTests = {
184190
requestSamplePermission: () => {
185-
NativeSampleTurboModule.requestSamplePermission?.()
186-
.then(isGranted =>
187-
this._setResult('requestSamplePermission', isGranted),
188-
)
189-
.catch(e => this._setResult('requestSamplePermission', e.message));
191+
try {
192+
const isGranted = NativeSampleTurboModule.requestSamplePermission?.();
193+
this._setResult('requestSamplePermission', isGranted);
194+
} catch (e) {
195+
this._setResult('requestSamplePermission', e.message);
196+
return e.message;
197+
}
190198
},
191199
};
192200

@@ -326,6 +334,7 @@ class SampleTurboModuleExample extends React.Component<{}, State> {
326334
</RNTesterText>
327335
</TouchableOpacity>
328336
<View style={[styles.column]}>
337+
{/* $FlowFixMe[incompatible-type] */}
329338
{this._renderResult(item)}
330339
</View>
331340
</View>

0 commit comments

Comments
 (0)