> ## Documentation Index
> Fetch the complete documentation index at: https://botmesh.afaya.com.co/llms.txt
> Use this file to discover all available pages before exploring further.

# MemoryCoordinator

> Class exported by @botmesh/core/testing.

Defined in: [coordination/MemoryCoordinator.ts:151](https://github.com/andresayac/botmesh/blob/main/packages/core/src/coordination/MemoryCoordinator.ts#L151)

## Implements

* [`Coordinator`](https://botmesh.afaya.com.co/api/core/interfaces/Coordinator)

## Constructors

### Constructor

```ts theme={null}
new MemoryCoordinator(opts?): MemoryCoordinator;
```

Defined in: [coordination/MemoryCoordinator.ts:159](https://github.com/andresayac/botmesh/blob/main/packages/core/src/coordination/MemoryCoordinator.ts#L159)

#### Parameters

| Parameter | Type |
| - | - |
| `opts?` | [`MemoryCoordinatorOptions`](https://botmesh.afaya.com.co/api/core/testing/interfaces/MemoryCoordinatorOptions) |

#### Returns

`MemoryCoordinator`

## Properties

### replicaId

```ts theme={null}
readonly replicaId: string;
```

Defined in: [coordination/MemoryCoordinator.ts:152](https://github.com/andresayac/botmesh/blob/main/packages/core/src/coordination/MemoryCoordinator.ts#L152)

This replica's id; unique among the replicas sharing the backend.

#### Implementation of

[`Coordinator`](https://botmesh.afaya.com.co/api/core/interfaces/Coordinator).[`replicaId`](https://botmesh.afaya.com.co/api/core/interfaces/Coordinator#replicaid)

## Methods

### start()

```ts theme={null}
start(): Promise<void>;
```

Defined in: [coordination/MemoryCoordinator.ts:164](https://github.com/andresayac/botmesh/blob/main/packages/core/src/coordination/MemoryCoordinator.ts#L164)

Begin heartbeating/renewing (idempotent).

#### Returns

`Promise`\<`void`>

#### Implementation of

[`Coordinator`](https://botmesh.afaya.com.co/api/core/interfaces/Coordinator).[`start`](https://botmesh.afaya.com.co/api/core/interfaces/Coordinator#start)

***

### stop()

```ts theme={null}
stop(): Promise<void>;
```

Defined in: [coordination/MemoryCoordinator.ts:169](https://github.com/andresayac/botmesh/blob/main/packages/core/src/coordination/MemoryCoordinator.ts#L169)

Release every turn and leadership this replica holds and stop renewing (idempotent).

#### Returns

`Promise`\<`void`>

#### Implementation of

[`Coordinator`](https://botmesh.afaya.com.co/api/core/interfaces/Coordinator).[`stop`](https://botmesh.afaya.com.co/api/core/interfaces/Coordinator#stop)

***

### crash()

```ts theme={null}
crash(): void;
```

Defined in: [coordination/MemoryCoordinator.ts:187](https://github.com/andresayac/botmesh/blob/main/packages/core/src/coordination/MemoryCoordinator.ts#L187)

Simulate this replica dying without cleaning up: it stops counting as alive, so its
tickets are skipped and its turns and leaderships are handed over, and every signal
it holds aborts with `'expired'` (what its watchdog would do once the lease lapsed).

#### Returns

`void`

***

### reserve()

```ts theme={null}
reserve(key): Promise<Ticket>;
```

Defined in: [coordination/MemoryCoordinator.ts:204](https://github.com/andresayac/botmesh/blob/main/packages/core/src/coordination/MemoryCoordinator.ts#L204)

Reserve a place in `key`'s cross-replica FIFO. The bot calls it in ingress order.

#### Parameters

| Parameter | Type |
| - | - |
| `key` | `string` |

#### Returns

`Promise`\<[`Ticket`](https://botmesh.afaya.com.co/api/core/spi/interfaces/Ticket)>

#### Implementation of

[`Coordinator`](https://botmesh.afaya.com.co/api/core/interfaces/Coordinator).[`reserve`](https://botmesh.afaya.com.co/api/core/interfaces/Coordinator#reserve)

***

### acquireTurn()

```ts theme={null}
acquireTurn(ticket, opts?): Promise<ChatTurn>;
```

Defined in: [coordination/MemoryCoordinator.ts:217](https://github.com/andresayac/botmesh/blob/main/packages/core/src/coordination/MemoryCoordinator.ts#L217)

Wait until it is `ticket`'s turn (tickets of replicas that died are skipped), then
hold the key. Rejects with [LeaseLostError](https://botmesh.afaya.com.co/api/core/classes/LeaseLostError.md) (`'superseded'`) when the ticket was
skipped because this replica was presumed dead, and with
[CoordinationUnavailableError](https://botmesh.afaya.com.co/api/core/classes/CoordinationUnavailableError.md) on timeout or backend failure.

#### Parameters

| Parameter | Type |
| - | - |
| `ticket` | [`Ticket`](https://botmesh.afaya.com.co/api/core/spi/interfaces/Ticket) |
| `opts?` | [`AcquireTurnOptions`](https://botmesh.afaya.com.co/api/core/spi/interfaces/AcquireTurnOptions) |

#### Returns

`Promise`\<[`ChatTurn`](https://botmesh.afaya.com.co/api/core/spi/interfaces/ChatTurn)>

#### Implementation of

[`Coordinator`](https://botmesh.afaya.com.co/api/core/interfaces/Coordinator).[`acquireTurn`](https://botmesh.afaya.com.co/api/core/interfaces/Coordinator#acquireturn)

***

### lead()

```ts theme={null}
lead(name, opts?): Promise<Leadership>;
```

Defined in: [coordination/MemoryCoordinator.ts:242](https://github.com/andresayac/botmesh/blob/main/packages/core/src/coordination/MemoryCoordinator.ts#L242)

Campaign for `name` until elected (or `opts.signal` aborts, rejecting).

#### Parameters

| Parameter | Type |
| - | - |
| `name` | `string` |
| `opts?` | \{ `signal?`: `AbortSignal`; } |
| `opts.signal?` | `AbortSignal` |

#### Returns

`Promise`\<[`Leadership`](https://botmesh.afaya.com.co/api/core/spi/interfaces/Leadership)>

#### Implementation of

[`Coordinator`](https://botmesh.afaya.com.co/api/core/interfaces/Coordinator).[`lead`](https://botmesh.afaya.com.co/api/core/interfaces/Coordinator#lead)

***

### tryLead()

```ts theme={null}
tryLead(name): Promise<Leadership | null>;
```

Defined in: [coordination/MemoryCoordinator.ts:252](https://github.com/andresayac/botmesh/blob/main/packages/core/src/coordination/MemoryCoordinator.ts#L252)

One election attempt: the leadership, or `null` when another replica leads.

#### Parameters

| Parameter | Type |
| - | - |
| `name` | `string` |

#### Returns

`Promise`\<[`Leadership`](https://botmesh.afaya.com.co/api/core/spi/interfaces/Leadership) | `null`>

#### Implementation of

[`Coordinator`](https://botmesh.afaya.com.co/api/core/interfaces/Coordinator).[`tryLead`](https://botmesh.afaya.com.co/api/core/interfaces/Coordinator#trylead)

***

### on()

```ts theme={null}
on<E>(event, listener): () => void;
```

Defined in: [coordination/MemoryCoordinator.ts:275](https://github.com/andresayac/botmesh/blob/main/packages/core/src/coordination/MemoryCoordinator.ts#L275)

Subscribe to coordinator events; returns an unsubscribe.

#### Type Parameters

| Type Parameter |
| - |
|  `E` *extends* keyof [`CoordinatorEvents`](https://botmesh.afaya.com.co/api/core/interfaces/CoordinatorEvents) |

#### Parameters

| Parameter | Type |
| - | - |
| `event` | `E` |
| `listener` | (`payload`) => `void` |

#### Returns

() => `void`

#### Implementation of

[`Coordinator`](https://botmesh.afaya.com.co/api/core/interfaces/Coordinator).[`on`](https://botmesh.afaya.com.co/api/core/interfaces/Coordinator#on)


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.