# Concurrency

JavaScript runs a program in one thread. Rook keeps that thread for waiting and gives the
work that keeps a processor busy to other threads, with the means of Go: an async function waits for
a timer or the network in the same thread, `go f(x)` runs a function as a goroutine, on another
processor, and channels and `select` pass values between goroutines in any thread. The compiler
checks that what goes to another thread can be copied there.

### Async functions

```go
async func loadText(url string) (string, error) {
    const response = await fetch(url)
    if !response.ok {
        return "", error(`HTTP ${response.status}`)
    }
    return await response.text(), null
}

const text, err = await loadText("/hello.txt")
```

```js
async function loadText(url) {
  const response = await fetch(url)
  if (!response.ok) {
    return ['', new Error(`HTTP ${response.status}`)]
  }
  return [await response.text(), null]
}

const [text, err] = await loadText('/hello.txt')
```

`async` is written before `func`, before a method of a class (`async load() string`), before a
`func` literal and before an arrow function (`async x => ...`).

- **The result types are what `return` gives;** the function itself returns a Promise of them:
  `loadText` has the type `func(string) Promise<(string, error)>`. So the results are written as
  in any function, also several of them; `Promise<string>` as the result of an async function is
  an error.
- **`await p` waits for the Promise and gives its value.** The results of an async function are
  unpacked from it as from a call: `const text, err = await loadText(url)`. `await` of a value
  that is not a Promise gives the value itself, as in JS.
- **Where `await` is allowed:** in async functions and at the top level of a module, since
  modules are ES modules. Not in `defer`: deferred code runs when the function ends and cannot
  wait. Not in markup: its code runs again on updates. A component cannot be async: load the data
  in `mount` or in an event handler.
- **An event handler may be async:** `onClick={async () => { user = await load() }}`. A change of
  state after `await` updates the markup like any other.
- A `defer` of an async function runs when it ends, after everything it has waited for.

A Promise made by hand gets its type from the declared one:

```go
async func sleep(ms number) {
    const done Promise<number> = new Promise(resolve => setTimeout(() => resolve(ms), ms))
    await done
}
```

Without a declared type, what it gives is inferred from the calls of `resolve` in the callback:
`new Promise(resolve => resolve(1))` is a `Promise<number>`, and calls with a number and a string
make it `Promise<number | string>`. A `resolve` that is not called, or is passed on, as a pause is
written, `await new Promise(resolve => setTimeout(resolve, 5))`, gets values the callback does not
show: then the promise gives nothing, `Promise<void>`, and a value given to `resolve` elsewhere is
an error that asks for the type. That is so for the parameters of the functions that a callback
gets, as `resolve`; a parameter of the callback itself, `apply(x => ...)` of
`func apply<T>(f func(T))`, still needs its type.

### Goroutines

```go
func countPrimes(from, to number) number {
    // ...
}

const PART = 1_000_000
const parts = await Promise.all([0, 1, 2, 3].map(i => go countPrimes(i * PART, (i + 1) * PART)))
go countPrimes(0, 100)              // nothing waits for it
```

```js
const parts = await Promise.all(
  [0, 1, 2, 3].map((i) => $$go('countPrimes', [i * PART, (i + 1) * PART])),
)
$$go('countPrimes', [0, 100])
```

`go f(x)` calls `f` as a goroutine, in another thread, and the program goes on without waiting for
it. Goroutines run at once, on different processors: the threads are a pool of workers, Web
Workers in the browser and worker threads in Node, and a worker can run several goroutines (see
"The pool" below).

- **What `go` calls:** a function declared at the top level of the module, an imported one, or a
  function literal, `go func(n number) { ... }(i)`. Its worker loads the functions that `go` calls, with the declarations and the imports that their
  code uses, but not the rest of the module and not its top-level code. The modules it imports run
  their top-level code in the worker too.
- **A function literal** captures local names at dispatch. The compiler puts these captures in the
  first parameters of the worker function and passes them through the generated `$$go` call.
  Ordinary captured objects, arrays, and other data are copied between threads. Changes to
  the worker's copy do not update the caller's closure, and rebinding captured names is an error.
  To coordinate goroutines, send values through a `chan` or put shared state in a `struct` and
  protect concurrent access with `rwMutex`.
