Welcome to the final chapter! In the previous Local JSX Action Handler chapter, we built the visual interface for our feature. Inside that code, we called a function named saveGlobalConfig to remember that the user had seen the screen.
But we didn't explain how that actually works.
How does an application remember things after you close it?
Computer programs have two types of memory:
let score = 10). It is fast, but it gets wiped clean the moment you close the application.If we only used variables, our application would act like a goldfish.
User opens app.
App: "Welcome! Here is a tour of the new feature."
User closes app.
User opens app again.
App: "Welcome! Here is a tour of the new feature."
This is annoying. We need a permanent memory.
The Global Configuration State is the application's "Diary." It is a file saved on your computer's hard drive.
When the application wants to remember something (like "User has visited the Passes screen"), it writes it into this diary. When the application starts up again, it reads the diary to recall what happened last time.
To see what is in memory, we use getGlobalConfig(). This gives us an object containing all the saved settings.
import { getGlobalConfig } from '../../utils/config.js';
// Read the current state
const config = getGlobalConfig();
// Access a specific memory
if (config.hasVisitedPasses) {
console.log("Welcome back, old friend!");
}
Explanation:
getGlobalConfig(): Instantly returns the current settings.Writing is slightly trickier. Imagine if you tore out all the pages of a diary just to write one new sentence. You would lose everything else!
To save data safely, we use an Updater Pattern. We take the current state, copy it, change one thing, and save the result.
import { saveGlobalConfig } from '../../utils/config.js';
// We pass a function that receives the 'current' state
saveGlobalConfig((current) => ({
// 1. Keep everything else exactly the same
...current,
// 2. Overwrite only the specific flag we want to change
hasVisitedPasses: true,
}));
Explanation:
...current: This is the Spread Operator. It says "Copy all existing properties from the current state." This ensures we don't accidentally delete other settings (like the user's theme or login token).hasVisitedPasses: true: This adds or updates our specific field.Let's look at the real-world scenario from our project. We want to show a "New!" animation only the first time a user visits.
Here is how we combine logic with state:
// Inside our command logic
const config = getGlobalConfig();
// Check the memory. If undefined or false, it's their first time.
const isFirstVisit = !config.hasVisitedPasses;
if (isFirstVisit) {
// TRIGGER LOGIC: Show the onboarding animation
showOnboarding();
// UPDATE MEMORY: Save this so we don't do it next time
saveGlobalConfig(c => ({ ...c, hasVisitedPasses: true }));
}
Result:
hasVisitedPasses is false. Code runs. Config is saved.hasVisitedPasses is true. Code is skipped.How does a JavaScript object turn into a file on your hard drive?
The system acts as a bridge between the fast world of RAM and the permanent world of the Disk.
The core implementation handles the file system operations (reading and writing files). It usually relies on Node.js built-in fs (File System) module.
Here is a simplified version of what saveGlobalConfig does internally:
import fs from 'fs';
// The internal variable (RAM)
let memoryCache = loadConfigFromFile();
export function saveGlobalConfig(updater) {
// 1. Calculate new state based on old state
const newState = updater(memoryCache);
// 2. Update RAM (so next read is fast)
memoryCache = newState;
// 3. Update Disk (so it survives restart)
// We turn the object into a text string
const jsonString = JSON.stringify(newState, null, 2);
fs.writeFileSync('./config.json', jsonString);
}
Why is this important?
memoryCache, immediate subsequent reads are instant. We don't have to wait for the disk write to finish.config.json, we ensure the data exists tomorrow.Congratulations! You have completed the Passes tutorial series. Let's review the architecture you have learned:
index.ts) so the app knows your feature exists.passes.tsx) that cooks up the visual interface using React.You now understand the core building blocks of a high-performance, interactive command-line application. You can use these patterns to build settings pages, complex dashboards, or interactive games right inside the terminal!
End of Tutorial.
Generated by Code IQ