-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Get uiRuntime directly from react-native-worklets
#4276
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
base: main
Are you sure you want to change the base?
Changes from 4 commits
85194cf
bb38d49
3f1f1fb
d4760a6
9bdf85f
e95ef67
9200d64
2227896
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,6 +3,10 @@ | |
|
|
||
| #include "RNGestureHandlerModule.h" | ||
|
|
||
| #ifdef RNGH_USE_WORKLETS | ||
| #include <worklets/android/WorkletsModule.h> | ||
| #endif | ||
|
|
||
| namespace gesturehandler { | ||
| using namespace facebook; | ||
| using namespace facebook::react; | ||
|
|
@@ -21,6 +25,9 @@ void RNGestureHandlerModule::registerNatives() { | |
| RNGestureHandlerModule::getBindingsInstallerCxx), | ||
| makeNativeMethod( | ||
| "decorateUIRuntime", RNGestureHandlerModule::decorateUIRuntime), | ||
| makeNativeMethod( | ||
| "decorateUIRuntimeWithWorklets", | ||
| RNGestureHandlerModule::decorateUIRuntimeWithWorklets), | ||
| makeNativeMethod( | ||
| "invalidateNative", RNGestureHandlerModule::invalidateNative)}); | ||
| } | ||
|
|
@@ -58,6 +65,39 @@ bool RNGestureHandlerModule::decorateUIRuntime() { | |
| }); | ||
| } | ||
|
|
||
| bool RNGestureHandlerModule::decorateUIRuntimeWithWorklets( | ||
| jni::alias_ref<jobject> workletsModule) { | ||
| #ifdef RNGH_USE_WORKLETS | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| if (!workletsModule) { | ||
| return false; | ||
| } | ||
|
|
||
| const auto jWorkletsModule = | ||
| jni::static_ref_cast<worklets::WorkletsModule::javaobject>( | ||
| workletsModule); | ||
| const auto workletsModuleProxy = | ||
| jWorkletsModule->cthis()->getWorkletsModuleProxy(); | ||
| if (!workletsModuleProxy) { | ||
| return false; | ||
| } | ||
|
|
||
| const auto uiWorkletRuntime = workletsModuleProxy->getUIWorkletRuntime(); | ||
| if (!uiWorkletRuntime) { | ||
| return false; | ||
| } | ||
|
Comment on lines
+77
to
+83
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This might be the one example when nesting these |
||
|
|
||
| RNGHRuntimeDecorator::decorateUIRuntime( | ||
| uiWorkletRuntime->getJSIRuntime(), [&](int handlerTag, int state) { | ||
| this->setGestureState(handlerTag, state); | ||
| }); | ||
|
|
||
| return true; | ||
| #else | ||
| (void)workletsModule; | ||
| return false; | ||
| #endif | ||
| } | ||
|
|
||
| void RNGestureHandlerModule::invalidateNative() { | ||
| // This is called when the module is being destroyed, so we need to clear | ||
| // the reference to the java part to avoid memory leaks. | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -16,6 +16,13 @@ | |
|
|
||
| #import "RNGHRuntimeDecorator.h" | ||
|
|
||
| #if __has_include(<worklets/apple/WorkletsModule.h>) | ||
|
coado marked this conversation as resolved.
Outdated
|
||
| #import <worklets/apple/WorkletsModule.h> | ||
| #define RNGH_HAS_WORKLETS 1 | ||
| #else | ||
| #define RNGH_HAS_WORKLETS 0 | ||
| #endif | ||
|
|
||
| #import "RNGestureHandler.h" | ||
| #import "RNGestureHandlerDirection.h" | ||
| #import "RNGestureHandlerState.h" | ||
|
|
@@ -108,6 +115,26 @@ - (bool)decorateUIRuntime | |
| { | ||
| __weak RNGestureHandlerModule *weakSelf = self; | ||
|
|
||
| #if RNGH_HAS_WORKLETS | ||
| WorkletsModule *workletsModule = (WorkletsModule *)[self.moduleRegistry moduleForName:"WorkletsModule"]; | ||
| if (workletsModule != nil) { | ||
| auto workletsModuleProxy = [workletsModule getWorkletsModuleProxy]; | ||
| if (workletsModuleProxy != nullptr) { | ||
| auto uiWorkletRuntime = workletsModuleProxy->getUIWorkletRuntime(); | ||
| if (uiWorkletRuntime != nullptr) { | ||
| RNGHRuntimeDecorator::decorateUIRuntime( | ||
| uiWorkletRuntime->getJSIRuntime(), [weakSelf](int handlerTag, int state) { | ||
| RNGestureHandlerModule *strongSelf = weakSelf; | ||
| if (strongSelf != nil) { | ||
| [strongSelf setGestureState:state forHandler:handlerTag]; | ||
| } | ||
| }); | ||
| return true; | ||
| } | ||
| } | ||
| } | ||
| #endif | ||
|
Comment on lines
+117
to
+127
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. And this |
||
|
|
||
| return RNGHRuntimeDecorator::installUIRuntimeBindings(*_rnRuntime, _moduleId, [weakSelf](int handlerTag, int state) { | ||
| RNGestureHandlerModule *strongSelf = weakSelf; | ||
| if (strongSelf != nil) { | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.