- **Copies:** ordinary arguments and results are copied
  (the structured clone of `postMessage`). So they are data: numbers, strings, `bool`, arrays,
  objects of interfaces without methods, `Map`, `Set`, `Date`, typed arrays, queues, `error`, and
  instances of a `data class` (section 8), which keep their class. A function, an instance of another
  class or a DOM element is a compile error: its copy would lose its methods.
  Changing a copy does not change the original.
- **Structures:** named structures automatically use shared memory at supported `go` and
  `go for` boundaries and when sent to another thread over a typed structure channel.
- **Moving instead of copying:** `transfer(x)` gives `x`, an `ArrayBuffer` or a typed array, and
  marks its buffer: the next time it goes to another thread, as an argument, a result or through a
  channel, it moves there without a copy, and here it is left empty (the transfer list of
  `postMessage`). `go sum(transfer(data))` passes a large array for free; `data.length` is 0 after
  it.
- **Worker scope:** the function cannot access mutable module bindings, components, or markup.
  Pass values explicitly or use a channel to communicate with the owner thread.
- **The result:** as an expression, `go f(x)` is a Promise of what `f` returns, or of what its
  Promise gives for an async `f`: `const n, err = await go load(id)`. As a statement nothing waits
  for it, and an exception in it is not caught: in Node it ends the program, like a panic in Go.
- **The pool:** the workers start on the first `go`, up to one per processor, and the modules of
  the program share them. A call waits in one queue for a free worker of its module, one whose
  thread is not running code: a worker that is busy with a long call gets no more, and the next
  call goes to the first worker that is free. A worker runs several calls at once while they wait
  in `await`, so a call goes to a free worker without calls, else to a new worker, else to a free
  worker whose calls wait. When there are as many workers as processors, an idle worker of another
  module makes room, unless it keeps channels. In Node, idle workers do not keep the program
  running, but, unlike Go, the program waits for the goroutines that run, even for one that waits
  forever to send to a channel nobody reads, and for a worker whose channel it waits for. `go`
  inside a goroutine runs in the same worker.

The worker module goes next to the module: `rook build app.rk` writes `app.js` and
`app.worker.js`, `rook run` compiles it on the fly, and the Vite plugin gives it to Vite, which
bundles it as a worker.

`go` is for work that keeps a processor busy. Waiting, for a timer or the network, needs no
thread: the call of an async function without `await`, `search(query, out)`, runs in the same
thread at once with the rest of the program, sharing its memory, as goroutines of Go share theirs.
It talks to the others through channels as well.

### go for

```go
async func lighten(pixels Uint8ClampedArray, amount number) {
    go for i, v in pixels {
        pixels[i] = v + (255 - v) * amount
    }
}
```

```js
async function lighten(pixels, amount) {
  await $$goFor($$go, '$$goroutine0', pixels, amount)
}

// app.worker.js
async function $$goroutine0(pixels, $$from, amount) {
  for (let $$i = 0; $$i < pixels.length; $$i++) {
    const i = $$from + $$i
    const v = pixels[$$i]
    pixels[$$i] = v + (255 - v) * amount
  }
  return $$transfer(pixels)
}
```

`go for i, v in items` is a parallel loop over an array: the array is split into parts, each part
runs as a goroutine, and the loop waits for all of them, as a `WaitGroup` would. After it, the
array has what the body wrote into it.

- **What it goes over:** an array `T[]` or a typed array (`float64[]`, `byte[]` and the others)
  whose elements can be copied, as the arguments of `go`. `i` is the index in the
  whole array, `v` the element; one variable gets the element, as in `for`.
- **Its own element:** a worker gets only its part, so the body uses the array only as `items[i]`,
  with the index of the loop, to read the element or write it. Other elements, such as neighbours,
  come from another array: `go for i, _ in out { out[i] = (src[i - 1] + src[i + 1]) / 2 }`. The
  array that the body writes is a name, the one in the header.
- **Other names** that the body uses are copied to each worker, as for a function literal of `go`:
  they must be data, and the body cannot change them or the names of the module, since only a copy
  would change. The results go into the array, or through a channel.
- **It waits,** so it is allowed where `await` is: in async functions and at the top level of a
  module. An exception in a part fails the loop, as in `Promise.all`. `continue` skips an element;
  `break`, `return` and `defer` are errors, since the parts run at once and apart from the
  function.
- **Parts:** four per processor, each to the first worker that is free, so that the workers end at
  about the same time when some elements take longer than others (in `isPrime(i)` the large `i`)
  or some processors are slower. The other names are copied with each part, so if one of them is an
  object or an array, there is one part per processor.
- **Memory:** the part of a typed array is a copy that moves to its worker and back with
  `transfer`, and goes into the array with `set`; the part of a `T[]` is copied there and back. If
  the body writes nothing into the array, nothing comes back. A typed array on a
  `SharedArrayBuffer`, `make(float64[], n, shared)`, is not copied: the workers write into it.
  Only a page with cross-origin isolation (the COOP and COEP headers) can make one; GitHub Pages, where the site of Rook
  is, cannot send them, so its playground copies.
- Inside a goroutine, `go for` runs its parts in the same worker, as `go` does.

### Channels

```go
async func square(jobs chan number, results chan number) {
    for n in jobs {
        results <- n * n
    }
}

const jobs = chan<number>(10)          // room for 10 values
const results = chan<number>()          // no room: a send waits for a receiver
go square(jobs, results)
go square(jobs, results)
for let n = 1; n <= 4; n++ {
    jobs <- n
}
close(jobs)
for let i = 0; i < 4; i++ {
    console.log(<-results)
}
close(results)                          // every result is received
```

```js
const jobs = $$channels.make(10, 0)
const results = $$channels.make(0, 0)
// ...
await $$channels.send(jobs, n)
// ...
console.log(await $$channels.receive(results, false, 0))
```

Channels pass values between goroutines, as in Go. `chan T` is the type of a channel of `T`, and
`chan<T>(size)` makes one with room for `size` values; `chan<T>()` has none. `chan(stream, size)`
makes one that receives a stream (see "Streams" below).

- **`ch <- v` sends a value** and waits until the channel takes it: while the channel has room,
  at once, and otherwise until a receiver comes. **`<-ch` receives a value**, waiting until there
  is one. Both wait, so they are allowed where `await` is, in async functions and at the top
  level of a module, and compile to `await`.
- **`for v in ch`** receives values until the channel is closed and empty.
- **`close(ch)`** closes the channel: nothing more goes in, and a send to a closed channel or a
  second `close` throws. A closed, empty channel gives the zero value of its type at once, as in
  Go, and `const v, ok = <-ch` tells it: `ok` is `false`. A type without a zero value (an
  interface, a class) gives `null`, so `<-ch` of a `chan Point` is a `?Point`.
- **The zero value** of a channel type is `Channel.EMPTY`, one channel for all types, which is
  closed: receiving from it gives the zero value of the type at once, which the code that
  receives passes, and `ok` is `false`; `for v in ch` ends at once, and a case of `select` with it
  always goes on. A send to it goes nowhere, and `close` does nothing. So `let results chan
number` is a channel that is safe to use before it is given one; a `?chan T` is `null` until
  then, and a `null` channel turns its case of `select` off.
- **A channel that is never closed is a warning,** and so are a topic and a `tick`: a tick runs
  and a topic listens until they are closed, and `for v in ch` waits for a close that does not
  come. A variable made by `chan<T>()`, `topic<T>()` or `tick(ms)` (or `Channel.make` and the like)
  counts as closed when some line closes it, `close(ch)`, or when it goes to a function of the
  module that closes that parameter: `go produce(jobs)` with `close(out)` in
  `produce(out chan<- number)`. A channel that goes where the module does not see what happens to
  it, to another module, into an object, out of a function or through a channel, is left to that
  code, and so are an exported variable, `after(ms)` and `chan(stream)`, which close themselves.
  A warning does not stop the build.
- **Between threads:** a channel goes to another thread as its id, so goroutines in different
  workers and the main thread share its queue. Ordinary values sent between threads are copied.
  A typed structure channel passed directly through `go` sends a shared handle, so the receiving
  thread sees the same structure, including its methods and mutexes. Within one thread a value
  is passed itself. The queue lives in the thread that made the channel, and the
  other threads ask that thread directly, each through a port of its own: workers pass values to
  each other also while the main thread is busy. A channel that goes on with a case of a `select`
  over channels of several threads locks the `select` first, so only one case goes on, as in Go.
  A channel that no value holds any more, in any thread, is forgotten.
- `chan number` is not a `chan ?number`: a channel both takes and gives values, so the types must
  be the same.
- **One direction:** `chan<- T` only sends and `<-chan T` only receives, as in Go: a function that
  takes `out chan<- string` can only send to it, and `close` it. A `chan T` goes where either is
  expected, but not the other way round. `<-` goes with the leftmost `chan`, so `chan<- chan T` is
  a channel that sends channels, and a channel of `<-chan T` is written `chan (<-chan T)`.

### select

```go
select {
case job = <-jobs:
    work(job)
case msg, ok = <-control:
    if !ok {
        return
    }
case results <- last:
    console.log("sent")
case <-quit:
    return
default:
    console.log("nothing is ready")
}
```

```js
{
  const [$$case, $$received, $$ok] = await $$channels.select(
    [{ channel: jobs }, { channel: control }, { channel: results, value: last }, { channel: quit }],
    true,
  )
  switch ($$case) {
    case 0: {
      const job = $$received
      work(job)
      break
    }
    // ...
  }
}
```

`select` waits until one of several operations on channels can go on, does it and runs its case,
as in Go. A case receives, `case v = <-ch:` or `case <-ch:`, or sends, `case ch <- v:`.

- **The variables of a case:** `case job = <-jobs:` declares `job` for that case only, and
  `case msg, ok = <-control:` gets `ok` as `v, ok = <-ch` does. They are always new variables: to
  keep the value, assign it in the case.
- **Which case:** one that can go on at once, a random one if several can; otherwise `default`, if
  there is one; otherwise `select` waits for the first one that can.
- **A null channel** turns its case off, as a nil channel does in Go: the channel of a case may be
  a `?chan T`, and a case whose channel is `null` never goes on. So a loop can stop waiting on a
  channel by setting a variable to `null`.
- `break` inside a case leaves the `select`, as in Go.
- `select` waits, so it is allowed where `await` is. It is a keyword only before `{`: elsewhere
  `select` is an ordinary name.

### Timers and cancel

```go
const ticks = tick(100)
const done = chan<number>(1)
const task = go async func() {
    done <- countPrimes(100_000_000)
    close(done)                         // one value, and nothing more
}()

select {
case n = <-done:
    console.log(n)
case <-after(500):
    cancel(task)
}
close(ticks)
```

```js
const ticks = $$channels.tick(100)
// ...
const [$$case, $$received, $$ok] = await $$channels.select(
  [
    { channel: done, zero: 0 },
    { channel: $$channels.after(500), zero: 0 },
  ],
  false,
)
// ...
$$cancel(task)
```

- **`after(ms)`** is a `<-chan number` that gets the time (`Date.now()`) once, after `ms`, as
  `time.After` in Go: `case <-after(500):` is a timeout.
- **`tick(ms)`** is a `chan number` that gets the time every `ms`, as `time.Tick`. A tick that
  finds the last one not received yet is dropped. `close(ticks)` stops it; until then, in Node, it
  keeps the program running.
- **`cancel(task)`** stops a goroutine, given the Promise of `go`: a call that waits for a worker
  leaves the queue, and a running one stops with its worker (`terminate()`), even in the middle of
  a loop. Its Promise fails with "goroutine cancelled", and nothing else fails because of it if
  nobody waits for it. The other calls that wait in that worker in `await` fail too, and the
  channels that the worker made are gone. Inside a goroutine, `go` runs in the same worker, so
  `cancel` makes its Promise fail, but code that has started goes on. A Promise that is not of
  `go`, or of one that has finished, is left as it is.
- `after`, `tick` and `cancel` are predeclared, like `close`: a declaration with one of these names
  hides them.

### Signals and cancellation

> **Experimental:** may change between releases; see the status at the top.

```go
import { createServer } from "node:http"

const stop = signal("SIGINT", "SIGTERM")
const server = createServer(handle).listen(8080)
console.log("stopping on", <-stop)          // waits for Ctrl+C or for the process manager
close(stop)
server.close()

async func load(url string, request Request) (string, error) {
    const response, err = try await fetch(url, { signal: request.signal })
    // ...
}

select {
case body = <-results:
    reply(body)
case reason = <-done(request.signal):       // the client has gone
    console.log("cancelled:", reason)
}
```

```js
const stop = $$channels.signal('SIGINT', 'SIGTERM')
// ...
const [$$case, $$received, $$ok] = await $$channels.select(
  [{ channel: results, zero: '' }, { channel: $$channels.done(request.signal) }],
  false,
)
```

A server stops when the process manager tells it to, with a signal, and a request stops when its
client goes, with an `AbortSignal`. Both become channels, so that `select` waits for them with the
rest, as `signal.Notify` and the `Done()` of a `context.Context` do in Go.

