Skip to content

Commit 5754b4a

Browse files
Add GraphNode example for Cxx TMs (#41766)
Summary: Pull Request resolved: #41766 Changelog: [Internal] Adds a simple example showing a recursive node, stored inside a collection in a Cxx TM. Currently we can't auto-generate [the necessary C++ Types](https://reactnative.dev/docs/next/the-new-architecture/cxx-custom-types#struct-generator) - but we can add it later if this scenarios becomes really common. Reviewed By: rshest Differential Revision: D51783974 fbshipit-source-id: 7352db1a354cd7da32febc650f7cc5e10dd16d2d
1 parent 3854735 commit 5754b4a

File tree

4 files changed

+61
-0
lines changed

4 files changed

+61
-0
lines changed

packages/rn-tester/NativeCxxModuleExample/NativeCxxModuleExample.cpp

+10
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,16 @@ std::string NativeCxxModuleExample::consumeCustomHostObject(
6565
return value->a_ + std::to_string(value->b_);
6666
}
6767

68+
GraphNode NativeCxxModuleExample::getGraphNode(
69+
jsi::Runtime& rt,
70+
GraphNode arg) {
71+
if (arg.neighbors) {
72+
arg.neighbors->emplace_back(GraphNode{.label = "top"});
73+
arg.neighbors->emplace_back(GraphNode{.label = "down"});
74+
}
75+
return arg;
76+
}
77+
6878
NativeCxxModuleExampleCxxEnumFloat NativeCxxModuleExample::getNumEnum(
6979
jsi::Runtime& rt,
7080
NativeCxxModuleExampleCxxEnumInt arg) {

packages/rn-tester/NativeCxxModuleExample/NativeCxxModuleExample.h

+38
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,42 @@ struct CustomHostObjectRef {
9090

9191
using CustomHostObject = HostObjectWrapper<CustomHostObjectRef>;
9292

93+
#pragma mark - recursive objects
94+
struct GraphNode {
95+
std::string label;
96+
std::optional<std::vector<GraphNode>> neighbors;
97+
};
98+
99+
template <>
100+
struct Bridging<GraphNode> {
101+
static GraphNode fromJs(
102+
jsi::Runtime& rt,
103+
const jsi::Object& value,
104+
const std::shared_ptr<CallInvoker>& jsInvoker) {
105+
GraphNode result{
106+
bridging::fromJs<std::string>(
107+
rt, value.getProperty(rt, "label"), jsInvoker),
108+
bridging::fromJs<std::optional<std::vector<GraphNode>>>(
109+
rt, value.getProperty(rt, "neighbors"), jsInvoker)};
110+
return result;
111+
}
112+
113+
static jsi::Object toJs(
114+
jsi::Runtime& rt,
115+
const GraphNode value,
116+
const std::shared_ptr<CallInvoker>& jsInvoker) {
117+
auto result = facebook::jsi::Object(rt);
118+
result.setProperty(rt, "label", bridging::toJs(rt, value.label, jsInvoker));
119+
if (value.neighbors) {
120+
result.setProperty(
121+
rt,
122+
"neighbors",
123+
bridging::toJs(rt, value.neighbors.value(), jsInvoker));
124+
}
125+
return result;
126+
}
127+
};
128+
93129
#pragma mark - implementation
94130
class NativeCxxModuleExample
95131
: public NativeCxxModuleExampleCxxSpec<NativeCxxModuleExample> {
@@ -120,6 +156,8 @@ class NativeCxxModuleExample
120156
jsi::Runtime& rt,
121157
std::shared_ptr<CustomHostObject> arg);
122158

159+
GraphNode getGraphNode(jsi::Runtime& rt, GraphNode arg);
160+
123161
NativeCxxModuleExampleCxxEnumFloat getNumEnum(
124162
jsi::Runtime& rt,
125163
NativeCxxModuleExampleCxxEnumInt arg);

packages/rn-tester/NativeCxxModuleExample/NativeCxxModuleExample.js

+6
Original file line numberDiff line numberDiff line change
@@ -56,13 +56,19 @@ export type ValueStruct = {|
5656

5757
export type CustomHostObject = {};
5858

59+
export type GraphNode = {
60+
label: string,
61+
neighbors?: Array<GraphNode>,
62+
};
63+
5964
export interface Spec extends TurboModule {
6065
+getArray: (arg: Array<ObjectStruct | null>) => Array<ObjectStruct | null>;
6166
+getBool: (arg: boolean) => boolean;
6267
+getConstants: () => ConstantsStruct;
6368
+getCustomEnum: (arg: EnumInt) => EnumInt;
6469
+getCustomHostObject: () => CustomHostObject;
6570
+consumeCustomHostObject: (customHostObject: CustomHostObject) => string;
71+
+getGraphNode: (arg: GraphNode) => GraphNode;
6672
+getNumEnum: (arg: EnumInt) => EnumFloat;
6773
+getStrEnum: (arg: EnumNone) => EnumStr;
6874
+getMap: (arg: {[key: string]: ?number}) => {[key: string]: ?number};

packages/rn-tester/js/examples/TurboModule/NativeCxxModuleExampleExample.js

+7
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ type Examples =
4242
| 'getBool'
4343
| 'getConstants'
4444
| 'getCustomEnum'
45+
| 'getCustomHostObject'
46+
| 'getGraphNode'
4547
| 'getNumEnum'
4648
| 'getStrEnum'
4749
| 'getMap'
@@ -102,6 +104,11 @@ class NativeCxxModuleExampleExample extends React.Component<{||}, State> {
102104
NativeCxxModuleExample?.consumeCustomHostObject(
103105
NativeCxxModuleExample?.getCustomHostObject(),
104106
),
107+
getGraphNode: () =>
108+
NativeCxxModuleExample?.getGraphNode({
109+
label: 'root',
110+
neighbors: [{label: 'left'}, {label: 'right'}],
111+
}),
105112
getNumEnum: () => NativeCxxModuleExample?.getNumEnum(EnumInt.IB),
106113
getStrEnum: () => NativeCxxModuleExample?.getStrEnum(EnumNone.NB),
107114
getNumber: () => NativeCxxModuleExample?.getNumber(99.95),

0 commit comments

Comments
 (0)