Language
Reactivity
Language
reactivity.md

Reactivity

Examplea whole program
shape Post
id: number
title: string
state tag = "all"
state ticks = 0
resource posts: Post[] = net.get(`posts-${tag}.json`) else []
every 1000
ticks += 1
column 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=dim

Mental model#

State#

Derive#

Resources#

Tasks#

Examplea whole program
state saved: number[] = []
state opened = 0
task
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=danger

Timers#

watch#

Examplea whole program
state active = "a"
state settings = {exposure: 0}
state trail: string[] = []
state saves = 0
state deep = false
watch active
trail.push(`now ${active}`)
watch settings
db.set("settings", settings)?
saves += 1
column 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 = false
state frame = 0
state 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=dim

Actions that wait#

assume: shown before the answer, taken back if it fails#

Examplea whole program
shape Post
id: string
likes: number
liked: bool
state 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 1
action 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 = 24
state 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 = 3000000
state found = 0
fn 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 count
main 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 main
state found = 0
fn squares(upTo: number): number
let total = 0
for n in [1, 2, 3, 4]
if n <= upTo total += n * n
return total
off fn slowSquares(upTo: number) = squares(upTo) * 1
action count
found = squares(3)
action countAway
found = slowSquares(3)
button "Here" press=count
button "In a worker" press=countAway
text `${found}`

What is copied and what is moved#

Pitfalls#

In the source#

esc
Reading the docs…A heading's words count for more than what stands under it.
↑↓move
↵open
ranked by BM25, in the page