-
Notifications
You must be signed in to change notification settings - Fork 24.3k
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
Add EventEmitter code-gen support for C++ Turbo Modules #44809
Conversation
This pull request was exported from Phabricator. Differential Revision: D57407871 |
Base commit: ed9978b |
…cebook#44809) Summary: Adding react-native-codegen parser support for a new `EventEmitter` property type on C++ Turbo Modules. It is possible to later expand this feature to other languages (Java, ObjC). ## Characteristics An `EventEmitter` must: - be non null: `EventEmitter<string>` works, `?EventEmitter<string>` does NOT - have a non null eventType: `EventEmitter<number>` works, `EventEmitter<?number>` does NOT - have at most 1 eventType, `void` is possible as well: `EventEmitter<>` or `EventEmitter<MyObject>` work - `EventEmitter<number, string>` do NOT - have a concrete eventType, `{}` is not allowed `EventEmitter<{}>` does NOT work - be used in `Cxx` Turbo Modules only at this time ## Example For these 4 eventEmitters in on an RN JS TM spec ``` +onPress: EventEmitter<void>; +onClick: EventEmitter<string>; +onChange: EventEmitter<ObjectStruct>; +onSubmit: EventEmitter<ObjectStruct[]>; ``` We now generate this code: 1.) in the spec based header `{MyModuleName}CxxSpec` in the constructor: ``` ... // existing code eventEmitterMap_["onPress"] = std::make_shared<AsyncEventEmitter<>>(); eventEmitterMap_["onClick"] = std::make_shared<AsyncEventEmitter<OnClickType>>(); eventEmitterMap_["onChange"] = std::make_shared<AsyncEventEmitter<OnChangeType>>(); eventEmitterMap_["onSubmit"] = std::make_shared<AsyncEventEmitter<OnSubmitType>>(); ``` 2.) as `protected` functions ``` void emitOnPress() { std::static_pointer_cast<AsyncEventEmitter<>>(delegate_.eventEmitterMap_["onPress"])->emit(); } void emitOnClick(const OnClickType& value) { std::static_pointer_cast<AsyncEventEmitter<OnClickType>>(delegate_.eventEmitterMap_["onClick"])->emit(value); } void emitOnChange(const OnChangeType& value) { std::static_pointer_cast<AsyncEventEmitter<OnChangeType>>(delegate_.eventEmitterMap_["onChange"])->emit(value); } void emitOnSubmit(const OnSubmitType& value) { std::static_pointer_cast<AsyncEventEmitter<OnSubmitType>>(delegate_.eventEmitterMap_["onSubmit"])->emit(value); } ``` ## Changelog: [General] [Added] - Add EventEmitter code-gen support for C++ Turbo Modules Differential Revision: D57407871
Summary: Adding react-native-codegen parser support for a new `EventEmitter` property type on C++ Turbo Modules. It is possible to later expand this feature to other languages (Java, ObjC). ## Characteristics An `EventEmitter` must: - be non null: `EventEmitter<string>` works, `?EventEmitter<string>` does NOT - have a non null eventType: `EventEmitter<number>` works, `EventEmitter<?number>` does NOT - have at most 1 eventType, `void` is possible as well: `EventEmitter<>` or `EventEmitter<MyObject>` work - `EventEmitter<number, string>` do NOT - have a concrete eventType, `{}` is not allowed `EventEmitter<{}>` does NOT work - be used in `Cxx` Turbo Modules only at this time ## Example For these 4 eventEmitters in on an RN JS TM spec ``` +onPress: EventEmitter<void>; +onClick: EventEmitter<string>; +onChange: EventEmitter<ObjectStruct>; +onSubmit: EventEmitter<ObjectStruct[]>; ``` We now generate this code: 1.) in the spec based header `{MyModuleName}CxxSpec` in the constructor: ``` ... // existing code eventEmitterMap_["onPress"] = std::make_shared<AsyncEventEmitter<>>(); eventEmitterMap_["onClick"] = std::make_shared<AsyncEventEmitter<OnClickType>>(); eventEmitterMap_["onChange"] = std::make_shared<AsyncEventEmitter<OnChangeType>>(); eventEmitterMap_["onSubmit"] = std::make_shared<AsyncEventEmitter<OnSubmitType>>(); ``` 2.) as `protected` functions ``` void emitOnPress() { std::static_pointer_cast<AsyncEventEmitter<>>(delegate_.eventEmitterMap_["onPress"])->emit(); } void emitOnClick(const OnClickType& value) { std::static_pointer_cast<AsyncEventEmitter<OnClickType>>(delegate_.eventEmitterMap_["onClick"])->emit(value); } void emitOnChange(const OnChangeType& value) { std::static_pointer_cast<AsyncEventEmitter<OnChangeType>>(delegate_.eventEmitterMap_["onChange"])->emit(value); } void emitOnSubmit(const OnSubmitType& value) { std::static_pointer_cast<AsyncEventEmitter<OnSubmitType>>(delegate_.eventEmitterMap_["onSubmit"])->emit(value); } ``` ## Changelog: [General] [Added] - Add EventEmitter code-gen support for C++ Turbo Modules Differential Revision: D57407871
dda9a6e
to
cb38aa5
Compare
This pull request was exported from Phabricator. Differential Revision: D57407871 |
Summary: Adds an `AsyncEventEmitter` class which can be used as a property of currently C++ only Turbo Modules to send type safe data back to JavaScript. Adding support for ObjC / Java Turbo Modules is possible, straight forward and can be added as an afterthought. It implements this interface ``` export type EventEmitter<T> = { addListener(handler: (T) => mixed): EventSubscription, }; ``` ## Hybrid It is a 'hybrid' object. 1.) You `addListener(handler: (T) => mixed)` in JavaScript for emitted events (coming from C++, native code) 2.) You `emit(...Arg)` events in C++, native code (getting sent to JavaScript) ## Changelog: [General] [Added] - Add EventEmitter C++ bridging type ## Facebook: Apps usually create custom functionality to achieve this kind of behavior - e.g. https://www.internalfb.com/code/fbsource/[e72bd42a028a]/arvr/js/apps/RemoteDesktopCompanion/shared/turbo_modules/TMSubscription.h Differential Revision: D57424391
Summary: Adding react-native-codegen parser support for a new `EventEmitter` property type on C++ Turbo Modules. It is possible to later expand this feature to other languages (Java, ObjC). ## Characteristics An `EventEmitter` must: - be non null: `EventEmitter<string>` works, `?EventEmitter<string>` does NOT - have a non null eventType: `EventEmitter<number>` works, `EventEmitter<?number>` does NOT - have at most 1 eventType, `void` is possible as well: `EventEmitter<>` or `EventEmitter<MyObject>` work - `EventEmitter<number, string>` do NOT - have a concrete eventType, `{}` is not allowed `EventEmitter<{}>` does NOT work - be used in `Cxx` Turbo Modules only at this time ## Example For these 4 eventEmitters in on an RN JS TM spec ``` +onPress: EventEmitter<void>; +onClick: EventEmitter<string>; +onChange: EventEmitter<ObjectStruct>; +onSubmit: EventEmitter<ObjectStruct[]>; ``` We now generate this code: 1.) in the spec based header `{MyModuleName}CxxSpec` in the constructor: ``` ... // existing code eventEmitterMap_["onPress"] = std::make_shared<AsyncEventEmitter<>>(); eventEmitterMap_["onClick"] = std::make_shared<AsyncEventEmitter<OnClickType>>(); eventEmitterMap_["onChange"] = std::make_shared<AsyncEventEmitter<OnChangeType>>(); eventEmitterMap_["onSubmit"] = std::make_shared<AsyncEventEmitter<OnSubmitType>>(); ``` 2.) as `protected` functions ``` void emitOnPress() { std::static_pointer_cast<AsyncEventEmitter<>>(delegate_.eventEmitterMap_["onPress"])->emit(); } void emitOnClick(const OnClickType& value) { std::static_pointer_cast<AsyncEventEmitter<OnClickType>>(delegate_.eventEmitterMap_["onClick"])->emit(value); } void emitOnChange(const OnChangeType& value) { std::static_pointer_cast<AsyncEventEmitter<OnChangeType>>(delegate_.eventEmitterMap_["onChange"])->emit(value); } void emitOnSubmit(const OnSubmitType& value) { std::static_pointer_cast<AsyncEventEmitter<OnSubmitType>>(delegate_.eventEmitterMap_["onSubmit"])->emit(value); } ``` ## Changelog: [General] [Added] - Add EventEmitter code-gen support for C++ Turbo Modules Differential Revision: D57407871
cb38aa5
to
e69324d
Compare
This pull request was exported from Phabricator. Differential Revision: D57407871 |
Summary: Adding react-native-codegen parser support for a new `EventEmitter` property type on C++ Turbo Modules. It is possible to later expand this feature to other languages (Java, ObjC). ## Characteristics An `EventEmitter` must: - be non null: `EventEmitter<string>` works, `?EventEmitter<string>` does NOT - have a non null eventType: `EventEmitter<number>` works, `EventEmitter<?number>` does NOT - have at most 1 eventType, `void` is possible as well: `EventEmitter<>` or `EventEmitter<MyObject>` work - `EventEmitter<number, string>` do NOT - have a concrete eventType, `{}` is not allowed `EventEmitter<{}>` does NOT work - be used in `Cxx` Turbo Modules only at this time ## Example For these 4 eventEmitters in on an RN JS TM spec ``` +onPress: EventEmitter<void>; +onClick: EventEmitter<string>; +onChange: EventEmitter<ObjectStruct>; +onSubmit: EventEmitter<ObjectStruct[]>; ``` We now generate this code: 1.) in the spec based header `{MyModuleName}CxxSpec` in the constructor: ``` ... // existing code eventEmitterMap_["onPress"] = std::make_shared<AsyncEventEmitter<>>(); eventEmitterMap_["onClick"] = std::make_shared<AsyncEventEmitter<OnClickType>>(); eventEmitterMap_["onChange"] = std::make_shared<AsyncEventEmitter<OnChangeType>>(); eventEmitterMap_["onSubmit"] = std::make_shared<AsyncEventEmitter<OnSubmitType>>(); ``` 2.) as `protected` functions ``` void emitOnPress() { std::static_pointer_cast<AsyncEventEmitter<>>(delegate_.eventEmitterMap_["onPress"])->emit(); } void emitOnClick(const OnClickType& value) { std::static_pointer_cast<AsyncEventEmitter<OnClickType>>(delegate_.eventEmitterMap_["onClick"])->emit(value); } void emitOnChange(const OnChangeType& value) { std::static_pointer_cast<AsyncEventEmitter<OnChangeType>>(delegate_.eventEmitterMap_["onChange"])->emit(value); } void emitOnSubmit(const OnSubmitType& value) { std::static_pointer_cast<AsyncEventEmitter<OnSubmitType>>(delegate_.eventEmitterMap_["onSubmit"])->emit(value); } ``` ## Changelog: [General] [Added] - Add EventEmitter code-gen support for C++ Turbo Modules Differential Revision: D57407871
This pull request has been merged in fd61881. |
This pull request was successfully merged by @christophpurrer in fd61881. When will my fix make it into a release? | How to file a pick request? |
Summary:
Adding react-native-codegen parser support for a new
EventEmitter
property type on C++ Turbo Modules.It is possible to later expand this feature to other languages (Java, ObjC).
Characteristics
An
EventEmitter
must:EventEmitter<string>
works,?EventEmitter<string>
does NOTEventEmitter<number>
works,EventEmitter<?number>
does NOTvoid
is possible as well:EventEmitter<>
orEventEmitter<MyObject>
work -EventEmitter<number, string>
do NOT{}
is not allowedEventEmitter<{}>
does NOT workCxx
Turbo Modules only at this timeExample
For these 4 eventEmitters in on an RN JS TM spec
We now generate this code:
1.) in the spec based header
{MyModuleName}CxxSpec
in the constructor:2.) as
protected
functionsChangelog:
[General] [Added] - Add EventEmitter code-gen support for C++ Turbo Modules
Differential Revision: D57407871