add support for multiple calls to Linking.getInitialURL (#46502)

Summary:
Pull Request resolved: https://github.com/facebook/react-native/pull/46502

changelog: [internal]

At the moment, Linking module has undocumented restriction that if you call it twice when Android Activity is not available, it will return with an error.

This is unnecessary and can be handled more sensibly.

Reviewed By: javache, yungsters

Differential Revision: D62708123

fbshipit-source-id: 79a6beb70e834e631f7bedaf6a64076b050a7daa
This commit is contained in:
Samuel Susla
2024-09-16 06:45:53 -07:00
committed by Facebook GitHub Bot
parent b63ec30614
commit 1ac3b890d8
@@ -24,12 +24,15 @@ import com.facebook.react.bridge.ReadableArray;
import com.facebook.react.bridge.ReadableMap;
import com.facebook.react.bridge.ReadableType;
import com.facebook.react.module.annotations.ReactModule;
import java.util.ArrayList;
import java.util.List;
/** Intent module. Launch other activities or open URLs. */
@ReactModule(name = NativeIntentAndroidSpec.NAME)
public class IntentModule extends NativeIntentAndroidSpec {
private @Nullable LifecycleEventListener mInitialURLListener = null;
private final List<Promise> mPendingOpenURLPromises = new ArrayList<>();
private static final String EXTRA_MAP_KEY_FOR_VALUE = "value";
@@ -40,6 +43,7 @@ public class IntentModule extends NativeIntentAndroidSpec {
@Override
public void invalidate() {
synchronized (this) {
mPendingOpenURLPromises.clear();
if (mInitialURLListener != null) {
getReactApplicationContext().removeLifecycleEventListener(mInitialURLListener);
mInitialURLListener = null;
@@ -82,10 +86,8 @@ public class IntentModule extends NativeIntentAndroidSpec {
}
private synchronized void waitForActivityAndGetInitialURL(final Promise promise) {
mPendingOpenURLPromises.add(promise);
if (mInitialURLListener != null) {
promise.reject(
new IllegalStateException(
"Cannot await activity from more than one call to getInitialURL"));
return;
}
@@ -93,11 +95,14 @@ public class IntentModule extends NativeIntentAndroidSpec {
new LifecycleEventListener() {
@Override
public void onHostResume() {
getInitialURL(promise);
getReactApplicationContext().removeLifecycleEventListener(this);
synchronized (IntentModule.this) {
for (Promise promise : mPendingOpenURLPromises) {
getInitialURL(promise);
}
mInitialURLListener = null;
mPendingOpenURLPromises.clear();
}
}