UNPKG

@synet/patterns

Version:

Robust, battle-tested collection of stable patterns used in Synet packages

53 lines (52 loc) 1.25 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.CommandInvoker = void 0; /** * Manages the execution of commands and keeps track of command history */ class CommandInvoker { constructor() { this.history = []; } /** * Executes a command */ execute(command) { command.execute(); if (this.isUndoable(command)) { this.history.push(command); } } /** * Undoes the most recently executed command * @returns true if a command was undone, false if there was no command to undo */ undo() { const command = this.history.pop(); if (command) { command.undo(); return true; } return false; } /** * Clears the command history */ clearHistory() { this.history = []; } /** * Returns the number of undoable commands in history */ historyLength() { return this.history.length; } /** * Type guard to check if a command is undoable */ isUndoable(command) { return ("undo" in command && typeof command.undo === "function"); } } exports.CommandInvoker = CommandInvoker;