πŸ“ commands/output-style/ Β· 04_command_handler_implementation.md

Chapter 4: Command Handler Implementation

πŸ“„ commands/output-style/04_command_handler_implementation.md

Chapter 4: Command Handler Implementation

Welcome to the fourth and final chapter of the output-style project tutorial!

In the previous chapter, Chapter 3: Lazy Loading Strategy, we learned how the system efficiently fetches the code file from the "library" only when needed.

Now that the file is loaded into memory, the system needs to know exactly which function to run to make the command happen. It's time to look at the specific instructions that execute the task.

The Concept: The Recipe

Let's revisit our restaurant analogy one last time:

  1. Menu (Chapter 1): The user points to "Output Style".
  2. Library (Chapter 3): The kitchen staff retrieves the specific cookbook for that item.
  3. Waiter (Chapter 2): The person ready to carry the result to the table.

The Command Handler is the Recipe itself. It is the step-by-step set of instructions that the chef follows to prepare the dish. In our code, this recipe is contained within a specific function named call.

The Use Case

The system has loaded output-style.tsx. It needs a standard entry pointβ€”a "Start Here" button. We cannot just write code loosely in the file; we must wrap it in a function that the system expects.

The goal of this chapter is to understand the structure of the call function, which acts as the "Worker" that ties everything together.


The Anatomy of the Handler

To implement a command handler, we define an exported function. Let's break down the code in output-style.tsx.

Step 1: The Function Signature

The system expects a specific setup. It looks for a function named call.

// File: output-style.tsx

import type { LocalJSXCommandOnDone } from '../../types/command.js';

// We export 'call' so the system can see it
export async function call(onDone: LocalJSXCommandOnDone) {
  
  // Logic goes here...

}

Explanation:

Step 2: Accepting Tools

Notice the argument inside the parenthesis: (onDone: LocalJSXCommandOnDone).

When the system runs your command, it hands you a toolbox. In this specific command type (local-jsx), the most important tool is onDone.

Step 3: Execution (The Logic)

Inside the curly braces { ... } is where the actual work happens.

// Inside the call function...

  // We use the tool provided to us
  onDone(
    '/output-style has been deprecated. Use /config...', 
    { display: 'system' }
  );

  // The function reaches the end and finishes

Explanation:


Under the Hood: The Execution Lifecycle

How does the main application manage this handler? It follows a strict lifecycle to ensure the command runs safely.

The Sequence

Here is what happens the moment the file is loaded and the system is ready to run the command:

sequenceDiagram participant System as Main App participant Handler as call() Function participant User Note over System: 1. File is loaded System->>Handler: Invokes call(onDone) Note over Handler: 2. Handler starts working Handler->>Handler: Prepares message string Handler->>System: 3. Calls onDone(message) System->>User: Displays message Handler-->>System: 4. Function completes (Promise resolves) Note over System: 5. Command marked as "Finished"

Why is this abstraction useful?

By forcing every command to use a standard export async function call, the system doesn't need to know what the command does.


Putting It All Together

We have now covered the entire lifecycle of the output-style command across four chapters.

Let's look at the complete picture of what we built:

  1. Registration (Chapter 1):

We created an ID card in index.ts so the system knows the command name is output-style.

  1. Lazy Loading (Chapter 3):

We told the system to wait and only download output-style.tsx when the user actually asks for it.

  1. Handler Implementation (This Chapter):

We defined export async function call(...) as the standard entry point for our logic.

  1. Feedback (Chapter 2):

Inside that handler, we used onDone to safely send a result back to the user interface.

Final Code Review

Here is the complete file output-style.tsx one last time, with your new understanding of all the parts:

import type { LocalJSXCommandOnDone } from '../../types/command.js';

// The Handler (Chapter 4)
export async function call(onDone: LocalJSXCommandOnDone) {
  
  // The Feedback System (Chapter 2)
  onDone(
    '/output-style has been deprecated...', 
    { display: 'system' }
  );
  
}

Conclusion

Congratulations! You have completed the tutorial on the output-style project.

You now understand how a modern command-line application structure works:

You are now ready to create your own commands by following this same pattern!


Generated by Code IQ