# History

Class · extends [`Events`](https://api.playcanvas.com/observer/classes/Events.md)

Source: https://github.com/playcanvas/playcanvas-observer/blob/7b88674dee207b87954eabfb61685546418ad536/src/history.ts#L54

Manages history actions for undo/redo operations. This class keeps track of actions that can be
undone and redone, allowing for complex state management in applications such as editors, games,
or any interactive applications where state changes need to be reversible.

**Example**

```ts
const history = new History();

// Define an action
const action = {
  name: 'draw',
  undo: () => { console.log('Undo draw'); },
  redo: () => { console.log('Redo draw'); }
};

// Add the action to history
history.add(action);

// Perform undo
history.undo();

// Perform redo
history.redo();
```

## Accessors

### canRedo

```ts
get canRedo(): boolean
set canRedo(value: boolean)
```

Gets whether we can redo at this time.

### canUndo

```ts
get canUndo(): boolean
set canUndo(value: boolean)
```

Gets whether we can undo at this time.

### currentAction

```ts
get currentAction(): HistoryAction
```

The current history action.

### executing

```ts
get executing(): number
set executing(value: number)
```

Gets the number of async actions currently executing.

### lastAction

```ts
get lastAction(): HistoryAction
```

The last action committed to the history.

## Methods

### add

```ts
add(action: HistoryAction): boolean
```

Adds a new history action to the stack. If the action has a combine flag and matches the
current action's name, the redo function of the current action is updated. If actions have
been undone before adding this new action, it removes all actions that come after the
current action to maintain a consistent history.

**Parameters**

- `action` ([`HistoryAction`](https://api.playcanvas.com/observer/types/HistoryAction.md)): The action to add.

**Returns** `boolean`: Returns `true` if the action is successfully added, `false` otherwise.

### addAndExecute

```ts
addAndExecute(action: HistoryAction): Promise<void>
```

Adds a new history action and immediately executes its redo function.

**Parameters**

- `action` ([`HistoryAction`](https://api.playcanvas.com/observer/types/HistoryAction.md)): The action.

**Returns** `Promise<void>`: A promise that resolves once the redo function has been executed.

### clear

```ts
clear(): void
```

Clears all history actions.

### redo

```ts
redo(): Promise<void>
```

Redoes the next history action. This retrieves the next action from the history stack and
executes the action's redo function.

**Returns** `Promise<void>`: A promise that resolves once the redo function has been executed.

### undo

```ts
undo(): Promise<void>
```

Undoes the last history action. This method retrieves the current action from the history
stack and executes the action's undo function.

**Returns** `Promise<void>`: A promise that resolves once the undo function has been executed.

## Inherited from [Events](https://api.playcanvas.com/observer/classes/Events.md)

- `new History()`
- `get suspendEvents(): boolean` · `set suspendEvents(value: boolean)`
- `addEmitter(emitter: Events): void`
- `emit(name: string, arg0?: any, arg1?: any, arg2?: any, arg3?: any, arg4?: any, arg5?: any, arg6?: any, arg7?: any): History`
- `on(name: string, fn: HandleEvent): EventHandle`
- `once(name: string, fn: HandleEvent): EventHandle`
- `removeEmitter(emitter: Events): void`
- `unbind(name?: string, fn?: HandleEvent): History`
