Js command pattern

From Intense-Visions/harness-engineering · View interactive version

Js command pattern is an AI agent skill from the GitHub repository Intense-Visions/harness-engineering. > Encapsulate operations as objects to support undo, queue, and logging

How to use this skill

Skill file

JS Command Pattern

Encapsulate operations as objects to support undo, queue, and logging

When to Use

Instructions

  1. Define a command interface with execute() and optionally undo() methods.
  2. Each command class captures the receiver and parameters needed to perform (and reverse) the operation.
  3. Store executed commands in a history stack for undo support.
  4. The invoker (button, menu, keyboard shortcut) calls command.execute() without knowing the implementation.
class AddTextCommand {
  constructor(editor, text) {
    this.editor = editor;
    this.text = text;
    this.prevContent = null;
  }

  execute() {
    this.prevContent = this.editor.content;
    this.editor.content += this.text;
  }

  undo() {
    this.editor.content = this.prevContent;
  }
}

const editor = { content: 'Hello' };
const history = [];

const cmd = new AddTextCommand(editor, ' World');
cmd.execute();
history.push(cmd);
console.log(editor.content); // 'Hello World'

history.pop().undo();
console.log(editor.content); // 'Hello'

Details

The Command pattern turns function calls into first-class objects. This makes operations serializable, loggable, and reversible. Redux actions are a functional variant of this pattern.

Trade-offs:

When NOT to use:

Source

https://patterns.dev/javascript/command-pattern

Process

  1. Read the instructions and examples in this document.
  2. Apply the patterns to your implementation, adapting to your specific context.
  3. Verify your implementation against the details and edge cases listed above.

Harness Integration

Success Criteria

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.