Skip to content

Commit

Permalink
Move event structs into event emitter namespace
Browse files Browse the repository at this point in the history
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
  • Loading branch information
sammy-SC authored and facebook-github-bot committed Jan 14, 2020
1 parent f15b80b commit 6bdfd84
Show file tree
Hide file tree
Showing 8 changed files with 114 additions and 106 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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};
}

Expand Down Expand Up @@ -136,7 +138,7 @@ - (void)ensurePresentedOnlyIfNeeded

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{});
}];
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -309,11 +309,11 @@ - (void)onChange:(UISlider *)sender withContinuous:(BOOL)continuous

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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ - (void)onChange:(UISwitch *)sender
}

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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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> = [],
Expand Down Expand Up @@ -206,4 +211,5 @@ module.exports = {
toSafeCppString,
toIntEnumValueName,
generateStructName,
generateEventStructName,
};
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

'use strict';

const {generateStructName} = require('./CppHelpers.js');
const {generateEventStructName} = require('./CppHelpers.js');

import type {
ComponentShape,
Expand Down Expand Up @@ -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);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ const nullthrows = require('nullthrows');
const {
getCppTypeForAnnotation,
toSafeCppString,
generateStructName,
generateEventStructName,
} = require('./CppHelpers.js');

import type {
Expand Down Expand Up @@ -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,
Expand All @@ -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 ');
Expand Down Expand Up @@ -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 => {
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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;`;
}
Expand Down Expand Up @@ -261,7 +264,7 @@ module.exports = {
.replace(/::_CLASSNAME_::/g, componentName)
.replace(
'::_STRUCTS_::',
generateStructs(componentName, component),
indent(generateStructs(componentName, component), 2),
)
.replace(
'::_EVENTS_::',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
{
Expand Down Expand Up @@ -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);
Expand All @@ -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));
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down
Loading

0 comments on commit 6bdfd84

Please sign in to comment.