import {Meta} from '@storybook/addon-docs/blocks'

<Meta title='@dxtmisha/en/figma-code/Classes/FigmaPluginMessenger - Backend Messenger'/>

# FigmaPluginMessenger Class

Messenger for the Figma plugin side (backend). It handles sending messages to the Figma UI and listening for messages from the UI using the internal Figma `figma.ui` API.

## Initialization

Extends `FigmaPostAbstract`.

```typescript
import { FigmaPluginMessenger } from '@dxtmisha/figma-code'

const messenger = new FigmaPluginMessenger()
  .make()
  .add('hello-ui', (msg) => console.log('UI:', msg))
```

## Public Methods

### `make`

Initializes the message listener using the `figma.ui.onmessage` API. Should be called once to start receiving messages from the UI.

**Returns:** `this`

```javascript
messenger.make()
```

### `add`

Registers a listener for a specific message type coming from the UI.

**Parameters:**
- `type: string` — Unique identifier for the message type to listen for.
- `callback: (message: Message) => void` — function to execute when a matching message arrives.

**Returns:** `this`

```javascript
messenger.add('resize-ui', (data) => {
  figma.ui.resize(data.width, data.height)
})
```

### `post`

Sends a message to the Figma UI side.

**Parameters:**
- `type: string` — The identifier of the message (type).
- `message: Message` — (Optional) Data payload to send to the UI.

**Returns:** `void`

```javascript
messenger.post('selection-data', { 
  ids: figma.currentPage.selection.map(node => node.id) 
})
```
