silmaril

Computed variables

A variable whose initializer reads a reactive variable is computed. It evaluates again each time a variable it reads changes.

computed-variables-message.js

No logs.

message is computed because it reads count. $ tracks message, so the write to count runs it.

Updates are synchronous

The compiler records which computed variables read each variable. A write updates them before it returns. Code after the write reads the new values.

import { $$ } from 'silmaril';

$$(() => {
  let count = 0;
  const message = `Count: ${count}`;
  count = 100; // message is now 'Count: 100'
  count = 200; // message is now 'Count: 200'
});

Computed variables run in declaration order. When every computed variable reads only variables declared before it, and no computed variable writes a variable, each runs at most once per write.

Writable computed variables

A computed variable declared with let can also be written. The next change to a variable it reads replaces the value.

computed-variables-writable.js

No logs.

Declaration forms

Limits