-
Notifications
You must be signed in to change notification settings - Fork 5
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
Components with JSI events #1
Comments
I have no experience with components in JSI yet, but the keyword you should google for is "Fabric" / "React Native Fabric" Update: There's now one more good example of Fabric usage: https://blog.swmansion.com/introducing-fabric-to-react-native-screens-fd17bf18858e |
I just remembered an example of Fabric usage - the react-native-swiftui repo. It's a fork of react-native and it's rather complicated, but maybe browsing commit history will uncover some interesting stuff. 😉 I didn't dig deep into it, but maybe it helps. The author describes it briefly in a blog post. |
Hey , dude . Nice to have this great example. --- Code Below --- // JavaXXXModule.java
...
static void doSomeAsyncTask() {
SomeModule.runTask()
.addOnSuccessListener(v -> {
})
.addOnFailureListener(e -> {
});
}
... //CppXXXModule.cpp
jsiRuntime.global().setProperty(jsiRuntime, "runNativeTask", move(runNativeTask)); // App.tsx
global.runNativeTask( (v) => {
// accept the result from native
} ); Here is the full flow
here comes the question , when and where should I invoke the callback passed from javascript to return the output . It's pretty easy to get this done if the java method is synchronous . but it's async ... 😕 |
Hi, actually I'm doing something similar here: expo/expo#16075, but in your case, it could be simplified more:
|
dude , I've tried the flow . There comes another question of the jsi::Value *args . static void onTaskSuccess(jni::alias_ref<jclass> clazz,
jlong jsiRuntimePtr,
jni::alias_ref<react::CallInvokerHolder::javaobject> holder) {
auto jsiRuntime = reinterpret_cast<jsi::Runtime *>(jsiRuntimePtr);
if(store.args != nullptr) {
if(store.args[0].asObject(*jsiRuntime).isFunction(*jsiRuntime)){
LOGD("yes ,it is");
}else{
LOGD("not a function");
}
}
} this is my c++ store class class Store {
public:
jsi::Runtime *runtime;
const jsi::Value *args;
void setRuntime(jsi::Runtime *rt){
this->runtime = rt;
}
void setArgs(const jsi::Value *ags) {
this->args = ags;
}
}; and this is my host-function jsi::Function::createFromHostFunction(
jsiRuntime,
jsi::PropNameID::forAscii(jsiRuntime, "identifyLanguage"),
1,
[](jsi::Runtime &runtime, const jsi::Value &thiz, const jsi::Value *args, size_t count) -> jsi::Value {
MlkitTranslateTextModule::identifyLanguage();
jsi::Value* cps = new jsi::Value[1];
memcpy(cps,args,sizeof(args[0])*1);
store.setArgs(cps);
return jsi::Value::null();
}); on the TaskSuccessCallback , it logged out
so , I assume that the args[0] is released somehow. |
I think one problem is that you're trying to // iOS, see AudioSampleCallbackWrapper code for further part
if (argsCount > 1 && args[1].isObject()) {
auto callback = args[1].asObject(runtime).asFunction(runtime);
auto wrapper = std::make_unique<AudioSampleCallbackWrapper>(std::move(callback), runtime, strongCallInvoker); // Android PR example
if (argsCount > 1 && args[1].isObject()) {
auto callback = args[1].asObject(runtime).asFunction(runtime);
auto callbackShared = std::make_shared<jsi::Function>(std::move(callback));
// you want to store this function (as std::function) somewhere
auto cppFunction = [callbackShared, &runtime, callInvoker](int someResultYouWantToPass) {
callInvoker->invokeAsync([callbackShared, &runtime, someResultYouWantToPass] () {
try {
// not sure about passing the result here, but see the PR code
callbackShared->call(runtime, jsi::Value(someResultYouWantToPass));
} catch (std::exception &exception) {
// ...
}
});
// then call that function in JNI function
cppFunction() |
dude !! you saved my ass !!!!!!! It worked. 😆 |
Amazing examples!
I'm looking to send events in a custom MapView with ViewManager component over the bridge is this possible?
The text was updated successfully, but these errors were encountered: