Reference · Jobs and services

watch

signature
watch(any...) -> null

What it does

Inside a live loader, subscribes the page to a topic, so that publish of that topic runs the loader again. The tables and globals a loader reads are followed by themselves; watch is for a topic of your own, usually one narrower than a table: one thread, one project. Several topics can be given at once; each is written as text. It gives null. See Live data.

Examples

in a program
server fn load_posts(thread: int) {    watch(`thread:${thread}`)    unwatched(() => db.query("select * from posts where thread = ?", thread))}component Thread(id: int) {    state posts = live(load_posts, id)    <ul>{posts | map((p) => <li>{p.text}</li>)}</ul>}
print(watch("thread:7"))print(watch("thread:7", "thread:8"))
output
nullnull

Edge cases

print(watch(), watch(7), unwatched(() => watch("ignored")))
output
null null null

Errors

Any number of topics, of any type, is taken, each as its text. An empty topic, or one past 256 bytes, is a runtime error, not an error value.

print(watch(null, { thread: 7 }))
output
null
this one fails
print(watch(""))
output
line 1: `watch`: a topic is a name, and this one is empty

Chaining

watch goes first in the loader, then the reads it should not follow by table inside unwatched, then the value:

in a program
server fn load_project(id: int) {    watch(`project:${id}`)    unwatched(() => db.one("select * from projects where id = ?", id))}

See also

publish, unwatched, live

Thread
6Useful? Like it, or add to its thread below.

Thread 0 posts

On watch(): questions, tips, corrections, and code that uses it.

No posts yet. The first one opens the thread.

    Edit this page on GitHub · every example's output was made by running it (tools/refcheck.mjs) · questions on Discord or r/quillo