| Copyright | (C) 2016-2026 David M. Johnson |
|---|---|
| License | BSD3-style (see the file LICENSE) |
| Maintainer | David M. Johnson <code@dmj.io> |
| Stability | experimental |
| Portability | non-portable |
| Safe Haskell | None |
| Language | Haskell2010 |
Miso.Storage
Description
Overview
Miso.Storage wraps the browser's
Web Storage API,
providing access to both
localStorage
and
sessionStorage
through symmetric IO functions.
Both stores map MisoString keys to MisoString
values (the Web Storage API only persists strings). Reads return
— Maybe MisoStringNothing when the key is absent.
- localStorage — persists across browser sessions until explicitly cleared.
- sessionStorage — persists only for the lifetime of the current browser tab/session; cleared automatically when the tab is closed.
Quick start
import Miso.Storage import Miso.String (ms) -- Persist a preference saveTheme ::MisoString-> IO () saveTheme theme =setLocalStorage"theme" theme -- Restore it on startup loadTheme :: IO (MaybeMisoString) loadTheme =getLocalStorage"theme" -- Session-scoped token (cleared when tab closes) saveToken ::MisoString-> IO () saveToken tok =setSessionStorage"auth_token" tok
API groups
- localStorage:
getLocalStorage,setLocalStorage,removeLocalStorage,clearLocalStorage,localStorageLength - sessionStorage:
getSessionStorage,setSessionStorage,removeSessionStorage,clearSessionStorage,sessionStorageLength
See also
- Miso.Effect — schedule storage reads/writes with
io/io_ - Miso.Subscription.History — URL-based state that survives page reloads
Synopsis
- getLocalStorage :: MisoString -> IO (Maybe MisoString)
- setLocalStorage :: MisoString -> MisoString -> IO ()
- removeLocalStorage :: MisoString -> IO ()
- clearLocalStorage :: IO ()
- localStorageLength :: IO Int
- getSessionStorage :: MisoString -> IO (Maybe MisoString)
- setSessionStorage :: MisoString -> MisoString -> IO ()
- removeSessionStorage :: MisoString -> IO ()
- clearSessionStorage :: IO ()
- sessionStorageLength :: IO Int
Local
Arguments
| :: MisoString | Storage key to look up |
| -> IO (Maybe MisoString) |
Retrieves a value stored under the given key in local storage.
Arguments
| :: MisoString | Storage key to set |
| -> MisoString | Value to store |
| -> IO () |
Sets the value of a key in local storage.
setLocalStorage key value sets the value of key to value.
Arguments
| :: MisoString | Storage key to remove |
| -> IO () |
Removes an item from local storage.
removeLocalStorage key removes the value of key.
clearLocalStorage :: IO () Source #
Clears local storage.
clearLocalStorage removes all values from local storage.
localStorageLength :: IO Int Source #
Returns the number of items in local storage.
localStorageLength returns the count of items in local storage
Session
Arguments
| :: MisoString | Storage key to look up |
| -> IO (Maybe MisoString) |
Retrieves a value stored under the given key in session storage.
Arguments
| :: MisoString | Storage key to set |
| -> MisoString | Value to store |
| -> IO () |
Sets the value of a key in session storage.
setSessionStorage key value sets the value of key to value.
Arguments
| :: MisoString | Storage key to remove |
| -> IO () |
Removes an item from session storage.
removeSessionStorage key removes the value of key.
clearSessionStorage :: IO () Source #
Clears session storage.
clearSessionStorage removes all values from session storage.
sessionStorageLength :: IO Int Source #
Returns the number of items in session storage.
sessionStorageLength returns the count of items in session storage