Not quite. viewof
produces two cells internally. If you write viewof foo
, you end up with viewof foo
(which has the DOM element as its value), and foo
(which holds the value of the DOM element’s value
property). You may also find this explanation helpful.
Now, whenever a cell produces a promise as its value, Observable’s Runtime will wait for that promise to resolve before handing it off to other cells. You can play around with that behavior by using the stdlib’s Promises helpers, e.g.:
// Produces the string "done!" after 3 seconds
foo = Promises.delay(3000, "done!")
Cells cannot be awaitable, because the Runtime abstracts that away for you. It only runs/reruns dependent cells once the promise has resolved.
There’s no link between cells. Observable wraps your cell’s code in a function that receives all referenced values as arguments. For example, if a cell contains:
`${foo} ${bar}`
then Observable will compile that cell to
"use strict";(
function(foo,bar){return(
`${foo} ${bar}`
)}
)
Now whenever the foo
or bar
cell produces a new value, this function will be invoked with their updated values, and the returned value becomes the cell’s new value.
In JavaScript, everything that’s not a scalar value (string, number, bool …) is passed around by reference. If multiple cells consume a value that contains a DOM element, then they all have a reference to same element. If the value updates, it might produce a new reference, but it doesn’t have to:
hey = {
const hey = html`<p>Hello!`;
while(true) {
yield hey;
await Promises.delay(3000);
}
}
Let’s verify that with the following code (note that this
contains the cell’s previous value, which is undefined
on the first run). You should see the above cell’s “Hello!” start out red, then turn green after 3 seconds:
{
hey.style.color = this === hey ? 'green' : 'red';
return hey;
}
To really see that we’re only working with references, you could even create an intermediate cell:
ho = hey
and then reference that:
{
ho.style.color = this === ho ? 'green' : 'red';
return ho;
}
A word of warning: This pattern produces side effects, because it modifies another cell’s value. You should try to avoid side effects in Observable cells.