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] Fabric: Post RCTInstanceDidLoadBundle notification after bundle loaded #48082

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

zhongwuzw
Copy link
Contributor

Summary:

Fixes #47949

Changelog:

[IOS] [FIXED] - Fabric: Post RCTInstanceDidLoadBundle notification after bundle loaded

Test Plan:

Post RCTInstanceDidLoadBundle notification after bundle loaded

@facebook-github-bot facebook-github-bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Dec 4, 2024
@facebook-github-bot facebook-github-bot added the Shared with Meta Applied via automation to indicate that an Issue or Pull Request has been shared with the team. label Dec 4, 2024
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.

Thanks for looking into this, it was next o my todo list! xD

I left a couple of suggestion to improve the solution.

@@ -476,7 +476,11 @@ - (void)_loadScriptFromSource:(RCTSource *)source
auto script = std::make_unique<NSDataBigString>(source.data);
const auto *url = deriveSourceURL(source.url).UTF8String;
_reactInstance->loadScript(std::move(script), url);
[[NSNotificationCenter defaultCenter] postNotificationName:@"RCTInstanceDidLoadBundle" object:nil];
[self callFunctionOnBufferedRuntimeExecutor:[](facebook::jsi::Runtime &_) {
Copy link
Contributor

Choose a reason for hiding this comment

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

This jumps to a different thread and then we jumps back to the original one. I'm not super excited about this: what do you think about changing the ReactInstance::loadScript method to take an extra, optional parameter (nullptr as default value) which is a callback and invoke the callback when loadScript finishes?

It should prevent us a thread jump and ensure that we are actually posting the notification when the script has been loaded.

We can check here if the callback is set and invoke it.

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.

@@ -476,7 +476,11 @@ - (void)_loadScriptFromSource:(RCTSource *)source
auto script = std::make_unique<NSDataBigString>(source.data);
const auto *url = deriveSourceURL(source.url).UTF8String;
_reactInstance->loadScript(std::move(script), url);
[[NSNotificationCenter defaultCenter] postNotificationName:@"RCTInstanceDidLoadBundle" object:nil];
[self callFunctionOnBufferedRuntimeExecutor:[](facebook::jsi::Runtime &_) {
dispatch_async(dispatch_get_main_queue(), ^{
Copy link
Contributor

Choose a reason for hiding this comment

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

why are we jumping back on the UI thread? I don't think that it is necessary: we should be able to post notification from a background thread.

What happens if we dispatch the notification from a different thread?

Copy link
Contributor Author

@zhongwuzw zhongwuzw Dec 4, 2024

Choose a reason for hiding this comment

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

@cipolleschi I just keep the same logic as old arch, please see

dispatch_async(dispatch_get_main_queue(), ^{
. Seems it may have issue when post on Javascript thread directly, not sure fabric wether has potential issue #35556 .

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.

Thanks for applying the feedback!

@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.

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. 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.

The RCTInstanceDidLoadBundle notification is sent before loadScript completes loading the JSBundle
3 participants