-
Notifications
You must be signed in to change notification settings - Fork 73
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
Expose worklet runtime #601
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This fixes a real life problem when using workletized parser in LiveMarkdown and I saw it working 👍
I have one question though. In your examples we pass an anonymous function onPress like this:
() => {runOnRuntime(workletRuntime, () => {
'worklet';
sv.value = 42;
})()
};
But runOnRuntime
already returns a function, so in onPress
could we also write code like this?
onPress={runOnRuntime(workletRuntime, () => {
'worklet';
sv.value = 42;
})}
I think in useEffect
we might still need the outer wrapper because React likes it that way (though I'm a bit unsure 🤔) but in onPress
it should be ok
Co-authored-by: Mateusz Titz <[email protected]>
@Kicu I often ask myself this question. |
Totally makes sense 👍 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
Details
When you write to a shared value in the RN runtime using
sv.value = 42;
orsv.value(42);
syntax, the call is scheduled to be run on the UI runtime. This means that the shared value doesn't update its value on other worklet runtimes.As a workaround, instead of calling
sv.value = 42
in the RN runtime which simply callsrunOnUI
under the hood, you can callrunOnRuntime
on your own like this:However, currently there's no way to obtain
workletRuntime
. This PR exposesworkletRuntime
used byMarkdownTextInput
component usinggetWorkletRuntime()
function.Because worklet runtime creation is lazy (since we really shouldn't call native/turbo modules in the top-level scope so we delay the creation till
MarkdownTextInput
is first rendered), please make sure to callgetWorkletRuntime()
function only when the worklet runtime has already been initialized. This means you cannot callgetWorkletRuntime()
in top-level scope of your code. Also, please make sure not to memoize or even store its result since since this affects the lifetime ofWorkletRuntime
instance which should be managed only byreact-native-live-markdown
. Finally, please do not callgetWorkletRuntime()
on web where parsers are run directly on the same JS runtime and there's no concept of worklet runtimes.As for
runOnRuntime
, this function is sadly not available on web. Also, keep in mind thatrunOnRuntime(worklet)
itself doesn't schedule the worklet to be run; it just creates a JS function that, when called, will schedule the call. So if you want to immediately schedule the worklet, please use the following syntax:runOnRuntime(() => { ... }))();
with()
at the end of the line.Related Issues
GH_LINK
Manual Tests
App.tsx
Linked PRs