Welcome back! In Chapter 3: Lazy Loading / Dynamic Import, we optimized our application so it fetches the code only when needed.
Now that the application has successfully loaded our code from the "archive," it says: "Okay, I have the file. What do I do now?"
In this chapter, we will explore the JSX Command Handler. This is the logic that actually puts pixels on the screen.
Most command-line tools just output text. You type date, and it prints Mon Jan 1.
But we want to build a rich Graphical User Interface (GUI). We want buttons, tabs, and layout. We can't just return "Hello World"; we need to return a visual component. Furthermore, this component needs to know when to appear and when to disappear.
The Use Case: When the user runs the "Usage" command, we need to:
context).onDone).To understand the Command Handler, imagine a Stage Play.
call function. It tells the actor what to do immediately after "Action!" is called.context.onDone callback is your cue to bow and exit.
If you don't use onDone, the curtain never falls, and the app gets stuck!
The "Handler" is simply the function named call inside our file usage.tsx. Let's break down its lifecycle parts.
When the handler starts, the system passes it a context object. This contains environment data.
// usage.tsx
export const call: LocalJSXCommandCall = async (onDone, context) => {
// Example: We can access data from the system here
console.log("Current User ID:", context.userId);
// ... continue to render
};
Explanation:
context: Think of this as a backpack full of tools the main app gives you. It might contain the current user's name, the active color theme, or API keys.Unlike a standard function that calculates a number, this handler returns a React Element (JSX).
// usage.tsx
// ... inside the call function
// We return a Component. This is what the user SEES.
return (
<Settings
context={context}
defaultTab="Usage"
// ... more props
/>
);
Explanation:
<Settings> panel on the screen for me."This is the most critical part of the interaction. The UI is interactive; it stays open while the user clicks around. We need a way to close it.
// usage.tsx
export const call: LocalJSXCommandCall = async (onDone, context) => {
// We pass 'onDone' down to our component as a prop named 'onClose'
return <Settings onClose={onDone} context={context} defaultTab="Usage" />;
};
Explanation:
onDone: This is a function provided by the system. Calling it means "I am finished."onDone() immediately. We pass it to the <Settings /> component. The component will call it when the user clicks a specific "Close" button inside the UI.What happens when the Director yells "Action!"?
The system creates a container (a "Stage") for your component. It watches that container. When onDone is finally triggered, the system dismantles the stage and frees up memory.
Here is the lifecycle of a command execution:
Inside the framework, there is code that wraps your handler to manage this lifecycle. It looks something like this (simplified):
// Framework Internal Code (Simplified)
async function executeCommand(commandModule, appData) {
return new Promise((resolve) => {
// 1. Define the Exit Strategy
const onDone = () => {
removeUIFromScreen(); // Clear the screen
resolve(); // Tell the app the command is finished
};
// 2. Run the Handler (Your code!)
const uiElement = commandModule.call(onDone, appData);
// 3. Render the result
renderOnScreen(uiElement);
});
}
Explanation:
Promise: The system pauses other background tasks while your command is active.onDone definition: The system creates the onDone function before it even calls you. This function handles the cleanup (removing the window).renderOnScreen: Takes the JSX you returned and actually attaches it to the application's DOM (Document Object Model).In this chapter, we learned that the JSX Command Handler is the script that controls the interaction.
context to understand the environment.onDone to control when the interaction ends.We have now covered the Contract (safety), the Registration (discovery), the Loading (performance), and the Handler (execution).
However, in our code, we returned a component called <Settings />. We haven't looked at that component yet. How does the component know how to arrange the buttons or style the text?
Next Chapter: View Delegation / UI Configuration
Generated by Code IQ