Skip to content

Commit

Permalink
globals installation example
Browse files Browse the repository at this point in the history
Differential Revision: D53083834
  • Loading branch information
philIip authored and facebook-github-bot committed Feb 17, 2024
1 parent 8ff05b5 commit 82ff664
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,13 @@ namespace facebook::react {

NativeCxxModuleExample::NativeCxxModuleExample(
std::shared_ptr<CallInvoker> jsInvoker)
: NativeCxxModuleExampleCxxSpec(std::move(jsInvoker)) {}
: NativeCxxModuleExampleCxxSpec(std::move(jsInvoker)) {
LOG(ERROR) << "[sammy] NativeCxxModuleExample::NativeCxxModuleExample";
}

NativeCxxModuleExample::~NativeCxxModuleExample() {
LOG(ERROR) << "[sammy] NativeCxxModuleExample::~NativeCxxModuleExample";
}

void NativeCxxModuleExample::getValueWithCallback(
jsi::Runtime& rt,
Expand Down Expand Up @@ -166,7 +172,15 @@ std::optional<bool> NativeCxxModuleExample::getWithWithOptionalArgs(
}

void NativeCxxModuleExample::voidFunc(jsi::Runtime& rt) {
// Nothing to do
LOG(ERROR) << "[sammy] NativeCxxModuleExample::voidFunc";
}

void NativeCxxModuleExample::installGlobals(jsi::Runtime& rt) {
LOG(ERROR) << "[sammy] NativeCxxModuleExample::installBindings";
rt.global().setProperty(
rt,
"NativeCxxModuleExample$global",
jsi::String::createFromUtf8(rt, "my nice global"));
}

void NativeCxxModuleExample::setMenu(jsi::Runtime& rt, MenuItem menuItem) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ class NativeCxxModuleExample
: public NativeCxxModuleExampleCxxSpec<NativeCxxModuleExample> {
public:
NativeCxxModuleExample(std::shared_ptr<CallInvoker> jsInvoker);
~NativeCxxModuleExample();

void getValueWithCallback(
jsi::Runtime& rt,
Expand Down Expand Up @@ -183,6 +184,8 @@ class NativeCxxModuleExample

void voidFunc(jsi::Runtime& rt);

void installGlobals(jsi::Runtime& rt);

void setMenu(jsi::Runtime& rt, MenuItem menuItem);

void emitCustomDeviceEvent(jsi::Runtime& rt, jsi::String eventName);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ export interface Spec extends TurboModule {
+getValueWithPromise: (error: boolean) => Promise<string>;
+getWithWithOptionalArgs: (optionalArg?: boolean) => ?boolean;
+voidFunc: () => void;
+installGlobals: () => void;
+setMenu: (menuItem: MenuItem) => void;
+emitCustomDeviceEvent: (eventName: string) => void;
+voidFuncThrows: () => void;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,8 @@ type Examples =
| 'voidFunc'
| 'setMenuItem'
| 'optionalArgs'
| 'emitDeviceEvent';
| 'emitDeviceEvent'
| 'installGlobals';

type ErrorExamples =
| 'voidFuncThrows'
Expand Down Expand Up @@ -136,6 +137,19 @@ class NativeCxxModuleExampleExample extends React.Component<{||}, State> {
.then(() => {})
.catch(e => this._setResult('rejectPromise', e.message)),
voidFunc: () => NativeCxxModuleExample?.voidFunc(),
installGlobals: () => {
console.log(
'sammy',
'globals are undefined',
global.NativeCxxModuleExample$global,
);
NativeCxxModuleExample?.installGlobals();
console.log(
'sammy',
'globals are defined',
global.NativeCxxModuleExample$global,
);
},
setMenuItem: () => {
let curValue = '';
NativeCxxModuleExample?.setMenu({
Expand Down

0 comments on commit 82ff664

Please sign in to comment.