Published
Edited
Jun 26, 2022
Also listed in…
Tips and Utilities
Insert cell
Insert cell
Insert cell
Insert cell
mjs = import("@maverick-js/observables")
Insert cell
{
// Create - all types supported (string, array, object, etc.)
const $m = mjs.$observable(1);
const $x = mjs.$observable(1);
const $b = mjs.$observable(0);

// Compute - only re-computed when `$m`, `$x`, or `$b` changes.
const $y = mjs.$computed(() => $m() * $x() + $b());

// Effect - this will run whenever `$y` is updated.
const stop = mjs.$effect(() => {
console.log('effect:', $y());
viewof textarea.value = `${viewof textarea.value}
${$y()}
`
});
$m.set(10); // logs `10` inside effect

// Wait a tick so update is applied and effect is run.
await mjs.$tick();

$b.update((prev) => prev + 5); // logs `15` inside effect

// Wait a tick so effect runs last update.
await mjs.$tick();

// Nothing has changed - no re-compute.
$y();

// Stop running effect.
stop();
}
Insert cell
viewof textarea = Inputs.textarea({
label: "Output",
rows: 60,
cols: 120,
value: "waiting"
})
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more