Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[iOS] Support launchOptions in bridgeless mode #43757

Conversation

zhongwuzw
Copy link
Contributor

Summary:

Support launchOptions in bridgeless mode

Changelog:

[IOS] [FIXED] - Support launchOptions in bridgeless mode

Test Plan:

useEffect(() => {
    const processInitialURL = async () => {
      const url = await Linking.getInitialURL();
      if (url !== null) {
        console.log(`Initial url is: ${url}`);
      }
    };

    processInitialURL();
  }, []);

@facebook-github-bot facebook-github-bot added CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. Shared with Meta Applied via automation to indicate that an Issue or Pull Request has been shared with the team. labels Apr 2, 2024
Comment on lines 157 to 160
RCTBridge *bridge = self.bridge;
if (!bridge) {
bridge = [RCTBridge currentBridge];
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Modules are always scoped to a single bridge. Accessing the singleton here is incorrect.

If bridge is nil, it means this module is being destroyed and the module should be torn down.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

emm,in the bridgeless mode, bridge ivar is always nil because it is a turbo module.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, isn't this part of bridgeless compat? @cipolleschi ?

Accessing the singleton here is an anti-pattern and should be avoided.

Copy link
Contributor

@cipolleschi cipolleschi Apr 3, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree with @javache that we should not access the [RCTBridge currentBridge].

In bridgeless mode, self.bridge would be nil, and the currentBridge will return the bridgeProxy that behaves like a bridge. So, this technically works, but it was intended to be used only for backward compatibility.

However, I'd rather fix the issue properly, looking if we can find a way to remove the reference to the bridge altogether. There are only three reference to the bridge in the RCTLinkingManager: if we find a place to store the launchOptions which can work for both Old and New Architecture, that would be great.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also... this should actually works already: in bridgeless mode, this module should be created by the RCTTurboModuleManager here.

This method checks whether there exists a _bridge or a _bridgeProxy and it sets the bridge ivar with the _bridge or the _bridgeProxy object (here).

So, my guess is that the

if (!bridge) {
    bridge = [RCTBridge currentBridge];
  }

Is not necessary at all, but we need to store the launchOptions in the bridgeProxy.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@cipolleschi you can see it also check _isLegacyModuleClass of class, so only legacy module can be set _bridgeProxy in bridgeless mode.

      if (_bridge) {
        [(id)module setValue:_bridge forKey:@"bridge"];
      } else if (_bridgeProxy && [self _isLegacyModuleClass:[module class]]) {
        [(id)module setValue:_bridgeProxy forKey:@"bridge"];
      }

Copy link
Contributor

@cipolleschi cipolleschi Apr 3, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

True, but _isLegacyModuleClass checks this. I think that this should be turned on in the OSS to simplify the migration...

I think we should just remove the && _isLegacyModuleClass in that check. We are already checking that the module supports the bridge property, so it would be better to just set the bridgeProxy if it's available.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@cipolleschi Done, please review again.

Copy link
Contributor

@cipolleschi cipolleschi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is good enough to unblock us in the short term. We should still look for a way to remove the dependency RCTLinkingManager -> RCTBridge for when we are going to remove it.

@cipolleschi
Copy link
Contributor

/rebase - (this triggers an automatic rebase - build android should be green on main!)

@zhongwuzw zhongwuzw force-pushed the features/bridgeless_support_launchoptions branch from e002f9c to 37bf818 Compare April 4, 2024 14:33
@zhongwuzw zhongwuzw force-pushed the features/bridgeless_support_launchoptions branch from 37bf818 to f065614 Compare April 4, 2024 16:03
@analysis-bot
Copy link

Platform Engine Arch Size (bytes) Diff
android hermes arm64-v8a 18,918,797 +0
android hermes armeabi-v7a n/a --
android hermes x86 n/a --
android hermes x86_64 n/a --
android jsc arm64-v8a 22,283,293 -11
android jsc armeabi-v7a n/a --
android jsc x86 n/a --
android jsc x86_64 n/a --

Base commit: c1081cc
Branch: main

@facebook-github-bot
Copy link
Contributor

@cipolleschi has imported this pull request. If you are a Meta employee, you can view this diff on Phabricator.

@facebook-github-bot facebook-github-bot added the Merged This PR has been merged. label Apr 8, 2024
@facebook-github-bot
Copy link
Contributor

@cipolleschi merged this pull request in 90296be.

Copy link

github-actions bot commented Apr 8, 2024

This pull request was successfully merged by @zhongwuzw in 90296be.

When will my fix make it into a release? | How to file a pick request?

cipolleschi pushed a commit that referenced this pull request Apr 9, 2024
Summary:
Support launchOptions in bridgeless mode
bypass-github-export-checks

[IOS] [FIXED] - Support launchOptions in bridgeless mode

Pull Request resolved: #43757

Test Plan:
```
useEffect(() => {
    const processInitialURL = async () => {
      const url = await Linking.getInitialURL();
      if (url !== null) {
        console.log(`Initial url is: ${url}`);
      }
    };

    processInitialURL();
  }, []);
```

Reviewed By: javache

Differential Revision: D55790758

Pulled By: cipolleschi

fbshipit-source-id: 0f6aa6bdcebfc5bc42d632bea9193f122c1eb84f
cortinico pushed a commit that referenced this pull request Apr 10, 2024
* Support launchOptions in bridgeless mode (#43757)

Summary:
Support launchOptions in bridgeless mode
bypass-github-export-checks

[IOS] [FIXED] - Support launchOptions in bridgeless mode

Pull Request resolved: #43757

Test Plan:
```
useEffect(() => {
    const processInitialURL = async () => {
      const url = await Linking.getInitialURL();
      if (url !== null) {
        console.log(`Initial url is: ${url}`);
      }
    };

    processInitialURL();
  }, []);
```

Reviewed By: javache

Differential Revision: D55790758

Pulled By: cipolleschi

fbshipit-source-id: 0f6aa6bdcebfc5bc42d632bea9193f122c1eb84f

* Fix Connect to Metro after Reload in Bridgeless mode (#43994)

Summary:
Pull Request resolved: #43994

We received [this issue](#43764) from OSS where an app can't connect to Metro on reloads in the following scenario:

* Start the App when metro does not run.
* Observe the error screen
* Start Metro
* Press Reload
* Observe the error message again

While the desired behavior should be to connect to Metro now that this is running.

The root cause of the problem is that the RCTHost is initialized with a value of the `bundleURL` that is `nil`. Upon reload, the RCTHost is **not** recreated: the instance is restarted, but with the previous `bundleURL`, which is still `nil`.

The solution is to initialize the `RCTHost` with a closure that re-evaluate the `bundleURL` whenever it is invoked and to evaluate it only on `start`, to keep the initialization path light.
This way, when the app is started with Metro not running, the `bundleURL` is `nil`. But when it is reloaded with Metro starting, the `bundleURL` is properly initialized.

Note that the changes in this diff are not breaking as I reimplemented (and deprecated) the old initializer so that they should work in the same way.

[iOS][Fixed] - Let RCTHost be initialized with a function to provide the `bundleURL` so that it can connect to metro on Reload when the url changes.

Reviewed By: dmytrorykun

Differential Revision: D55916135

fbshipit-source-id: 6927b2154870245f28f42d26bd0209b28c9518f2

* Fix badly resolved merge  conflicts

---------

Co-authored-by: zhongwuzw <zhongwuzw@qq.com>
facebook-github-bot pushed a commit that referenced this pull request Apr 12, 2024
Summary:
We would set the value of  _bridge ivar to bridgeProxy for turbo module in bridgeless mode in #43757 , so we need to change the way of bridgeless/bridge check.

## Changelog:

[IOS] [FIXED] - Change bridgeless check in dev menu

Pull Request resolved: #43976

Test Plan: Dev menu shows bridgeless/bridge mode correctly.

Reviewed By: christophpurrer

Differential Revision: D56056640

Pulled By: cipolleschi

fbshipit-source-id: 1358c3027c1d5f12c70dd4486cc1d5975c7a185a
huntie pushed a commit that referenced this pull request Apr 15, 2024
Summary:
We would set the value of  _bridge ivar to bridgeProxy for turbo module in bridgeless mode in #43757 , so we need to change the way of bridgeless/bridge check.

## Changelog:

[IOS] [FIXED] - Change bridgeless check in dev menu

Pull Request resolved: #43976

Test Plan: Dev menu shows bridgeless/bridge mode correctly.

Reviewed By: christophpurrer

Differential Revision: D56056640

Pulled By: cipolleschi

fbshipit-source-id: 1358c3027c1d5f12c70dd4486cc1d5975c7a185a
This was referenced Jun 28, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. Merged This PR has been merged. Shared with Meta Applied via automation to indicate that an Issue or Pull Request has been shared with the team.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants