Skip to content
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 GraphNode example for Cxx TMs #41766

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,16 @@ std::string NativeCxxModuleExample::consumeCustomHostObject(
return value->a_ + std::to_string(value->b_);
}

GraphNode NativeCxxModuleExample::getGraphNode(
jsi::Runtime& rt,
GraphNode arg) {
if (arg.neighbors) {
arg.neighbors->emplace_back(GraphNode{.label = "top"});
arg.neighbors->emplace_back(GraphNode{.label = "down"});
}
return arg;
}

NativeCxxModuleExampleCxxEnumFloat NativeCxxModuleExample::getNumEnum(
jsi::Runtime& rt,
NativeCxxModuleExampleCxxEnumInt arg) {
Expand Down
38 changes: 38 additions & 0 deletions packages/rn-tester/NativeCxxModuleExample/NativeCxxModuleExample.h
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,42 @@ struct CustomHostObjectRef {

using CustomHostObject = HostObjectWrapper<CustomHostObjectRef>;

#pragma mark - recursive objects
struct GraphNode {
std::string label;
std::optional<std::vector<GraphNode>> neighbors;
};

template <>
struct Bridging<GraphNode> {
static GraphNode fromJs(
jsi::Runtime& rt,
const jsi::Object& value,
const std::shared_ptr<CallInvoker>& jsInvoker) {
GraphNode result{
bridging::fromJs<std::string>(
rt, value.getProperty(rt, "label"), jsInvoker),
bridging::fromJs<std::optional<std::vector<GraphNode>>>(
rt, value.getProperty(rt, "neighbors"), jsInvoker)};
return result;
}

static jsi::Object toJs(
jsi::Runtime& rt,
const GraphNode value,
const std::shared_ptr<CallInvoker>& jsInvoker) {
auto result = facebook::jsi::Object(rt);
result.setProperty(rt, "label", bridging::toJs(rt, value.label, jsInvoker));
if (value.neighbors) {
result.setProperty(
rt,
"neighbors",
bridging::toJs(rt, value.neighbors.value(), jsInvoker));
}
return result;
}
};

#pragma mark - implementation
class NativeCxxModuleExample
: public NativeCxxModuleExampleCxxSpec<NativeCxxModuleExample> {
Expand Down Expand Up @@ -120,6 +156,8 @@ class NativeCxxModuleExample
jsi::Runtime& rt,
std::shared_ptr<CustomHostObject> arg);

GraphNode getGraphNode(jsi::Runtime& rt, GraphNode arg);

NativeCxxModuleExampleCxxEnumFloat getNumEnum(
jsi::Runtime& rt,
NativeCxxModuleExampleCxxEnumInt arg);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,19 @@ export type ValueStruct = {|

export type CustomHostObject = {};

export type GraphNode = {
label: string,
neighbors?: Array<GraphNode>,
};

export interface Spec extends TurboModule {
+getArray: (arg: Array<ObjectStruct | null>) => Array<ObjectStruct | null>;
+getBool: (arg: boolean) => boolean;
+getConstants: () => ConstantsStruct;
+getCustomEnum: (arg: EnumInt) => EnumInt;
+getCustomHostObject: () => CustomHostObject;
+consumeCustomHostObject: (customHostObject: CustomHostObject) => string;
+getGraphNode: (arg: GraphNode) => GraphNode;
+getNumEnum: (arg: EnumInt) => EnumFloat;
+getStrEnum: (arg: EnumNone) => EnumStr;
+getMap: (arg: {[key: string]: ?number}) => {[key: string]: ?number};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ type Examples =
| 'getBool'
| 'getConstants'
| 'getCustomEnum'
| 'getCustomHostObject'
| 'getGraphNode'
| 'getNumEnum'
| 'getStrEnum'
| 'getMap'
Expand Down Expand Up @@ -102,6 +104,11 @@ class NativeCxxModuleExampleExample extends React.Component<{||}, State> {
NativeCxxModuleExample?.consumeCustomHostObject(
NativeCxxModuleExample?.getCustomHostObject(),
),
getGraphNode: () =>
NativeCxxModuleExample?.getGraphNode({
label: 'root',
neighbors: [{label: 'left'}, {label: 'right'}],
}),
getNumEnum: () => NativeCxxModuleExample?.getNumEnum(EnumInt.IB),
getStrEnum: () => NativeCxxModuleExample?.getStrEnum(EnumNone.NB),
getNumber: () => NativeCxxModuleExample?.getNumber(99.95),
Expand Down
Loading