Welcome to the final chapter of our beginner tutorial!
In the previous Declarative UI Composition chapter, we learned how to paint our user interface onto the screen. We successfully displayed the "Help" menu.
But now we have a new problem. How do we leave?
If you have ever been stuck in a computer program where pressing Esc, q, or Ctrl+C didn't work, you know the frustration. The program is "hanging." We need a way to tell the main system, "We are finished here, please return to the main menu."
This concept is called Lifecycle Management.
Imagine a hotel.
If you never return the key, the hotel thinks you are still there. The cleaners can't enter, and the front desk can't give the room to a new guest. The system is blocked.
In our software:
We need a standardized way to hand back the key.
onDone Callback
In our system, the "Key" is a function called onDone.
When the system runs your command, it hands you this function. It says:
"Here is a special button. You can keep the screen as long as you want. But when you are finished, please press this button so I know I can take over again."
Let's look at help.tsx one last time. We need to receive the key (onDone) and pass it to our UI so the user can click it.
In Chapter 1, we saw the arguments for our function. Now let's focus specifically on the first one.
// help.tsx
export const call: LocalJSXCommandCall = async (
onDone, // <--- This is the "Check Out" button
context
) => {
// ...
};
Explanation:
The variable onDone is a function. We didn't write it; the system wrote it and gave it to us. We just need to hold onto it.
The logic layer (the call function) doesn't have a mouse or keyboard. It can't "click" anything. It must pass the key to the component that can handle user input: <HelpV2 />.
// help.tsx
// ... imports
export const call: LocalJSXCommandCall = async (onDone, { options }) => {
const { commands } = options;
// We pass 'onDone' to the 'onClose' prop
return <HelpV2 commands={commands} onClose={onDone} />;
};
Explanation: We act as a messenger.
onDone to call.call gives onDone to HelpV2 (renaming it onClose because that makes more sense for a UI button).
Inside the visual component (HelpV2), there is likely code that listens for a keypress (like Esc) or a mouse click on an "X" button.
// Inside the hypothetical HelpV2 component
function HelpV2(props) {
// When user presses 'Esc'...
useInput((input) => {
if (input === 'Escape') {
// Call the function passed down from the parent
props.onClose();
}
});
return <Text>Press Esc to Close</Text>;
}
Explanation:
When props.onClose() is called inside the UI, it triggers onDone() inside our logic, which signals the System. The chain is complete.
What actually happens inside the system when you call onDone()? It triggers a cleanup process.
onDone.Here is a simplified version of how the system "waits" for you to finish.
// Hypothetical System Core
function executeCommand() {
return new Promise((resolve) => {
// We create the onDone function
const onDone = () => {
console.log("Room is free!");
resolve(); // This unblocks the system
};
// We pass it to your command
yourCommand.call(onDone, context);
});
}
Explanation: The system wraps the entire execution in a Promise.
onDone function effectively resolves that Promise.await executeCommand() line in the main system stays paused.You might ask: "Why can't I just kill the process?"
If you force-quit a program, you might lose data, corrupt files, or leave database connections open. By using a controlled Lifecycle (onDone), we ensure:
Congratulations! You have completed the Help Project Tutorial.
Let's recap our journey:
import() to fetch the code from storage only when requested (The Librarian).onDone to politely signal when we are finished, ensuring a smooth user experience.You now understand the core architecture of a modular, scalable command-line application. You can add new commands, build complex UIs, and manage system resources efficiently.
Happy Coding!
Generated by Code IQ