Last active
September 14, 2021 13:52
-
-
Save zaceno/ee531aea58d9a7b1e9ca546d4caa31e8 to your computer and use it in GitHub Desktop.
persistence middleware for hyperapp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
When you are developing using a live-reloading server, it can be | |
annoying when the state keeps resetting. This middleware (just | |
meant for debugging) persists state across reloads so you see | |
immediate changes on what you were workig on, without extra steps | |
over and over to get back to where you were. | |
Usage: | |
```js | |
import {app} from 'hyperapp' | |
import persistence from './persistence.js' | |
app({ | |
... | |
dispatch: persistence | |
}) | |
``` | |
*/ | |
const getSaved = () => { | |
let json = localStorage.getItem("persistent-state") | |
return json ? JSON.parse(json) : null | |
} | |
/** @param {any} state */ | |
const setSaved = state => { | |
localStorage.setItem("persistent-state", JSON.stringify(state)) | |
} | |
/** @template S @typedef {import('hyperapp').Dispatch<S> } Dispatch*/ | |
/** @type {<S>(d:Dispatch<S>)=>Dispatch<S>}*/ | |
export default d => { | |
let started = false | |
return (action, payload) => { | |
if (typeof action !== "function" && !Array.isArray(action)) { | |
if (started) { | |
setSaved(action) | |
} else { | |
started = true | |
let prev = getSaved() | |
if (!prev) { | |
setSaved(action) | |
} else { | |
action = prev | |
} | |
} | |
} | |
return d(action, payload) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment