Terminology (4)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.
- View
- A range description that accesses existing elements without owning a separate copy of them.
- Owner
- The object or storage responsible for the lifetime of the elements being selected.
- Local coordinate
- A position within a selection, translated to a position in its underlying owner.
- Independent copy
- Separate element storage whose numeric cells do not change when the original cells are changed.
A range and its storage
A view describes a range of existing storage without owning an independent copy of its elements. A copied range instead supplies separate elements. The two can initially display identical values, so their contents alone do not tell you which relationship was created.
A range also has local coordinates: its first position corresponds to some position in the underlying owner. Follow both that owner and the range boundaries when reading or writing. The selected native API determines how bounds are expressed, what is copied and how long a view remains usable.
01Local and owner coordinates
Locate the samples under the selection
A scanline editor lets you work on a window of intensity samples without repainting the whole row. Its comparison proof must keep the samples from before a write. Extending the image with a prefix then raises another question: which row and positions does the paint window still name?
The editor receives a starting position and a count. Before it can total or paint those samples, check how that request maps into the native range API. This probe selects two cells beginning at owner position 1.
const owner = new Int32Array([3, 8, 5, 1]);
const start = 1;
const count = 2;
const window = owner.subarray(start, start + count);
console.log("window", ...window);
npx tsc main.ts --noEmit --skipLibCheck --target es2022 --module nodenext --types node && node main.ts
Save one complete example at a time in a fresh folder under the selected filename. Run the shown checking/build command before execution. The browser lab supplies its editor, stdin fixtures and native runner.
02Shared cells or an independent proof
Keep a comparison while a window is painted
The editor first tries saving a whole-row proof while painting a smaller window. Inspect the constructions of proof and window, then predict which report follows the write.
const owner = new Int32Array([3, 8, 5]);
const proof = owner.slice();
const window = owner.subarray(1, 3);
window[0] = 9;
console.log("owner", owner[1], "proof", proof[1]);
03Replacement owners and view provenance
Check which row a retained window edits
The editor now constructs a prefixed row while keeping the current row available for comparison. The probe still holds a window selected from owner. Predict what happens when that window is painted.
const owner = new Int32Array([3, 8, 5]);
const prefixed = new Int32Array([7, ...owner]);
const window = owner.subarray(1, 2);
window[0] = 9;
console.log("original", owner[1], "prefixed", prefixed[2]);
04Practice
Repair the scanline editor
Continue the scanline tool: total the requested window, preserve a proof of only those samples, then paint their corresponding positions after a prefix creates a replacement row.
The supplied caller saves the selected proof and changes the first selected working cell before requesting the replacement edit. Preserve the row as received by that operation. Inputs are well formed, ranges fit their owners, and samples, edits and intermediate totals fit the shown integer types. These are supplied assumptions, not validation or clipping requirements.
Start the lab
Keep the selected extent, the saved comparison and the replacement edit consistent across the row reports.
In the frontend A typed-array view shares a buffer; framework publication does not change that relationship. Optional
A shade editor must edit the replacement buffer and publish that replacement. Mutating a retained subarray can instead change an older state object. Typed arrays do not become independent merely because a framework stores them.
'use client';
import { useState } from 'react';
export function Shades() {
const [shades, setShades] = useState(() => new Int32Array([3, 8, 5]));
function paint() {
setShades((previous) => {
const next = previous.slice();
const window = next.subarray(1, 2);
window[0] += 1;
return next;
});
}
return <button onClick={paint}>{Array.from(shades).join(', ')}</button>;
}
slice creates a replacement buffer; the window is selected from that buffer. The updater returns the replacement so the previous state remains unchanged.
Sources: React: Updating arrays in state,Svelte: deeply reactive state boundaries.
Sign in to keep your read marks on every device. Sign in
Was this helpful?