# ActionManager
Source: https://docs.maelstrom-ai.co/dev/reference/client/classes/actionmanager/
Documentation revision: 89a8f5e9d10cedcf0b30376d7846113f06f26120
SDK manifests in this build: @maelstrom-co/react@0.2.1, @maelstrom-co/client@0.2.1, @maelstrom-co/connectors@0.2.1, @maelstrom-co/protocol@0.2.1, @maelstrom-co/engine@0.2.1
These docs follow this source revision. Manifest versions do not prove release compatibility; verify the installed package exports before using an API.

Defined in: [packages/client/src/lib/command-processor/action-manager.ts:185](https://github.com/maelstrom-co/maelstrom/blob/89a8f5e9d10cedcf0b30376d7846113f06f26120/packages/client/src/lib/command-processor/action-manager.ts#L185)

ActionManager handles action execution for vessel instances.

Components register typed action runners per instance to handle actions. Actions are executed:

-   In parallel across different instances
-   Sequentially within the same instance (respects executionPlan order)

The manager includes a waitForReady mechanism to handle race conditions where the server sends actions before components have registered their runners.

Given [VariantTransitions](https://docs.maelstrom-ai.co/dev/reference/client/interfaces/varianttransitions/), each Action resolves against the Instance’s Variant at its turn and applies its `transitionsTo` after the handler succeeds. An Action declared effect `'none'` with no handler applies its transition on its own, but only once the Instance has a registered runner. Any other Action without a handler is skipped and changes no Variant. Without them, handlers are looked up by name alone and no Variant changes.

## Example

```typescript
const store = new Store(createInitialState());
const actionManager = new ActionManager();

// Register typed action handlers for a specific instance
actionManager.registerRunner('instance-1', {
  selectOrder: async (params) => {
    // params is fully typed based on vessel definition
    setSelectedOrderId(params.orderId);
    return ok(undefined);
  },
  closeOrderDetails: async () => {
    setSelectedOrderId(null);
    return ok(undefined);
  },
});

// Execute actions from a server response
const result = await actionManager.executeActions(response.executionPlan);
console.log(`Executed ${result.actions.length} actions`);
```

## Constructors

### Constructor

> **new ActionManager**(`options?`): `ActionManager`

Defined in: [packages/client/src/lib/command-processor/action-manager.ts:193](https://github.com/maelstrom-co/maelstrom/blob/89a8f5e9d10cedcf0b30376d7846113f06f26120/packages/client/src/lib/command-processor/action-manager.ts#L193)

#### Parameters

##### options?

###### executionTimeoutMs?

`number`

###### logger?

[`Logger`](https://docs.maelstrom-ai.co/dev/reference/client/classes/logger/)

###### readinessTimeoutMs?

`number`

#### Returns

`ActionManager`

## Methods

### dispose()

> **dispose**(): `void`

Defined in: [packages/client/src/lib/command-processor/action-manager.ts:480](https://github.com/maelstrom-co/maelstrom/blob/89a8f5e9d10cedcf0b30376d7846113f06f26120/packages/client/src/lib/command-processor/action-manager.ts#L480)

Release the manager: cancel every pending readiness wait so no timer outlives disposal, and settle its waiters as not-ready.

Waiters resolve `false` rather than rejecting so an in-flight [executeActions](https://docs.maelstrom-ai.co/dev/reference/client/classes/actionmanager/#executeactions) still returns its actions as skipped instead of surfacing a rejection its callers do not expect. A disposed manager stays silent and never schedules another readiness timer.

#### Returns

`void`

* * *

### executeActions()

> **executeActions**(`executionPlan`, `observe?`, `transitions?`): `Promise`<[`BatchExecutionResult`](https://docs.maelstrom-ai.co/dev/reference/client/interfaces/batchexecutionresult/)\>

Defined in: [packages/client/src/lib/command-processor/action-manager.ts:371](https://github.com/maelstrom-co/maelstrom/blob/89a8f5e9d10cedcf0b30376d7846113f06f26120/packages/client/src/lib/command-processor/action-manager.ts#L371)

Execute all actions in the execution plan.

Actions are grouped by instance and executed:

-   In parallel across different instances
-   Sequentially within the same instance (respects executionPlan order)

Waits for runners to be ready before executing. Actions are skipped if no runner is registered within the timeout, including Actions that only transition: an Instance whose runner never registers changes no Variant.

With `transitions`, each Action in turn:

1.  Reads the Instance’s runner, waiting for readiness again if it went away mid-sequence. A runner that never returns skips the Action as `readiness`.
2.  Resolves against the Instance’s current Variant. An Action not defined there is skipped as `application`, with a `reason`.
3.  Runs its handler, if the runner has one. A failed handler applies no transition. With no handler, only an Action declared effect `'none'` continues. Any other is skipped as `application`, with a `reason`, and applies no transition.
4.  Applies its `transitionsTo` with compare-and-set against the Variant from step 2. An effect `'none'` transition with no handler counts as applied. A target equal to the current Variant, or one the Vessel does not define, applies nothing. A lost compare-and-set is not an error.

With no handler, an effect `'none'` Action that declares no transition or transitions to a Variant the Vessel does not define is skipped as `application`, with a `reason`. One whose target equals the current Variant succeeds without a change.

Without `transitions`, handlers are looked up by name alone and no Variant changes.

#### Parameters

##### executionPlan

readonly `ActionExecution`\[\]

Array of actions to execute

##### observe?

(`progress`) => `void`

Receives readiness and handler start progress

##### transitions?

[`VariantTransitions`](https://docs.maelstrom-ai.co/dev/reference/client/interfaces/varianttransitions/)

Resolves Actions per Variant and applies transitions

#### Returns

`Promise`<[`BatchExecutionResult`](https://docs.maelstrom-ai.co/dev/reference/client/interfaces/batchexecutionresult/)\>

Result of the batch execution

* * *

### getRegisteredInstances()

> **getRegisteredInstances**(): `InstanceId`\[\]

Defined in: [packages/client/src/lib/command-processor/action-manager.ts:264](https://github.com/maelstrom-co/maelstrom/blob/89a8f5e9d10cedcf0b30376d7846113f06f26120/packages/client/src/lib/command-processor/action-manager.ts#L264)

Get all registered instance IDs.

#### Returns

`InstanceId`\[\]

Array of instance IDs with registered runners

* * *

### hasRunner()

> **hasRunner**(`instanceId`): `boolean`

Defined in: [packages/client/src/lib/command-processor/action-manager.ts:255](https://github.com/maelstrom-co/maelstrom/blob/89a8f5e9d10cedcf0b30376d7846113f06f26120/packages/client/src/lib/command-processor/action-manager.ts#L255)

Check if a runner is registered for an instance.

#### Parameters

##### instanceId

`InstanceId`

The branded instance ID to check

#### Returns

`boolean`

true if a runner is registered

* * *

### registerRunner()

> **registerRunner**<`V`\>(`instanceId`, `runner`): [`Unsubscribe`](https://docs.maelstrom-ai.co/dev/reference/client/type-aliases/unsubscribe/)

Defined in: [packages/client/src/lib/command-processor/action-manager.ts:219](https://github.com/maelstrom-co/maelstrom/blob/89a8f5e9d10cedcf0b30376d7846113f06f26120/packages/client/src/lib/command-processor/action-manager.ts#L219)

Register an action runner for a specific instance. The runner is an object with typed action handlers.

#### Type Parameters

##### V

`V` _extends_ `VesselDefinition`

#### Parameters

##### instanceId

`InstanceId`

The branded instance ID this runner handles

##### runner

[`ActionRunner`](https://docs.maelstrom-ai.co/dev/reference/client/type-aliases/actionrunner/)<`V`\>

Object with action name -> handler function mapping

#### Returns

[`Unsubscribe`](https://docs.maelstrom-ai.co/dev/reference/client/type-aliases/unsubscribe/)

#### Example

```typescript
actionManager.registerRunner(InstanceId('instance-1'), {
  selectOrder: async (params) => {
    setSelectedOrderId(params.orderId);
    return ok(undefined);
  },
});
```

* * *

### waitForReady()

> **waitForReady**(`instanceId`, `timeoutMs?`): `Promise`<`boolean`\>

Defined in: [packages/client/src/lib/command-processor/action-manager.ts:286](https://github.com/maelstrom-co/maelstrom/blob/89a8f5e9d10cedcf0b30376d7846113f06f26120/packages/client/src/lib/command-processor/action-manager.ts#L286)

Wait for a runner to be registered for an instance. This handles the race condition where the server sends actions before React components have mounted and registered their runners.

#### Parameters

##### instanceId

`InstanceId`

The branded instance ID to wait for

##### timeoutMs?

`number` = `5000`

Maximum time to wait (default: 5000ms)

#### Returns

`Promise`<`boolean`\>

Promise that resolves to true if runner registered, false if the wait timed out or the manager was disposed

#### Example

```typescript
const isReady = await actionManager.waitForReady(InstanceId('instance-1'), 3000);
if (isReady) {
  // Runner is available, actions can execute
}
```
