Hot Reload (hotReload)

hotReload() lets you pick parts of your app's state that survive a page refresh.

hotReload was written as a debugging aid: reload the page while you work and keep your place. It may be useful more generally, but it has not been tested for production use and is not intended for it.

hotReload(test: PathTestFunction = () => true): void

Call it after registering your initial state. test is called with the name of each top-level piece of state and returns true for the ones to keep; with no argument, everything is kept.

import { tosi, hotReload } from 'tosijs'

const { app, prefs } = tosi({
  app: { draft: '' },
  prefs: { theme: 'system' },
})

// keep prefs across reloads, start app fresh each time
hotReload((name) => name === 'prefs')

The chosen state is saved to localStorage when it changes, and on the next load the saved values replace the initial ones. To reset, run localStorage.removeItem('xin-state') in the console.

Why not to use it for anything serious

Saving is debounced, so it is at least not pathologically bad: a burst of changes produces one write.

A production-ready version of this would:

That is not a gap hotReload is waiting to fill. We have built exactly that for a production app, and every one of those choices (what to serialize and how, what to encrypt, where the secret lives) belonged to that app. It cannot be made one-size-fits-all, so tosijs does not try: build it for your app, on top of observe.

We could ship it with a pluggable storage backend and a pluggable transformation function, but that would leave you writing the two parts that matter and are easy to get wrong. Storing data securely is not something to do through a plug-in you did not need to understand.