vue-lynx / runOnMainThread

Function: runOnMainThread()

function runOnMainThread<R, Fn>(fn): (...args) => Promise<R>

Mark a function to be executed on the Main Thread.

Returns a wrapper that, when called from the Background Thread, dispatches the call to the Main Thread via the worklet runtime and returns a Promise that resolves to the function's return value.

The SWC worklet transform replaces the fn argument with a worklet context object at build time. Without the transform, fn is passed through as-is (dev warning in non-production builds).

Type Parameters

Type Parameter
R
Fn extends (...args) => R

Parameters

ParameterType
fnFn

Returns

Function

Parameters

ParameterType
...argsParameters<Fn>

Returns

Promise<R>

Example

const animate = runOnMainThread((x: number) => {
  'main thread'
  element.setStyleProperty('opacity', String(x))
})
await animate(0.5) // executes on Main Thread

Defined in

packages/vue-lynx/runtime/src/cross-thread.ts:41