- **`signal(...names)`** is a `chan string` that gets the name of each signal that comes:
  `signal("SIGINT", "SIGTERM")`. While it listens, the signals do not end the process, and, as a
  `tick`, it keeps the program running until `close(stop)`; then the signals do what they did
  before. A signal that finds the last one not received yet is dropped. Only Node has signals, and
  only its main thread gets them; a name that does not start with `SIG` is an error, and a signal
  that is never closed is a warning. Make it once, before the loop that waits for it: each
  call listens anew.
- **`done(abortSignal)`** is a `<-chan any` that gets the reason of the `AbortSignal` when it
  aborts, and is closed then: `case reason = <-done(request.signal):` gives the reason, and a
  `case <-done(...)` goes on at once after the abort. An `AbortSignal` that has aborted already
  gives its reason at once.
- **`abortSignal(ch)`** is an `AbortSignal` that aborts when the channel gets a value, with the
  value as its reason, or is closed, for the APIs that take one: `fetch(url, { signal:
abortSignal(stop) })`. It receives that one value, as any receiver of the channel would.
- `rook run` gives the program the `SIGTERM` and `SIGHUP` that it gets, and waits for it; Ctrl+C
  reaches the program by itself, as a terminal sends it to both.
- `signal`, `done` and `abortSignal` are predeclared, like `after`, and are also functions of
  `Channel`.

### Streams

> **Experimental:** may change between releases; see the status at the top.

```go
async func countWords(chunks <-chan string) number {
    let words = 0
    for chunk in chunks {
        words += chunk.split(" ").length
    }
    return words
}

const response = await fetch("book.txt")
const text = response.body?.pipeThrough(new TextDecoderStream())
const chunks, failed = chan(text, 4)
const words = await go countWords(chunks)
const err = <-failed
```

```js
const [chunks, failed] = $$channels.fromStream(text, 4, '')
const words = await $$go('countWords', [chunks])
const err = await $$channels.receive(failed)
```

A `ReadableStream`, such as the body of a `fetch` response, goes into a channel, and a channel
becomes a stream, so that goroutines make pipelines over the network.

- **`chan(stream, size)`** gives two channels: a `chan T` with room for `size` values that receives
  the chunks of a `ReadableStream<T>`, and a `<-chan error` that gets the error of the stream, or
  `null` at its end, as the `errc` of the pipelines of Go. Both are unpacked: `const chunks, failed
= chan(body)`. The chunks of a body are `byte[]`; `pipeThrough(new TextDecoderStream())` makes
  them strings.
- **Backpressure:** the next chunk is read only when the last one is in the channel, so the room
  of the channel is all that is read ahead: when it is full, the stream is not read, and the
  network waits.
- **The end:** when the stream ends, the channel is closed and `failed` gets `null`; when it fails,
  as when the connection is lost, the channel is closed after the chunks it got, and `failed` gets
  the error. `close(chunks)` cancels the stream, and `failed` gets `null`. A `null` stream, as the
  body of a response without one, gives a closed channel.
- **Threads:** the stream is read in the thread that called `chan`, and the channel goes to a
  goroutine as any channel does: its chunks are copied there.
- **`stream(ch)`** is a `ReadableStream<T>` of the values of a `<-chan T` that ends when the
  channel is closed: `new Response(stream(lines))`. It reads one value for each read of the
  stream, so the room of the channel is all it holds. A reader that cancels the stream stops
  reading the channel; the channel is not closed, since its senders would fail.
- `stream` is predeclared, like `after`.

### Topics

> **Experimental:** may change between releases; see the status at the top.

```go
interface CartEvent {
    kind "add" | "remove"
    id number
}

const cart = topic<CartEvent>("cart")
let items number[] = []

func add(id number) {
    items.push(id)
    cart <- { kind: "add", id }      // to the other tabs; waits for nobody
}

async func follow() {
    for e in cart {                  // what the other tabs send
        if e.kind == "add" {
            items.push(e.id)
        }
    }
}

func stop() {
    close(cart)                      // follow() ends
}
```

```js
const cart = $$topics.make("cart", null);
// ...
$$topics.publish(cart, { kind: "add", id });
// ...
for await (const e of $$channels.range($$topics.listen(cart))) {
```

A topic passes values between the tabs of a site, as a channel passes them between goroutines:
`topic T` is the type of a topic of `T`, and `topic<T>(name)` makes one, over the
`BroadcastChannel` of that name. Every tab of the site, and every worker, that makes a topic of
the name and listens to it gets each value. It is a type of its own, not a `chan`: a channel gives
each value to one receiver and its sender waits for room, while a topic gives each value to all who
listen and its sender waits for nobody.

