Language
Reactivity
Examplea whole program
shape Post id: number title: stringstate tag = "all"state ticks = 0resource posts: Post[] = net.get(`posts-${tag}.json`) else []every 1000 ticks += 1column gap=8 padding=16 row gap=8 for t in ["all", "rust", "web"] button t press={tag = t} font-weight=(t == tag ? 700 : 400) button "Reload" press={refresh(posts)} if pending(posts) text "Loading…" color=dim if failed(posts) text `Could not load: ${failure(posts) ?? ""}` color=danger for p in posts key=p.id text p.title text `open for ${ticks} s` color=dimMental model#
State#
Derive#
Resources#
Tasks#
Examplea whole program
state saved: number[] = []state opened = 0task opened = now()task load saved = db.get("saved")? ?? []column gap=8 padding=16 button "Load again" press=load disabled=pending(load) text `${saved.length} saved` if failed(load) text (failure(load) ?? "") color=dangerTimers#
watch#
Examplea whole program
state active = "a"state settings = {exposure: 0}state trail: string[] = []state saves = 0state deep = falsewatch active trail.push(`now ${active}`)watch settings db.set("settings", settings)? saves += 1column gap=8 padding=16 row gap=8 button "a" press={active = "a"} button "b" press={active = "b"} button "Brighter" press={settings.exposure += 1} button (deep ? "Stop counting" : "Count") press={deep = !deep} if deep watch trail.length log(`${trail.length} changes`) text `${trail.join(", ")} · saved ${saves} times`Tasks and timers inside markup#
Examplea whole program
state playing = falsestate frame = 0state log: string[] = []column gap=8 padding=16 button (playing ? "Stop" : "Play") press={playing = !playing} if playing every 100 frame += 1 task log.push(`started at frame ${frame}`) text `frame ${frame}` for l in log text l color=dimActions that wait#
assume: shown before the answer, taken back if it fails#
Examplea whole program
shape Post id: string likes: number liked: boolstate posts: Post[] = [{id: "a", likes: 3, liked: false}]server fn like(id: string): number if id == "" fail("there is no such post", {kind: "missing"})? return 1action heart(p: Post) assume p.likes += 1 p.liked = true like(p.id)?for p in posts key=p.id button `♥ ${p.likes}` press={heart(p)}text `${failure(heart)?.words ?? ""}`newest: a later run calls an earlier one off#
Examplea whole program
state depth = 24state shown = ""fn fitted(depth: number): string let best = 0 let i = 0 while i < 30000000 if (i * depth) % 97 > best % 97 best += 1 i += 1 return `${best} parts at ${depth}`action preview newest shown = fitted(depth)column gap=8 padding=16 input type=range min=10 max=40 value=depth input=preview text (pending(preview) ? "Working…" : shown)Workers#
Examplea whole program
state limit = 3000000state found = 0fn primes(limit: number): number let count = 0 let n = 2 while n < limit let prime = true let d = 2 while d * d <= n if n % d == 0 prime = false break d += 1 if prime count += 1 n += 1 return countmain fn primesHere(limit: number) = primes(limit)action count found = primes(limit)action countHere found = primesHere(limit)column gap=8 padding=16 button "In a worker" press=count disabled=pending(count) button "On the page" press=countHere text `${found} primes`Said once, for a file or a package#
Examplea whole program
fns mainstate found = 0fn squares(upTo: number): number let total = 0 for n in [1, 2, 3, 4] if n <= upTo total += n * n return totaloff fn slowSquares(upTo: number) = squares(upTo) * 1action count found = squares(3)action countAway found = slowSquares(3)button "Here" press=countbutton "In a worker" press=countAwaytext `${found}`What is copied and what is moved#
Pitfalls#
In the source#
Edit this page:
docs/reactivity.md