For AI agents: the complete documentation index is available at /llms.txt, the full documentation bundle is available at /llms-full.txt, and this page is available as Markdown at /api/main-thread/main-thread-fn.md.

mainThreadFn

Registers a function as a main-thread script: it gets native elements directly and changes them without a change-detection pass. import { mainThreadFn } from '@blotch/angular-lynx';

Info

Template handlers such as (bindtap) already run on the main thread. Main-thread scripts explains when you need this API instead.

Usage

src/app/app.ts
import { Component } from '@angular/core';
import {
  LYNX_ELEMENTS,
  LynxMainThreadEvent,
  mainThreadFn,
  type MainThread,
} from '@blotch/angular-lynx';

const highlight = mainThreadFn((event: MainThread.TouchEvent) => {
  event.currentTarget.setStyleProperty('background-color', '#ff5722');
});

@Component({
  selector: 'app-root',
  imports: [LYNX_ELEMENTS, LynxMainThreadEvent],
  template: `
    <view [mainThreadBindtap]="highlight">
      <text>Tap me</text>
    </view>
  `,
})
export class App {
  readonly highlight = highlight;
}

Bind the result with a LynxMainThreadEvent input, or call it with LynxMainThread.runOnMainThread().

API

mainThreadFn(fn)

function mainThreadFn<TArgs extends unknown[], TReturn>(
  fn: (...args: TArgs) => TReturn,
): MainThreadFnHandle<TArgs, TReturn>;
  • fn: the function to run on the main thread. As an event handler, it receives the event.
  • Returns a handle. The handle's type keeps fn's argument and return types, so runOnMainThread() checks them.

'main thread' directive

const highlight = (event: MainThread.TouchEvent) => {
  'main thread';
  event.currentTarget.setStyleProperty('background-color', '#ff5722');
};

The build wraps any function whose first statement is 'main thread' (or 'main-thread') in mainThreadFn(), and adds the import. The result is the same as calling mainThreadFn() yourself.

  • Arrow functions and function expressions with a block body are transformed, wherever they appear.
  • Function declarations and class methods aren't. Write const name = () => { 'main thread'; … } instead.

Constraints

  • Define it at module scope. It's registered once, when its file loads. A class property also works for a [mainThreadBind*] input. Each instance registers its own function, released when the instance is destroyed.
  • It runs on the main thread, like your components. It reads the same module-level variables they do.
  • Keep state between calls in a MainThreadRef.

Types

// An opaque handle. Its fields are internal.
type MainThreadFnHandle<
  TArgs extends unknown[] = unknown[],
  TReturn = unknown,
> = {/* internal */};

MainThread.TouchEvent and the other event types are on LynxMainThreadEvent.