Reference · Jobs and services
watch
watch(any...) -> nullWhat 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
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"))nullnullEdge cases
- Outside a loader it does nothing: a route, a job or a plain program has no page to subscribe.
- A topic that is not a string is written as text:
watch(7)is the topic7, the onepublish(7)names. - Inside
unwatched(f), awatchinfis not recorded either; callwatchoutside it, as in the example above. - A topic is 1 to 256 bytes, of any characters, a comma included: an empty or a longer
one is a runtime error, as for
publish. - A page may follow at most 256 topics; past that, the rest hear nothing. With tenants, the topic watched is the tenant's own.
print(watch(), watch(7), unwatched(() => watch("ignored")))null null nullErrors
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 }))nullprint(watch(""))line 1: `watch`: a topic is a name, and this one is emptyChaining
watch goes first in the loader, then the reads it should not follow by table inside
unwatched, then the value:
server fn load_project(id: int) { watch(`project:${id}`) unwatched(() => db.one("select * from projects where id = ?", id))}See also
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