Skip to content

Conversation

@hannojg
Copy link
Contributor

@hannojg hannojg commented Dec 13, 2024

Summary:

We're trying to pass jsi::Values directly to our view components (and convert them to java/swift types manually). That way we can pass "complex" objects to our views (such as jsi::Objects with NativeState attached, without the need to convert them to e.g. folly::dynamic).

On android we store our complex prop values on the StateWrapperImpl to pass the complex types between c++ and java/kotlin. See an example here:

https://github.com/hannojg/nitro/blob/2378fe7754294c496b2cbcd62f7109529e276427/packages/react-native-nitro-image/nitrogen/generated/android/c%2B%2B/JValueFromStateWrapper.cpp#L21-L23

  const auto& customStateData = dynamic_cast<const ConcreteState<CustomStateData>&>(state);
  CustomStateData data = customStateData.getData();
  std::shared_ptr<HybridTestObjectSwiftKotlinSpec> nativeProp = data.nativeProp;

(And then it might be used in java like this:)
https://github.com/hannojg/nitro/blob/2378fe7754294c496b2cbcd62f7109529e276427/packages/react-native-nitro-image/android/src/main/java/com/margelo/nitro/image/NitroExampleViewManager.java#L31-L38

public Object updateState(@NonNull View view, ReactStylesDiffMap props, StateWrapper stateWrapper) {
   StateWrapperImpl stateWrapperImpl = (StateWrapperImpl) stateWrapper;

    HybridTestObjectSwiftKotlinSpec nativeProp = ValueFromStateWrapper.valueFromStateWrapper(stateWrapperImpl);
    long value = nativeProp.getBigintValue();
    Log.d("NitroExampleViewManager", "Value from state: " + value);

For that we need to be able to access the underlying state, which is what we added in this PR.

Changelog:

[ANDROID] [ADDED] - Added getState method for StateWrapperImpl

Test Plan:

Internal change, just make sure all tests are passing

@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 Dec 13, 2024
jni::local_ref<ReadableNativeMap::jhybridobject> getStateDataImpl();
void updateStateImpl(NativeMap* map);
void setState(std::shared_ptr<const State> state);
const std::shared_ptr<const State> getState() const;
Copy link
Member

Choose a reason for hiding this comment

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

The first const doesn't add any value

Suggested change
const std::shared_ptr<const State> getState() const;
std::shared_ptr<const State> getState() const;

Copy link
Contributor Author

Choose a reason for hiding this comment

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

On a second thought, I think returning a const ref might be a better option here to avoid an unnecessary copy?

Copy link
Member

Choose a reason for hiding this comment

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

Yes, that's fine too. Although you do have to make sure you don't hold on to the shared_ptr beyond the lifecycle of the StateWrapperImpl then.

hannojg and others added 2 commits December 13, 2024 12:44
…tateWrapperImpl.h

Co-authored-by: Pieter De Baets <pieter.debaets@gmail.com>
@hannojg hannojg requested a review from javache December 13, 2024 11:46
@facebook-github-bot
Copy link
Contributor

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

jni::local_ref<ReadableNativeMap::jhybridobject> getStateDataImpl();
void updateStateImpl(NativeMap* map);
void setState(std::shared_ptr<const State> state);
const std::shared_ptr<const State>& getState() const;
Copy link
Contributor

Choose a reason for hiding this comment

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

The state returned here is mutable, so returning a reference here seems a bit dangerous. We're making some assumptions about the fact that the caller will manage the lifecycle correctly. Maybe it'd be better to just return a copy of the shared pointer.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

alright, agree - let me change!

@hannojg hannojg requested a review from rubennorte December 13, 2024 15:47
@facebook-github-bot
Copy link
Contributor

@javache 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 Dec 13, 2024
@facebook-github-bot
Copy link
Contributor

@javache merged this pull request in ed36e89.

@react-native-bot
Copy link
Collaborator

This pull request was successfully merged by @hannojg in ed36e89

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

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