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 /guide/concepts/threads.md.

Threads: where your code runs

Lynx runs JavaScript on two threads. AngularLynx runs your app on one of them, and uses the other to reach APIs your app's thread doesn't have. You can write most components without thinking about this. It matters for a few APIs that exist on only one thread.

Two threads instead of one

In a browser, your JavaScript shares one thread with layout and painting. A long task blocks the page.

Lynx gives JavaScript two separate threads:

  • The main thread creates and updates the native UI, and receives touches first. It has a small set of APIs.
  • The background thread is a fuller JavaScript environment. Some APIs, such as fetch, exist only here.

The threads don't share memory. They can only send each other messages, and every message is copied, which takes time.

Where AngularLynx runs your code

Your Angular app runs on the main thread. The background thread doesn't load it: it loads AngularLynx's side of each bridge and your background functions, and runs only what the main thread asks of it.

  • Your app draws the UI on the main thread. Its templates become the native elements on screen.
  • Your event handlers run on the main thread, right when the native event fires. That's why a tap gets a response without waiting for the other thread.
  • The background thread runs calls for your app. Some AngularLynx services, such as LynxLogger and LynxNativeModule, send calls there for you. Your own code gets there through a background function.

Constructors, ngOnInit and effects run once, like in a browser.

The phone's UI thread

iOS and Android draw the screen on a thread of their own, the UI thread. In the apps ng build --platform ios|android makes, the main thread isn't that thread: Lynx runs it, and your app, on a thread of its own.

So while your app is busy, building a big screen or running a long handler, the phone keeps going:

  • A <scroll-view> you flicked keeps scrolling.
  • Text fields and web views keep taking touches and typing.

What waits for your app is what Lynx draws from your templates: the next screen, a changed style, and Lynx's animations (CSS transitions, @keyframes and element.animate()), which run on the main thread too. Keep handlers short, and send heavy work to the background thread.

Lynx Explorer runs the main thread on the UI thread. There, a busy app freezes scrolling too, so check how a heavy screen feels in a build of your own app.

Event handlers

Inside an event handler, do what you'd do on the web. Change signals, navigate, open overlays and start animations, straight away.

import { Component, inject, signal } from '@angular/core';
import { Router } from '@angular/router';
import { LYNX_ELEMENTS } from '@blotch/angular-lynx';

@Component({
  selector: 'app-item',
  imports: [LYNX_ELEMENTS],
  template: `
    <view (bindtap)="open()">
      <text>{{ label() }}</text>
    </view>
  `,
})
export class Item {
  readonly label = signal('Open details');
  readonly #router = inject(Router);

  open(): void {
    this.label.set('Opening…');
    this.#router.navigate(['/details']);
  }
}
  • Angular updates the screen after the handler returns. Setting a signal marks the view as changed, and the router builds the next screen a moment later, as on the web.
  • You don't need setTimeout. Older versions of this guide said to wrap navigation, overlays and animations in it. Measured on Lynx 4.1 on iOS, Android and the web preview, a handler can create and remove elements, flush them to the screen and navigate without a crash. On iOS and Android, so can a gesture callback.

APIs that live on one thread

You want toThread it needsWhat to do
Update the UIMainChange signals. Angular does the rest
Make network requestsBackgroundUse HttpClient or fetch from anywhere. AngularLynx sends the requests to the background thread. See HTTP requests
Move an element on every frame, e.g. a dragMainUse a main-thread script
Load a font at runtimeBackgroundCall LynxFont.addFont() from anywhere. AngularLynx sends it to the background thread. See Custom fonts
Call your host app's native codeBackgroundUse LynxNativeModule. AngularLynx sends the calls to the background thread