# Lifecycle

## MAESTROSDK.beforeRefresh(handler)

Registers a function that runs before Cowork refreshes this Mini App's tab, for example when the person presses the refresh button. Use it to keep unsaved work: ask, save, or cancel the refresh.

### Parameters

| Name | Type | Required | Description |
|---|---|---|---|
| `handler` | `() => boolean \| void \| Promise<boolean \| void>` | Yes | Runs once per refresh. Return `false` (or a promise that resolves to `false`) to cancel the refresh. Anything else lets it go ahead |

### Returns

`() => void`: call it to remove this handler.

### Behaviour

- Handlers run one after another, in the order they were added, and each is awaited.
- The first handler that returns `false` cancels the refresh. A handler that throws or rejects also cancels it.
- With no handler, the tab refreshes at once.
- Only one refresh is handled at a time; a second request while one is running is ignored.
- A `handler` that is not a function is ignored, and the returned function does nothing.

### Example

```js
const off = MAESTROSDK.beforeRefresh(async () => {
  if (!form.dirty) return;
  return await MAESTROSDK.confirm({ title: 'Refresh?', message: 'Unsaved changes will be lost.' });
});
// later
off();
```

## Related

- [Interaction](https://docs.micromeet.ai/miniapp-sdk/interaction.md): `confirm`, often used inside `beforeRefresh`
- [Overview](https://docs.micromeet.ai/miniapp-sdk/overview.md): All modules of the Mini App SDK
