LocalStorage API idea

It might be a really stupid idea.
But thinking more functionally, we always take a model and return a new model,
Could we somehow take the whole localState object and produce a new localState object ?

Read Once:
Browser.Storage.getLocalStorage or Browser.Storage.getSessionStorage

Read storage once, manipulate it like a normal model in Elm during the app’s lifetime, and then overwrite the whole thing every time you need to persist it.

If there comes a time during the app’s life, where you want to persist storage, you have a Cmd with Browser.Storage.persistLocalStorage and hands it (Dict String String) from your model.

In most cases, you only need to store before refresh and before page close ?

Subscriptions model =
Browser.Events.onUnload UnloadMsg

update … … =

UnloadMsg ->
( model, persistStorage model.storage )

1 Like

Evan has a good write up of the issues that need to be addressed before an Elm package for LocalStorage makes sense.

You can already write getLocalStorage and persistLocalStorage in a quite straight forward way as ports on your own.

1 Like

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.