Skip to content

Conversation

@jonthysell
Copy link
Contributor

@jonthysell jonthysell commented Aug 2, 2024

Description

This PR provides a proper implementation of the SampleTurboModule module, with the new EventEmitter events (coming in #13508) and removes the proxy code in TurboModuleManager which instead substituted the old SampleTurboCxxModule module.

Type of Change

  • New feature (non-breaking change which adds functionality)

Why

The APIs of SampleTurboModule are starting to deviate from the older SampleTurboCxxModule, specifically the addtion of new EventEmitter members. So it's time we had a implementation for both.

Closes #13531
Partial implementation of #13532

What

What changes were made to the codebase to solve the bug, add the functionality, etc. that you specified above.

Screenshots

Add any relevant screen captures here from before or after your changes.

Testing

Verified tests still pass and the new module is being called.

Changelog

Should this change be included in the release notes: yes

Implement the SampleTurboModule module

Microsoft Reviewers: Open in CodeFlow

@microsoft-github-policy-service microsoft-github-policy-service bot added Area: Turbo Modules New Architecture Broad category for issues that apply to the RN "new" architecture of Turbo Modules + Fabric labels Aug 2, 2024
This PR provides a proper implementation of the `SampleTurboModule`
module, and removes the proxy code in `TurboModuleManager` which instead
substituted the old `SampleTurboCxxModule` module.

Closes microsoft#13531
@jonthysell jonthysell marked this pull request as ready for review August 2, 2024 22:27
@jonthysell jonthysell requested a review from a team as a code owner August 2, 2024 22:27
@acoates-ms acoates-ms requested a review from a team as a code owner August 2, 2024 23:00
@jonthysell jonthysell changed the title Implement the SampleTurboModule module Implement REACT_EVENT_EMITTER and add to the SampleTurboModule module Aug 2, 2024
#define REACT_EVENT(/* field, [opt] eventName, [opt] eventEmitterName */...) \
INTERNAL_REACT_MEMBER(__VA_ARGS__)(EventField, __VA_ARGS__)

// REACT_EVENT_EMITTER(field, [opt] eventName, [opt] eventEmitterName)
Copy link
Member

Choose a reason for hiding this comment

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

eventName

Do we have the event name or not? I see it is not documented below.

auto eventEmitterInitializer = ModuleEventEmitterFieldInfo<TField>::GetEventEmitterInitializer(m_module, field);

m_moduleBuilder.AddInitializer(eventHandlerEmptyInitializer);
m_moduleBuilder.AddEventEmitter(eventName, eventEmitterInitializer);
Copy link
Member

Choose a reason for hiding this comment

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

m_moduleBuilder

We must not have two calls here. There must be only one of them.

m_methods.push_back(std::move(cxxMethod));
}

void ReactModuleBuilder::AddEventEmitter(hstring const &name, EventEmitterInitializerDelegate const &emitter) noexcept {
Copy link
Member

Choose a reason for hiding this comment

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

AddEventEmitter

Why do we need this method then? See the event implementation - we only need the AddInitializer call.

Copy link
Contributor

Choose a reason for hiding this comment

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

Event emitters are only supported on TurboModules. The ReactModuleBuilder is used when the module is running as a native module. We could choose to throw and not allow the module to run at all if it's being run as a NativeModule. I was leaning towards allowing at least the rest of the module to continue to work if its run in that mode. But I'd be ok making this throw.

Copy link
Member

@vmoroz vmoroz left a comment

Choose a reason for hiding this comment

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

Looking through the code it is not clear how the new code works.
Can we simply reuse the existing events?
If not then why the new EventEmitters cannot be implemented the same way as the Events.
Currently I see that the EventEmitter registration calls AddInitializer and the new AddEventEmitter. Both of them are no-op.

@microsoft-github-policy-service microsoft-github-policy-service bot added the Needs: Author Feedback The issue/PR needs activity from its author (label drives bot activity) label Aug 5, 2024
@acoates-ms
Copy link
Contributor

Looking through the code it is not clear how the new code works. Can we simply reuse the existing events? If not then why the new EventEmitters cannot be implemented the same way as the Events. Currently I see that the EventEmitter registration calls AddInitializer and the new AddEventEmitter. Both of them are no-op.

No, the new code works differently than the old events. To listen to the old events you would do something like:

let subscription = RCTDeviceEventEmitter.addListener(<EVENTNAME>, handler);

where has to be unique across all native modules.

In the new world you do:

import MyModule from 'NativeMyModule'
let subscription = MyModule.<EVENTNAME>(handler);

The AddInitializer call sets the native event std::function to a no-op function. Since if no-one has registered for the event, the std::function can be a no-op.
The AddEventEmitter call sets a callback which the TurboModuleProvider then uses to generate the JS function lazily by creating a HostObject implementation of EventEmitter, and set the std::function to a function that will cause the Host Object EventEmitter to emit the event.

@jonthysell
Copy link
Contributor Author

After syncing with @vmoroz, closing this PR in preference to #13541. We can figure out how to implement EventEmitters in a different PR.

@jonthysell jonthysell closed this Aug 6, 2024
@microsoft-github-policy-service microsoft-github-policy-service bot added Needs: Attention 👋 An issue that had been tagged "Needs: Author Feedback" has received activity (label applied by bot) and removed Needs: Author Feedback The issue/PR needs activity from its author (label drives bot activity) labels Aug 6, 2024
@jonthysell jonthysell removed the Needs: Attention 👋 An issue that had been tagged "Needs: Author Feedback" has received activity (label applied by bot) label Aug 12, 2024
@jonthysell jonthysell deleted the sampleturbomodule branch May 19, 2025 21:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Area: Turbo Modules New Architecture Broad category for issues that apply to the RN "new" architecture of Turbo Modules + Fabric

Projects

Status: No status

Development

Successfully merging this pull request may close these issues.

Implement the SampleTurboModule module

3 participants