> ## Documentation Index
> Fetch the complete documentation index at: https://docs.lynxhub.app/llms.txt
> Use this file to discover all available pages before exploring further.

# Lifecycle hooks and utilities

> Manage main process lifecycle event hooks and access host managers.

The backend registry allows you to execute operations during app startup, register custom tray menu triggers, and retrieve utility instances of major application managers.

***

## App lifecycle event registry

The `lynxApi` parameter exposes methods to register callback hooks executing at various stages of the application lifecycle:

```typescript theme={null}
// Triggers when Electron has finished booting (app.whenReady())
lynxApi.onAppReady(callback: () => Promise<void>): void;

// Triggers when the main BrowserWindow is ready to show
lynxApi.onReadyToShow(callback: () => void): void;

// Registers custom IPC channels (on/handle listeners)
lynxApi.listenForChannels(callback: () => void): void;
```

***

## System tray integration

You can insert custom menu options directly into the LynxHub system tray context menu.

```typescript theme={null}
import {MenuItemConstructorOptions} from 'electron';

lynxApi.trayMenu_AddItem(callback: () => {
  item: MenuItemConstructorOptions;
  index: number;
}): void;
```

### Example: Tray item clicks

```javascript theme={null}
lynxApi.trayMenu_AddItem(() => {
  return {
    item: {
      label: 'Restart LynxHub Backend',
      click: async () => {
        const appManager = await utils.getAppManager();
        appManager.restart();
      },
    },
    index: 2, // Index position inside the dropdown menu list
  };
});
```

***

## Error tracking telemetry

To track uncaught exceptions in the backend process, initialize a Sentry telemetry scope:

```typescript theme={null}
const scope = lynxApi.initNodeSentry('https://your-sentry-dsn@sentry.io/project');
scope.setTag('backend_extension', 'active');
```

***

## Main process utilities (`utils`)

The `utils` parameter contains references to core modules, terminal tools, and window managers:

### Native pseudo-terminals (`nodePty`)

Exposes the `node-pty` module directly. You can use it to spawn shell terminals or Python executables:

```typescript theme={null}
const ptyProcess = utils.nodePty.spawn('python', ['-m', 'venv', 'venv-dir'], {
  name: 'xterm-color',
  cols: 80,
  rows: 24,
  cwd: '/project-dir',
  env: process.env,
});
```

***

### Window manager (`getAppManager()`)

Resolves to the `MainWindowManager` class singleton. Use this to control the main window or send IPC messages directly to the frontend.

```typescript theme={null}
const appManager = await utils.getAppManager();

// Retrieve window objects
const win = appManager.getMainWindow(); // Returns BrowserWindow
const web = appManager.getWebContent(); // Returns WebContents

// Send raw IPC events to the frontend
appManager.sendMessage('custom-channel', {status: 'updated'});

// Relaunch the application
appManager.restart();
```

***

### Modules controller (`getModuleManager()`)

Resolves to the `ModuleManager` class singleton. Use this to monitor or modify installed card modules.

```typescript theme={null}
const moduleManager = await utils.getModuleManager();

// Retrieve methods registered by card modules
const methods = moduleManager.getMethodsById(cardId);

// Uninstall card modules
await moduleManager.uninstallCardByID(cardId);
```
