Skip to main content

undoable()

undoable(execute, undo): UndoableCommand

Creates an undoable command from an execute/undo pair.


Parameters​

execute: Command​

The action to perform

undo: Command​

The action that reverses execute


Returns: UndoableCommand​

An UndoableCommand


Since​

2.4.0


Example​

const items: string[] = [];

const addItem = (item: string) => undoable(
() => items.push(item),
() => items.pop(),
);

const cmd = addItem("hello");
cmd.execute(); // items = ["hello"]
cmd.undo(); // items = []

UndoableCommand​

Interface

An UndoableCommand pairs an execute action with its inverse. Replaces the GoF Command interface + ConcreteCommand classes.


Since​

2.4.0


Properties​

execute: Command​

undo​

readonly undo: Command

Command()​

Type

Command = () => void

A Command is a thunk - a deferred action with no arguments. In FP, this IS the pattern: a function is already a reifiable value.


Returns: void​


Since​

2.4.0