Repository navigation
Expose worklet runtime - #601
Conversation
There was a problem hiding this comment.
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 <titzmateusz@gmail.com>
@Kicu I often ask myself this question. |
|
Totally makes sense 👍 |
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 = 42in the RN runtime which simply callsrunOnUIunder the hood, you can callrunOnRuntimeon your own like this:However, currently there's no way to obtain
workletRuntime. This PR exposesworkletRuntimeused byMarkdownTextInputcomponent 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
MarkdownTextInputis 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 ofWorkletRuntimeinstance 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