codegenNativeCommands parameter type limitations #54
-
I'm trying to write NativeSpec for component of react-native-maps, and I have some questions regarding codegenNative commands. There is apparently pretty hard limitations on types that can be used in the commands, also return value is not possible for the command. These limitations apply to both typescript and FlowType. Supported paramet types are: boolean, Int32, Double, Float, string. export interface AIRMapNativeCommands {
getCamera: (viewRef: React.ElementRef<any>) => Promise<Camera>; // <----- return value discarded
setCamera: (viewRef: React.ElementRef<any>, camera: Camera) => void; ///<---- ERROR object types can not be used as a parameter to command
animateCamera: (
viewRef: React.ElementRef<any>,
camera: Camera,
duration?: Float
) => void;
...
}
export const AIRMapCommands: AIRMapNativeCommands =
codegenNativeCommands<AIRMapNativeCommands>({
supportedCommands: [
'getCamera',
'setCamera',
...
} Questing is: what reasons behind this limitations? Can supported types to be extended later? Or I am just using it wrong? - (void) sendResponseForCommand: (NSString*) uuid withJson: (NSDictionary*) json {
NSError *error;
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:json
options:NSJSONWritingPrettyPrinted // Pass 0 if you don't care about the readability of the generated string
error:&error];
if (! jsonData) {
NSLog(@"Got an error: %@", error);
} else {
NSString *jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
auto value = (AIRMapEventEmitter::OnCommandResponse) {
std::string([uuid UTF8String]), std::string([jsonString UTF8String])
};
std::static_pointer_cast<AIRMapEventEmitter const>(_eventEmitter)->onCommandResponse(value);
}
}
- (void)getCamera:(NSString *)commandUUID {
MKMapCamera * camera = [self._map camera];
NSDictionary * json = @{
@"center": @{
@"latitude": @(camera.centerCoordinate.latitude),
@"longitude": @(camera.centerCoordinate.longitude),
},
@"pitch": @(camera.pitch),
@"heading": @(camera.heading),
@"altitude": @(camera.altitude),
};
[self sendResponseForCommand:commandUUID withJson: json];
} This just feels wrong, and looks like can be done synchronously without such hacks. Or this can be implemented differently on new architecture, and I am just using anti-pattern from old architecture? FYI: @kelset |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments
-
bump. also curious, thanks |
Beta Was this translation helpful? Give feedback.
-
Does anyone answer this question? |
Beta Was this translation helpful? Give feedback.
-
The It looks like the author is using the command as a substitute to a normal method call. I would recommend to try finding a way to implement this using Native Modules. |
Beta Was this translation helpful? Give feedback.
The
Command
abstraction is a way to tell a native component to do something bypassing the renderer. You should not expect any return value from it. The return value from a command generally may depend on many things other than component itself. This makes such component non-composable. This breaks one of React's fundamental principles. This is also the reason why it only accepts primitive types. We may add some more capabilities in the future, but there are currently no plans for that.It looks like the author is using the command as a substitute to a normal method call. I would recommend to try finding a way to implement this using Native Modules.