mirror of
https://github.com/facebook/react-native.git
synced 2025-11-01 09:14:26 +00:00
Move event structs into event emitter namespace
Summary: In codegen we generate structs that represents events. These structs are later dispatched by generated `EventEmitter`. They had unpleasant naming, for example `SliderOnValueChangeStruct`. This diff changes the code generated so it becomes `SliderEventEmitter::OnValueChange`, this better expresses the relationship of the two classes. Changelog: [Internal] Motivation: Better express relationship between EventEmitter and classes that represent events. Reviewed By: rickhanlonii, shergin Differential Revision: D19373850 fbshipit-source-id: a5eea085013dbc119169e2b06ba9f9fe44c7fcd9
This commit is contained in:
committed by
Facebook Github Bot
parent
f15b80b675
commit
6bdfd84a45
@@ -85,10 +85,12 @@ static UIModalPresentationStyle presentationConfiguration(ModalHostViewProps con
|
||||
}
|
||||
}
|
||||
|
||||
static ModalHostViewOnOrientationChangeStruct onOrientationChangeStruct(CGRect rect)
|
||||
static ModalHostViewEventEmitter::OnOrientationChange onOrientationChangeStruct(CGRect rect)
|
||||
{
|
||||
auto orientation = rect.size.width < rect.size.height ? ModalHostViewOnOrientationChangeOrientationStruct::Portrait
|
||||
: ModalHostViewOnOrientationChangeOrientationStruct::Landscape;
|
||||
;
|
||||
auto orientation = rect.size.width < rect.size.height
|
||||
? ModalHostViewEventEmitter::OnOrientationChangeOrientation::Portrait
|
||||
: ModalHostViewEventEmitter::OnOrientationChangeOrientation::Landscape;
|
||||
return {orientation};
|
||||
}
|
||||
|
||||
@@ -136,7 +138,7 @@ static ModalHostViewOnOrientationChangeStruct onOrientationChangeStruct(CGRect r
|
||||
|
||||
assert(std::dynamic_pointer_cast<ModalHostViewEventEmitter const>(self->_eventEmitter));
|
||||
auto eventEmitter = std::static_pointer_cast<ModalHostViewEventEmitter const>(self->_eventEmitter);
|
||||
eventEmitter->onShow(ModalHostViewOnShowStruct{});
|
||||
eventEmitter->onShow(ModalHostViewEventEmitter::OnShow{});
|
||||
}];
|
||||
}
|
||||
|
||||
|
||||
@@ -309,11 +309,11 @@ using namespace facebook::react;
|
||||
|
||||
if (continuous && _previousValue != value) {
|
||||
std::dynamic_pointer_cast<const SliderEventEmitter>(_eventEmitter)
|
||||
->onValueChange(SliderOnValueChangeStruct{.value = static_cast<Float>(value)});
|
||||
->onValueChange(SliderEventEmitter::OnValueChange{.value = static_cast<Float>(value)});
|
||||
}
|
||||
if (!continuous) {
|
||||
std::dynamic_pointer_cast<const SliderEventEmitter>(_eventEmitter)
|
||||
->onSlidingComplete(SliderOnSlidingCompleteStruct{.value = static_cast<Float>(value)});
|
||||
->onSlidingComplete(SliderEventEmitter::OnSlidingComplete{.value = static_cast<Float>(value)});
|
||||
}
|
||||
|
||||
_previousValue = value;
|
||||
|
||||
@@ -99,7 +99,7 @@ using namespace facebook::react;
|
||||
}
|
||||
|
||||
std::dynamic_pointer_cast<const SwitchEventEmitter>(_eventEmitter)
|
||||
->onChange(SwitchOnChangeStruct{.value = static_cast<bool>(sender.on)});
|
||||
->onChange(SwitchEventEmitter::OnChange{.value = static_cast<bool>(sender.on)});
|
||||
}
|
||||
|
||||
#pragma mark - Native Commands
|
||||
|
||||
@@ -98,6 +98,11 @@ function getImports(properties: $ReadOnlyArray<PropTypeShape>): Set<string> {
|
||||
return imports;
|
||||
}
|
||||
|
||||
function generateEventStructName(parts: $ReadOnlyArray<string> = []): string {
|
||||
const additional = parts.map(toSafeCppString).join('');
|
||||
return `${additional}`;
|
||||
}
|
||||
|
||||
function generateStructName(
|
||||
componentName: string,
|
||||
parts: $ReadOnlyArray<string> = [],
|
||||
@@ -206,4 +211,5 @@ module.exports = {
|
||||
toSafeCppString,
|
||||
toIntEnumValueName,
|
||||
generateStructName,
|
||||
generateEventStructName,
|
||||
};
|
||||
|
||||
+2
-5
@@ -10,7 +10,7 @@
|
||||
|
||||
'use strict';
|
||||
|
||||
const {generateStructName} = require('./CppHelpers.js');
|
||||
const {generateEventStructName} = require('./CppHelpers.js');
|
||||
|
||||
import type {
|
||||
ComponentShape,
|
||||
@@ -151,10 +151,7 @@ function generateEvent(componentName: string, event): string {
|
||||
.replace(/::_CLASSNAME_::/g, componentName)
|
||||
.replace(/::_EVENT_NAME_::/g, event.name)
|
||||
.replace(/::_DISPATCH_EVENT_NAME_::/g, dispatchEventName)
|
||||
.replace(
|
||||
'::_STRUCT_NAME_::',
|
||||
generateStructName(componentName, [event.name]),
|
||||
)
|
||||
.replace('::_STRUCT_NAME_::', generateEventStructName([event.name]))
|
||||
.replace('::_IMPLEMENTATION_::', implementation);
|
||||
}
|
||||
|
||||
|
||||
+30
-27
@@ -15,7 +15,7 @@ const nullthrows = require('nullthrows');
|
||||
const {
|
||||
getCppTypeForAnnotation,
|
||||
toSafeCppString,
|
||||
generateStructName,
|
||||
generateEventStructName,
|
||||
} = require('./CppHelpers.js');
|
||||
|
||||
import type {
|
||||
@@ -55,31 +55,45 @@ namespace react {
|
||||
`;
|
||||
|
||||
const componentTemplate = `
|
||||
::_STRUCTS_::
|
||||
|
||||
class ::_CLASSNAME_::EventEmitter : public ViewEventEmitter {
|
||||
public:
|
||||
using ViewEventEmitter::ViewEventEmitter;
|
||||
|
||||
::_STRUCTS_::
|
||||
|
||||
::_EVENTS_::
|
||||
};
|
||||
`.trim();
|
||||
|
||||
const structTemplate = `
|
||||
struct ::_STRUCT_NAME_:: {
|
||||
::_FIELDS_::
|
||||
};
|
||||
struct ::_STRUCT_NAME_:: {
|
||||
::_FIELDS_::
|
||||
};
|
||||
`.trim();
|
||||
|
||||
const enumTemplate = `enum class ::_ENUM_NAME_:: {
|
||||
::_VALUES_::
|
||||
};
|
||||
|
||||
inline char const *toString(const ::_ENUM_NAME_:: value) {
|
||||
static char const *toString(const ::_ENUM_NAME_:: value) {
|
||||
switch (value) {
|
||||
::_TO_CASES_::
|
||||
}
|
||||
}`.trim();
|
||||
}
|
||||
`.trim();
|
||||
|
||||
function indent(nice: string, spaces: number) {
|
||||
return nice
|
||||
.split('\n')
|
||||
.map((line, index) => {
|
||||
if (line.length === 0 || index === 0) {
|
||||
return line;
|
||||
}
|
||||
const emptySpaces = new Array(spaces + 1).join(' ');
|
||||
return emptySpaces + line;
|
||||
})
|
||||
.join('\n');
|
||||
}
|
||||
|
||||
function getNativeTypeFromAnnotation(
|
||||
componentName: string,
|
||||
@@ -96,22 +110,16 @@ function getNativeTypeFromAnnotation(
|
||||
case 'FloatTypeAnnotation':
|
||||
return getCppTypeForAnnotation(type);
|
||||
case 'StringEnumTypeAnnotation':
|
||||
return generateStructName(
|
||||
componentName,
|
||||
nameParts.concat([eventProperty.name]),
|
||||
);
|
||||
return generateEventStructName(nameParts.concat([eventProperty.name]));
|
||||
case 'ObjectTypeAnnotation':
|
||||
return generateStructName(
|
||||
componentName,
|
||||
nameParts.concat([eventProperty.name]),
|
||||
);
|
||||
return generateEventStructName(nameParts.concat([eventProperty.name]));
|
||||
default:
|
||||
(type: empty);
|
||||
throw new Error(`Received invalid event property type ${type}`);
|
||||
}
|
||||
}
|
||||
function generateEnum(structs, componentName, options, nameParts) {
|
||||
const structName = generateStructName(componentName, nameParts);
|
||||
function generateEnum(structs, options, nameParts) {
|
||||
const structName = generateEventStructName(nameParts);
|
||||
const fields = options
|
||||
.map((option, index) => `${toSafeCppString(option.name)}`)
|
||||
.join(',\n ');
|
||||
@@ -141,7 +149,7 @@ function generateStruct(
|
||||
properties: $ReadOnlyArray<ObjectPropertyType>,
|
||||
): void {
|
||||
const structNameParts = nameParts;
|
||||
const structName = generateStructName(componentName, structNameParts);
|
||||
const structName = generateEventStructName(structNameParts);
|
||||
|
||||
const fields = properties
|
||||
.map(property => {
|
||||
@@ -171,12 +179,7 @@ function generateStruct(
|
||||
);
|
||||
return;
|
||||
case 'StringEnumTypeAnnotation':
|
||||
generateEnum(
|
||||
structs,
|
||||
componentName,
|
||||
property.options,
|
||||
nameParts.concat([name]),
|
||||
);
|
||||
generateEnum(structs, property.options, nameParts.concat([name]));
|
||||
return;
|
||||
default:
|
||||
(property: empty);
|
||||
@@ -213,7 +216,7 @@ function generateStructs(componentName: string, component): string {
|
||||
|
||||
function generateEvent(componentName: string, event: EventTypeShape): string {
|
||||
if (event.typeAnnotation.argument) {
|
||||
const structName = generateStructName(componentName, [event.name]);
|
||||
const structName = generateEventStructName([event.name]);
|
||||
|
||||
return `void ${event.name}(${structName} value) const;`;
|
||||
}
|
||||
@@ -261,7 +264,7 @@ module.exports = {
|
||||
.replace(/::_CLASSNAME_::/g, componentName)
|
||||
.replace(
|
||||
'::_STRUCTS_::',
|
||||
generateStructs(componentName, component),
|
||||
indent(generateStructs(componentName, component), 2),
|
||||
)
|
||||
.replace(
|
||||
'::_EVENTS_::',
|
||||
|
||||
+7
-7
@@ -176,7 +176,7 @@ Map {
|
||||
namespace facebook {
|
||||
namespace react {
|
||||
|
||||
void EventsNestedObjectNativeComponentEventEmitter::onChange(EventsNestedObjectNativeComponentOnChangeStruct event) const {
|
||||
void EventsNestedObjectNativeComponentEventEmitter::onChange(OnChange event) const {
|
||||
dispatchEvent(\\"change\\", [event=std::move(event)](jsi::Runtime &runtime) {
|
||||
auto payload = jsi::Object(runtime);
|
||||
{
|
||||
@@ -217,7 +217,7 @@ Map {
|
||||
namespace facebook {
|
||||
namespace react {
|
||||
|
||||
void EventsNativeComponentEventEmitter::onChange(EventsNativeComponentOnChangeStruct event) const {
|
||||
void EventsNativeComponentEventEmitter::onChange(OnChange event) const {
|
||||
dispatchEvent(\\"change\\", [event=std::move(event)](jsi::Runtime &runtime) {
|
||||
auto payload = jsi::Object(runtime);
|
||||
payload.setProperty(runtime, \\"value\\", event.value);
|
||||
@@ -227,14 +227,14 @@ payload.setProperty(runtime, \\"scale\\", event.scale);
|
||||
return payload;
|
||||
});
|
||||
}
|
||||
void EventsNativeComponentEventEmitter::onEventDirect(EventsNativeComponentOnEventDirectStruct event) const {
|
||||
void EventsNativeComponentEventEmitter::onEventDirect(OnEventDirect event) const {
|
||||
dispatchEvent(\\"eventDirect\\", [event=std::move(event)](jsi::Runtime &runtime) {
|
||||
auto payload = jsi::Object(runtime);
|
||||
payload.setProperty(runtime, \\"value\\", event.value);
|
||||
return payload;
|
||||
});
|
||||
}
|
||||
void EventsNativeComponentEventEmitter::onOrientationChange(EventsNativeComponentOnOrientationChangeStruct event) const {
|
||||
void EventsNativeComponentEventEmitter::onOrientationChange(OnOrientationChange event) const {
|
||||
dispatchEvent(\\"orientationChange\\", [event=std::move(event)](jsi::Runtime &runtime) {
|
||||
auto payload = jsi::Object(runtime);
|
||||
payload.setProperty(runtime, \\"orientation\\", toString(event.orientation));
|
||||
@@ -266,14 +266,14 @@ Map {
|
||||
namespace facebook {
|
||||
namespace react {
|
||||
|
||||
void InterfaceOnlyComponentEventEmitter::onChange(InterfaceOnlyComponentOnChangeStruct event) const {
|
||||
void InterfaceOnlyComponentEventEmitter::onChange(OnChange event) const {
|
||||
dispatchEvent(\\"change\\", [event=std::move(event)](jsi::Runtime &runtime) {
|
||||
auto payload = jsi::Object(runtime);
|
||||
payload.setProperty(runtime, \\"value\\", event.value);
|
||||
return payload;
|
||||
});
|
||||
}
|
||||
void InterfaceOnlyComponentEventEmitter::onDire tChange(InterfaceOnlyComponentOnDire tChangeStruct event) const {
|
||||
void InterfaceOnlyComponentEventEmitter::onDire tChange(OnDire tChange event) const {
|
||||
dispatchEvent(\\"dire tChange\\", [event=std::move(event)](jsi::Runtime &runtime) {
|
||||
auto payload = jsi::Object(runtime);
|
||||
payload.setProperty(runtime, \\"value\\", event.value);
|
||||
@@ -440,7 +440,7 @@ Map {
|
||||
namespace facebook {
|
||||
namespace react {
|
||||
|
||||
void InterfaceOnlyComponentEventEmitter::onChange(InterfaceOnlyComponentOnChangeStruct event) const {
|
||||
void InterfaceOnlyComponentEventEmitter::onChange(OnChange event) const {
|
||||
dispatchEvent(\\"change\\", [event=std::move(event)](jsi::Runtime &runtime) {
|
||||
auto payload = jsi::Object(runtime);
|
||||
payload.setProperty(runtime, \\"value\\", event.value);
|
||||
|
||||
+60
-60
@@ -184,25 +184,25 @@ Map {
|
||||
namespace facebook {
|
||||
namespace react {
|
||||
|
||||
struct EventsNestedObjectNativeComponentOnChangeLocationSourceStruct {
|
||||
std::string url;
|
||||
};
|
||||
|
||||
struct EventsNestedObjectNativeComponentOnChangeLocationStruct {
|
||||
EventsNestedObjectNativeComponentOnChangeLocationSourceStruct source;
|
||||
int x;
|
||||
int y;
|
||||
};
|
||||
|
||||
struct EventsNestedObjectNativeComponentOnChangeStruct {
|
||||
EventsNestedObjectNativeComponentOnChangeLocationStruct location;
|
||||
};
|
||||
|
||||
class EventsNestedObjectNativeComponentEventEmitter : public ViewEventEmitter {
|
||||
public:
|
||||
using ViewEventEmitter::ViewEventEmitter;
|
||||
|
||||
void onChange(EventsNestedObjectNativeComponentOnChangeStruct value) const;
|
||||
struct OnChangeLocationSource {
|
||||
std::string url;
|
||||
};
|
||||
|
||||
struct OnChangeLocation {
|
||||
OnChangeLocationSource source;
|
||||
int x;
|
||||
int y;
|
||||
};
|
||||
|
||||
struct OnChange {
|
||||
OnChangeLocation location;
|
||||
};
|
||||
|
||||
void onChange(OnChange value) const;
|
||||
};
|
||||
|
||||
} // namespace react
|
||||
@@ -227,42 +227,42 @@ Map {
|
||||
namespace facebook {
|
||||
namespace react {
|
||||
|
||||
struct EventsNativeComponentOnChangeStruct {
|
||||
bool value;
|
||||
std::string source;
|
||||
int progress;
|
||||
Float scale;
|
||||
};
|
||||
|
||||
struct EventsNativeComponentOnEventDirectStruct {
|
||||
bool value;
|
||||
};
|
||||
|
||||
enum class EventsNativeComponentOnOrientationChangeOrientationStruct {
|
||||
Landscape,
|
||||
Portrait
|
||||
};
|
||||
|
||||
inline char const *toString(const EventsNativeComponentOnOrientationChangeOrientationStruct value) {
|
||||
switch (value) {
|
||||
case EventsNativeComponentOnOrientationChangeOrientationStruct::Landscape: return \\"landscape\\";
|
||||
case EventsNativeComponentOnOrientationChangeOrientationStruct::Portrait: return \\"portrait\\";
|
||||
}
|
||||
}
|
||||
|
||||
struct EventsNativeComponentOnOrientationChangeStruct {
|
||||
EventsNativeComponentOnOrientationChangeOrientationStruct orientation;
|
||||
};
|
||||
|
||||
class EventsNativeComponentEventEmitter : public ViewEventEmitter {
|
||||
public:
|
||||
using ViewEventEmitter::ViewEventEmitter;
|
||||
|
||||
void onChange(EventsNativeComponentOnChangeStruct value) const;
|
||||
struct OnChange {
|
||||
bool value;
|
||||
std::string source;
|
||||
int progress;
|
||||
Float scale;
|
||||
};
|
||||
|
||||
void onEventDirect(EventsNativeComponentOnEventDirectStruct value) const;
|
||||
struct OnEventDirect {
|
||||
bool value;
|
||||
};
|
||||
|
||||
void onOrientationChange(EventsNativeComponentOnOrientationChangeStruct value) const;
|
||||
enum class OnOrientationChangeOrientation {
|
||||
Landscape,
|
||||
Portrait
|
||||
};
|
||||
|
||||
static char const *toString(const OnOrientationChangeOrientation value) {
|
||||
switch (value) {
|
||||
case OnOrientationChangeOrientation::Landscape: return \\"landscape\\";
|
||||
case OnOrientationChangeOrientation::Portrait: return \\"portrait\\";
|
||||
}
|
||||
}
|
||||
|
||||
struct OnOrientationChange {
|
||||
OnOrientationChangeOrientation orientation;
|
||||
};
|
||||
|
||||
void onChange(OnChange value) const;
|
||||
|
||||
void onEventDirect(OnEventDirect value) const;
|
||||
|
||||
void onOrientationChange(OnOrientationChange value) const;
|
||||
|
||||
void onEnd() const;
|
||||
};
|
||||
@@ -289,21 +289,21 @@ Map {
|
||||
namespace facebook {
|
||||
namespace react {
|
||||
|
||||
struct InterfaceOnlyComponentOnChangeStruct {
|
||||
bool value;
|
||||
};
|
||||
|
||||
struct InterfaceOnlyComponentOnDire tChangeStruct {
|
||||
bool value;
|
||||
};
|
||||
|
||||
class InterfaceOnlyComponentEventEmitter : public ViewEventEmitter {
|
||||
public:
|
||||
using ViewEventEmitter::ViewEventEmitter;
|
||||
|
||||
void onChange(InterfaceOnlyComponentOnChangeStruct value) const;
|
||||
struct OnChange {
|
||||
bool value;
|
||||
};
|
||||
|
||||
void onDire tChange(InterfaceOnlyComponentOnDire tChangeStruct value) const;
|
||||
struct OnDire tChange {
|
||||
bool value;
|
||||
};
|
||||
|
||||
void onChange(OnChange value) const;
|
||||
|
||||
void onDire tChange(OnDire tChange value) const;
|
||||
};
|
||||
|
||||
} // namespace react
|
||||
@@ -472,15 +472,15 @@ Map {
|
||||
namespace facebook {
|
||||
namespace react {
|
||||
|
||||
struct InterfaceOnlyComponentOnChangeStruct {
|
||||
bool value;
|
||||
};
|
||||
|
||||
class InterfaceOnlyComponentEventEmitter : public ViewEventEmitter {
|
||||
public:
|
||||
using ViewEventEmitter::ViewEventEmitter;
|
||||
|
||||
void onChange(InterfaceOnlyComponentOnChangeStruct value) const;
|
||||
struct OnChange {
|
||||
bool value;
|
||||
};
|
||||
|
||||
void onChange(OnChange value) const;
|
||||
};
|
||||
|
||||
} // namespace react
|
||||
|
||||
Reference in New Issue
Block a user