Composite command that groups multiple commands into a single undo/redo unit. Useful for batch operations where multiple matches are modified together. When undone/redone, all grouped commands are processed together.
export class BatchCommand implements Command { private readonly metadata: CommandMetadata; constructor( private readonly commands: Command[], description: string = "Batch operation", ) { const allTitles = commands.flatMap( (cmd) => cmd.getMetadata().affectedTitles, ); this.metadata = { type: CommandType.BATCH_OPERATION, timestamp: Date.now(), affectedTitles: allTitles, description: `${description} (${commands.length} items)`, }; } execute(): void { for (const cmd of this.commands) { cmd.execute(); } } undo(): void { // Undo in reverse order to maintain consistency for (let i = this.commands.length - 1; i >= 0; i--) { this.commands[i].undo(); } } getDescription(): string { return this.metadata.description; } getMetadata(): CommandMetadata { return this.metadata; }} Copy
export class BatchCommand implements Command { private readonly metadata: CommandMetadata; constructor( private readonly commands: Command[], description: string = "Batch operation", ) { const allTitles = commands.flatMap( (cmd) => cmd.getMetadata().affectedTitles, ); this.metadata = { type: CommandType.BATCH_OPERATION, timestamp: Date.now(), affectedTitles: allTitles, description: `${description} (${commands.length} items)`, }; } execute(): void { for (const cmd of this.commands) { cmd.execute(); } } undo(): void { // Undo in reverse order to maintain consistency for (let i = this.commands.length - 1; i >= 0; i--) { this.commands[i].undo(); } } getDescription(): string { return this.metadata.description; } getMetadata(): CommandMetadata { return this.metadata; }}
Composite command that groups multiple commands into a single undo/redo unit. Useful for batch operations where multiple matches are modified together. When undone/redone, all grouped commands are processed together.
Source