- **`t <- v` sends** the value to the other tabs and goes on at once: nothing is kept here, and
  nothing waits for a receiver. So a send is allowed anywhere, in a function that is not async and
  in an event handler, `onClick={() => { cart <- event }}`. The tab that sends does not get its own
  value, as a `BroadcastChannel` does not hear itself: it has applied the change already.
- **`<-t`, `const v, ok = <-t`, `for v in t` and `case v = <-t:`** receive, as from a channel, and
  wait for the next value of another tab, so they are allowed where `await` is.
- **Each topic is a subscription.** It listens from its first receive on and keeps what comes in
  order, without a limit, until it is received, so nothing is lost between two receives; a topic that
  only sends keeps nothing. Receivers of one topic share its values, as those of a channel do; two
  topics of the same name each get every value, a copy of their own. So two components that both
  need every value make a topic each.
- **`close(t)`** stops the topic: it listens and sends no more, `for v in t` ends after the values it
  has, and then `<-t` gives the zero value of `T` and `ok` is `false`. A send to a closed topic or
  a second `close` throws. A component that listens closes its topic when it is removed, with a
  `defer` in `mount`: `defer close(cart)`.
- **Copies:** the values go to other tabs as copies, so they are data, as the arguments of `go`
  are, and without channels: the id of a channel means something only to the workers of the tab
  that made it. Each tab checks its own program, so the tabs that share a name must agree on its
  type.
- **Threads:** a topic goes to a goroutine as its name, and there it is a topic of its own, a
  subscription of that worker; the worker hears the tabs and the tabs hear the worker. In Node,
  `BroadcastChannel` connects the threads of one process; a topic that listens keeps the program
  running until it is closed, as a `tick` does.
- `topic number` is not a `topic ?number`, as with channels. The zero value is `Topic.EMPTY`, a
  topic without a name: it is closed, a send to it goes nowhere, `close` does nothing, and
  receiving gives the zero value of `T` at once. A null `?topic T` turns its case of `select` off.
  A send to a topic is not a case of `select`, since it never waits. A topic that is never closed
  is a warning, as a channel is.
- **`topic` is a name elsewhere,** as `queue` is: it is a type only before the type of its values,
  `topic number`, and makes a topic only before `<T>(`.

### Channel and Topic

> **Experimental:** may change between releases; see the status at the top.

```go
const jobs = Channel.make(10, 0)       // chan<number>(10)
await Channel.send(jobs, 1)             // jobs <- 1
const n = await Channel.receive(jobs)   // <-jobs, but a ?number
Channel.close(jobs)                     // close(jobs)
let none chan string = Channel.EMPTY    // what `let none chan string` gives
```

```js
const jobs = $$channels.make(10, 0)
await $$channels.send(jobs, 1)
const n = await $$channels.receive(jobs)
$$channels.close(jobs)
let none = $$channels.EMPTY
```

`Channel` and `Topic` are predeclared objects with the functions that channels and topics compile
to, by name, for code that needs them as values: to pass `Channel.close` on, or to receive without
`await` in the same line. In JS they are `$$channels` and `$$topics` of the runtime, so a program
may declare names `Channel` and `Topic` of its own, which hide them.

- **`Channel`:** `EMPTY`; `make(size, zero)`, a channel with room for `size` values that gives
  `zero` after close; `send(ch, v)`, a Promise; `receive(ch)`, a Promise of a `?T`, which is the
  zero value after close and `null` from `EMPTY`; `close(ch)`; `range(ch)`, an async iterator of
  the values for `for await` of JS; `select(cases, hasDefault)`, what `select` compiles to;
  `try(ch, send, value)`, an operation that goes on at once or `null`; `fromStream(stream, size,
zero)`, `stream(ch)`, `after(ms)`, `tick(ms)`, `signal(...names)`, `done(abortSignal)` and
  `abortSignal(ch)`.
- **`Topic`:** `EMPTY`; `make(name, zero)`; `publish(t, v)`, which is `t <- v`; `listen(t)`, the
  `<-chan T` that the topic listens into from then on; `close(t)`.
- They do what the syntax does, without its checks: `topic<T>(name)` checks that the values can
  go to other tabs, and `Topic.make` does not.

