Welcome back! In the previous chapter, Stub Implementation, we learned that our library currently relies on a "Stub"โa placeholder object that prevents crashes but doesn't perform real logic.
In this chapter, we are going to look at how we interact with that stub. Even though the machine isn't working yet, we have already installed two different buttons to operate it.
Imagine you have written a program to "Reset Limits" on your server.
The Problem: At 3:00 AM, the script runs. The code tries to pop up the "Are you sure?" window. Since you are asleep, no one clicks "Yes." The script freezes, waiting forever. The server crashes.
The Solution: We need two distinct ways to trigger the same action:
Think of a high-tech vending machine.
In our project's current state (The Stub), the vending machine is unplugged.
However, the buttons exist. We have reserved the space for these two different behaviors right from the start.
By separating these two modes now, we force the developer to state their intent.
resetLimits)resetLimitsNonInteractive)Even though both exports currently point to the generic stub, using the correct one makes your code readable and future-proof.
You are building a button for a website.
import { resetLimits } from './index.js';
// A user clicks a button on the screen
function onUserClick() {
// We expect this might show a popup eventually
const system = resetLimits;
console.log("User triggered mode:", system.name);
}
Output:
User triggered mode: stub
You are writing a background worker.
import { resetLimitsNonInteractive } from './index.js';
// A timer runs this code automatically
function onTimerTick() {
// We promise NOT to show popups here
const system = resetLimitsNonInteractive;
console.log("Script triggered mode:", system.name);
}
Output:
Script triggered mode: stub
Explanation: Even though the output (stub) is the same for both, the code tells a story. A deeper look at the code reveals that the developer intended for one to be manual and the other to be automated.
How do we support two modes with only one stub? We use Aliasing.
Currently, our library doesn't actually have the logic for popups or background scripts. We just want to expose the names so developers can start typing valid code.
Here is what happens when different parts of your application ask for the library.
Let's look at the index.js file. This completes the puzzle we started in Unified Interface Exports.
// --- File: index.js ---
// 1. Define the Stub (The "Unplugged Machine")
const stub = { isEnabled: () => false, isHidden: true, name: 'stub' };
// 2. Export for Interactive Mode
export const resetLimits = stub;
// 3. Export for Non-Interactive Mode
export const resetLimitsNonInteractive = stub;
// 4. Default Export
export default stub;
Breakdown:
const stub = ...: We create the object once.export const resetLimits = stub;: We give the stub a name intended for human interaction.export const resetLimitsNonInteractive = stub;: We give the same stub a different name intended for robots.In the future, when we replace the Stub with real code, we will change these lines to point to different functions. But for now, they share the same placeholder.
By implementing Operational Modes, we have designed a mature API surface.
You have now completed the beginner tutorial for the reset-limits structure! You understand how a complex library can be set up using simple placeholders to ensure reliability, safety, and clear design before a single line of complex logic is written.
Generated by Code IQ