Storage Location view (save view state to local storage)

So I am a big fan of safe-local-storage but the imperative wiring of calling setItem and getItem doesn’t feel idiomatic to me.

So what I have done is create a utility to expose a storage location as a backwritable view, which wraps the getItem/setItem into Observablehq idioms.

What nice about this is you can then use Inputs.bind to add persistence to ANY ui control view. You don’t even need to modify the declaration of the UI control. It can all be wired up in adjacent cells.

4 Likes

Really neat. Thanks for sharing!

I been thinking a lot about views. They are powerful because they are readable AND writable, i.e. bidirectional.

Dataflow is unidirectional, and is great, but it means your graph has to be DAG => you can’t have circular references.

With views you can declare processing node and wire them together by a second sent of binds afterwards. So you can kinda do a two step graph computation definition which is a gain of expressivity if you need to construct loopy processing graphs. (think audio VCV racks, you have the step 1. equipment (composite views) connected with step 2. patch cables (binds) and lots of loops).

The other think aspect of views is that they can encapsulate external stateful dependancies. Elm has the concept of ports for wrapping stuff that doesn’t really follow its strict functional style (Ports · An Introduction to Elm) and it strikes me that views can be used in a similar way. Idiomatic use of ports in Elm is to wrap localstorage infact, it’s almost identical to this latest notebook.

So now I begin to think that views are a great adapter for non-functional APIs. If it’s strictly read only you could use a generator, but I kinda of like the ability to hang a DOM explanation or even a UI on it as well. I feel you are more future proofed with a view. Using a generator leads to painting yourself into a corner.

site feedback: I think the forum should have a ‘general’ board where we can just chat. I don’t always want to post a show and tell or notify the observable team about something. I sometimes want to chit chat with peers in the community. There is no clear place for peer-to-peer discussion except show and tell, but name suggests a narrower use case.