Terminology (3)Terms used in this lesson
Term on the front, definition on the back. Paste with tabs between fields and a new line between cards, or import the download in Anki as Basic.
- Exit target
- The iteration, loop, switch or function boundary selected by a control transfer.
- Processed prefix
- The part of a row reached before its stopping code.
- Provisional work
- Work whose effect is not yet allowed to appear in the committed state.
Transfer targets and completed work
continue skips the rest of an iteration, break leaves a targeted enclosing construct, and return leaves the current function. In nested code, first locate those boundaries: leaving one row loop, its batch loop and the helper call are different actions.
An early exit also needs a state policy. A running calculation may contain provisional work that should become caller-visible only after a complete unit succeeds. Follow both the next statement and the writes already performed when deciding what an interrupted operation reports.
01Raw diagnostic
Does a zero end the raw scan?
The telemetry importer reads a legacy integer-frame format. Operators need a raw sum for comparing input captures and a decoded unit report that respects row boundaries. A batch cancellation can arrive halfway through a row, so the committed report must describe only completed work.
The raw diagnostic sums every integer word, before interpreting control markers. This reduced row contains a zero between two nonzero values. Trace the transfer in its zero branch and predict the total.
let total = 0;
for (const value of [2, 0, 5]) {
if (value === 0) continue;
total += value;
}
console.log("total", total);
npx tsc main.ts --noEmit --skipLibCheck --target es2022 --module nodenext --types node && node main.ts
Save one complete example at a time under the shown filename in a fresh folder. Run the shown command. The browser lab supplies its editor, input fixtures and runner. Supplied rows have well-formed lengths and small integers; every intermediate sum stays exact and in range for the numeric types shown. The readers assume these inputs rather than validate them.
02Decoded row boundary
Which scope does a row end leave?
The decoded view must stop one row at its marker while leaving the batch available. The first row here has a negative code and an encoded suffix, followed by another row. Predict the total from the actual transfer.
const rows = [[2, -1, 9], [3]];
let total = 0;
for (const row of rows) {
for (const value of row) {
if (value < 0) break;
total += value;
}
}
console.log("total", total);
03Caller-visible state
What remains after the helper returns?
The importer exposes a running total to helpers and reports it after they finish. Before adding cancellation, inspect this smaller helper call: it changes a caller-visible value and returns. Predict what the caller reads.
const state = { total: 10 };
function visit(): void {
state.total += 4;
return;
}
visit();
console.log("total", state.total);
04Practice
Repair the batch desk
Continue the telemetry importer with raw diagnostics, a decoded row report and a committed batch report. Input begins with a row count; each row supplies its encoded length and integer words. The complete caller, parser and State type are supplied.
The raw checksum includes every code. Row totals stop at the first negative. Final processing treats -1 as row end and -2 as cancellation: discard that row’s units, keep completed rows, set cancelled and stop the batch. Codes after a row end are not reached.
Start the lab
Preserve every raw word, report each decoded row, then cancel without committing unfinished work.
In the frontend Returning from a handler does not undo its state update Optional
The import button records a checking status before testing whether an import can start. Follow the handler with canStart false. The return stops the handler; inspect the status operation that has already happened.
"use client";
import { useState } from "react";
export default function ImportButton({ canStart }: { canStart: boolean }) {
const [status, setStatus] = useState("idle");
function begin() {
setStatus("checking");
if (!canStart) return;
setStatus("queued");
}
return <button onClick={begin}>{status}</button>;
}
setStatus queues an update for React’s next render; it does not synchronously change the status value captured by this handler. With canStart false, returning leaves the checking update queued. It does not queue a reset to idle. This is a handler return, unlike a component return that selects its rendered output.
Sources: React: queueing state updates,Svelte: $state,Svelte: event attributes.
Sign in to keep your read marks on every device. Sign in
Was this helpful?