> For AI agents: the complete documentation index is available at /llms.txt, the full documentation bundle is available at /llms-full.txt.

# `mainThreadFn`

Registers a function as a [**main-thread script**](/guide/glossary.md#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](/guide/main-thread/index.md) explains when you need this API instead.
:::

## Usage

```typescript title="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`](/api/main-thread/main-thread-event.md) input, or call it with [`LynxMainThread.runOnMainThread()`](/api/main-thread/main-thread-service.md).

## API

### `mainThreadFn(fn)`

```typescript
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

```typescript
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`](/api/main-thread/main-thread-ref.md).**

## Types

```typescript
// 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`](/api/main-thread/main-thread-event.md#types).

## Related

- [Main-thread scripts guide](/guide/main-thread/index.md)
- [`LynxMainThreadEvent`](/api/main-thread/main-thread-event.md)
- [`LynxMainThread`](/api/main-thread/main-thread-service.md)
- [Main-thread example](/examples/main-thread.md)
