Welcome to the third chapter of the output-style tutorial!
In the previous chapter, Chapter 2: Feedback/Output System, we learned how our command talks back to the user using the "Waiter" (onDone).
However, we skipped a very important question: When does the code for our command actually get loaded into the computer's memory?
Imagine you are walking into a massive library. This library has millions of books (commands).
In software, this is called Lazy Loading.
Our application might have hundreds of commands. If we load the code for all of them the moment the app starts, the user will stare at a loading screen for 10 seconds. That is a bad experience.
We want the app to start instantly. To do that, we leave the heavy code sitting on the hard drive until the user specifically asks for it.
We implement this strategy inside our definition file (index.ts). We use a special JavaScript feature called Dynamic Imports.
Let's look at the output-style definition again. Pay attention to the load property.
// File: index.ts
const outputStyle = {
// ... name and description ...
// THE LAZY LOADING MAGIC:
load: () => import('./output-style.js'),
} satisfies Command
Explanation:
load: This is a function that the system calls only when needed.() => ...: This is an arrow function. It wraps the import so it doesn't run immediately.import(...): This tells the computer to go find the file ./output-style.js and load it into memory right now.To understand why this works, compare these two styles:
The Slow Way (Static Import):
// At the very top of the file
import myHeavyCommand from './heavy-file.js';
// This runs immediately when the app starts!
The Fast Way (Dynamic Import):
// Inside a function
const loadMyCommand = () => import('./heavy-file.js');
// This code sits here doing nothing until we call loadMyCommand()
By putting the import inside a function (the load property), we delay the heavy lifting.
What happens when a user actually types /output-style? The system has to act fast to fetch the code.
Here is the sequence of events that turns a "Menu Item" into running code:
In programming, reading a file from the hard drive takes time (even if it's just milliseconds). Because of this, the import() function returns a Promise.
Think of a Promise like a restaurant pager/buzzer:
This all happens so fast the user usually doesn't notice, but it saves massive amounts of memory.
Once the file is loaded via import('./output-style.js'), the system gets access to the functions exported inside it.
If you remember from Chapter 2: Feedback/Output System, our logic file looked like this:
// File: output-style.tsx (The target file)
export async function call(onDone) {
// ... logic here
}
The load function in index.ts essentially hands this entire file over to the main application. The application then looks for the function named call and runs it.
You have now learned the Lazy Loading Strategy.
load: () => import(...) to fetch code only when the user types the command.At this point, we have:
The final piece of the puzzle is understanding exactly how the system orchestrates running that loaded call function and manages the lifecycle of the command execution.
Next Chapter: Command Handler Implementation
Generated by Code IQ