The state graph

2 min·Assumes: state, computed, events

The
graph is a map, written at compile time, of which
each spot on the page reads.
import { computed, state } from '@markless/core';

const SONGS = ['Harbour Lights', 'Slow Train', 'Northbound'];

export default function Player() @{
	let playing = state('Harbour Lights');
	let liked = state(new Set<string>());
	let volume = state(6);
	const isLiked = computed(() => liked.has(playing));
	const title = computed(() => playing.toUpperCase());
	const cover = computed(() => '/covers/' + playing + '.jpg');

	<section>
		<h2>{title}</h2>
		<button
			class={isLiked ? 'heart on' : 'heart'}
			onClick={() => (liked = new Set([...liked, playing]))}
		>
			♥
		</button>
		<ul>
			@for (const song of SONGS; key song) {
				<li
					class={song === playing ? 'row on' : 'row'}
					onClick={() => (playing = song)}
				>
					{song}
				</li>
			}
		</ul>
		<img src={cover} alt="" />
		<label>
			Volume {volume}
			<input
				type="range"
				max="10"
				onInput={(e) => (volume = Number(e.target.value))}
			/>
		</label>
	</section>
}

playing, liked and volume are values the page watches. isLiked reads two of them, and title and cover read one. Each spot in the markup that reads a value gets an edge from it, so one value can feed many spots, and one spot can depend on many values.

You build the graph by building the page

The compiler turns each state() and computed() into a node, and each spot that reads one into a subscription, drawn as an edge. Drag pieces onto the page, then pick a song, like it, or turn the volume:

A pen-and-ink drawing of a music page that starts with only a song list, a tray of pieces below it, and a state graph beside it, laid out left to right: states, computed values, and the spots on the page that read them. The graph starts with three states, playing, liked and volume, and one edge from playing to the song list's row class. Drag the Now playing header, the heart, the album cover or the volume knob onto the page, or press it in the tray, or press 1 to 4: its computed value and its reader ink themselves in. The heart brings isLiked, which reads both liked and playing. The My playlist heading, key 5, reads nothing and draws no node. Pick a song: only playing's branch pulses. Press the heart, or L: only liked, isLiked and the heart pulse. Press the knob, or V: only volume and its text pulse. Drag a piece back to the tray and its edges are erased. Escape starts again.

A music page with just a song list, and its state graph: three states, and playing read by the list. Drag pieces onto the page.

  • Each piece brings its computed value and its spot, with edges from every value they read. The heart brings isLiked, which reads both liked and playing.
  • The heading reads nothing, so it never gets a node.
  • A write travels only its own branch: liking a song never touches the header or the cover. Drag a reader off again and a song pick reaches fewer spots.

A write runs only its own path

When playing changes, @markless/runtime:

  1. marks state:playing changed, and any computed that reads it stale,
  2. waits for the current code to finish, then runs one flush,
  3. runs only the subscriptions on that path and applies the page changes they return.
PlaygroundRestored your edit

Melon calls it Rocket

Rocket

Try this
  1. To do: Press Rename. The heading and the name on the hull flash; the rocket stays on the pad.
  2. To do: Press Launch. Only the rocket and its button flash, because only they read up.
  3. To do: Show the name a third time: add <p>{NAMES[name]}</p>, then press Rename. All three copies flash in the same flush.
  • Two writes in the same turn share one flush.
  • A write to song.title doesn't reach a reader of song.artist; replacing song reaches both.
  • @markless/core has no effect function. The compiler plans every subscription.

Lazy chunks shows where the code behind each subscription lives.