Persisting values

Can values be persisted across sessions? My use-case is to persist some slider values as I explore them.

Since localStorage does not work in a sandboxed iframe—nor should it—I was wondering if adding a Storage API to the standard library would be appropriate.

It could potentially postMessage to the parent frame to save/load localStorage values namespaced to the current notebook. So persisting a slider value might look like this:

viewof x = {
  const range = html`<input type=range min=0 max=100>`;
  range.value = 50; // default value

  const key = 'myval';
  range.addEventListener("input", () => Storage.save(key, range.value));
  Storage.load(key).then(value => {
    range.value = value; // load persisted value
    range.dispatchEvent(new Event('input'));
  });

  return range;
}
4 Likes

Yep, definitely! Mike especially interested in APIs that both provide value persistence and potentially value sharing. Because the Observable runtime knows about all values that cross ‘cell boundaries’, we could be able to do something like providing collaborative environments for some notebooks, as well as the ability to start where you left off last.

1 Like