{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE ForeignFunctionInterface #-}
{-# LANGUAGE LambdaCase #-}
module Miso.Effect.Storage
(
getLocalStorage
, getSessionStorage
, setLocalStorage
, setSessionStorage
, removeLocalStorage
, removeSessionStorage
, clearLocalStorage
, clearSessionStorage
, localStorageLength
, sessionStorageLength
) where
import Data.Aeson hiding (Object, String)
import GHCJS.Marshal
import GHCJS.Types
import Miso.FFI
import qualified Miso.FFI.Storage as Storage
import Miso.String
getStorageCommon
:: FromJSON b => (t -> JSM (Maybe JSVal)) -> t -> JSM (Either String b)
getStorageCommon :: (t -> JSM (Maybe JSVal)) -> t -> JSM (Either String b)
getStorageCommon t -> JSM (Maybe JSVal)
f t
key = do
Maybe JSVal
result :: Maybe JSVal <- t -> JSM (Maybe JSVal)
f t
key
case Maybe JSVal
result of
Maybe JSVal
Nothing -> Either String b -> JSM (Either String b)
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Either String b -> JSM (Either String b))
-> Either String b -> JSM (Either String b)
forall a b. (a -> b) -> a -> b
$ String -> Either String b
forall a b. a -> Either a b
Left String
"Not Found"
Just JSVal
v -> do
Value
r <- JSVal -> JSM Value
forall json. FromJSON json => JSVal -> JSM json
parse JSVal
v
Either String b -> JSM (Either String b)
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Either String b -> JSM (Either String b))
-> Either String b -> JSM (Either String b)
forall a b. (a -> b) -> a -> b
$ case Value -> Result b
forall a. FromJSON a => Value -> Result a
fromJSON Value
r of
Success b
x -> b -> Either String b
forall a b. b -> Either a b
Right b
x
Error String
y -> String -> Either String b
forall a b. a -> Either a b
Left String
y
getSessionStorage :: FromJSON model => MisoString -> JSM (Either String model)
getSessionStorage :: MisoString -> JSM (Either String model)
getSessionStorage =
(MisoString -> JSM (Maybe JSVal))
-> MisoString -> JSM (Either String model)
forall b t.
FromJSON b =>
(t -> JSM (Maybe JSVal)) -> t -> JSM (Either String b)
getStorageCommon ((MisoString -> JSM (Maybe JSVal))
-> MisoString -> JSM (Either String model))
-> (MisoString -> JSM (Maybe JSVal))
-> MisoString
-> JSM (Either String model)
forall a b. (a -> b) -> a -> b
$ \MisoString
t -> do
Storage
s <- JSM Storage
Storage.sessionStorage
JSVal
r <- Storage -> MisoString -> JSM JSVal
Storage.getItem Storage
s MisoString
t
JSVal -> JSM (Maybe JSVal)
forall a. FromJSVal a => JSVal -> JSM (Maybe a)
fromJSVal JSVal
r
getLocalStorage :: FromJSON model => MisoString -> JSM (Either String model)
getLocalStorage :: MisoString -> JSM (Either String model)
getLocalStorage = (MisoString -> JSM (Maybe JSVal))
-> MisoString -> JSM (Either String model)
forall b t.
FromJSON b =>
(t -> JSM (Maybe JSVal)) -> t -> JSM (Either String b)
getStorageCommon ((MisoString -> JSM (Maybe JSVal))
-> MisoString -> JSM (Either String model))
-> (MisoString -> JSM (Maybe JSVal))
-> MisoString
-> JSM (Either String model)
forall a b. (a -> b) -> a -> b
$ \MisoString
t -> do
Storage
s <- JSM Storage
Storage.localStorage
JSVal
r <- Storage -> MisoString -> JSM JSVal
Storage.getItem Storage
s MisoString
t
JSVal -> JSM (Maybe JSVal)
forall a. FromJSVal a => JSVal -> JSM (Maybe a)
fromJSVal JSVal
r
setLocalStorage :: ToJSON model => MisoString -> model -> JSM ()
setLocalStorage :: MisoString -> model -> JSM ()
setLocalStorage MisoString
key model
model = do
Storage
s <- JSM Storage
Storage.localStorage
Storage -> MisoString -> MisoString -> JSM ()
Storage.setItem Storage
s MisoString
key (MisoString -> JSM ()) -> JSM MisoString -> JSM ()
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< model -> JSM MisoString
forall json. ToJSON json => json -> JSM MisoString
stringify model
model
setSessionStorage :: ToJSON model => MisoString -> model -> JSM ()
setSessionStorage :: MisoString -> model -> JSM ()
setSessionStorage MisoString
key model
model = do
Storage
s <- JSM Storage
Storage.sessionStorage
Storage -> MisoString -> MisoString -> JSM ()
Storage.setItem Storage
s MisoString
key (MisoString -> JSM ()) -> JSM MisoString -> JSM ()
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< model -> JSM MisoString
forall json. ToJSON json => json -> JSM MisoString
stringify model
model
removeLocalStorage :: MisoString -> JSM ()
removeLocalStorage :: MisoString -> JSM ()
removeLocalStorage MisoString
key = do
Storage
s <- JSM Storage
Storage.localStorage
Storage -> MisoString -> JSM ()
Storage.removeItem Storage
s MisoString
key
removeSessionStorage :: MisoString -> JSM ()
removeSessionStorage :: MisoString -> JSM ()
removeSessionStorage MisoString
key = do
Storage
s <- JSM Storage
Storage.sessionStorage
Storage -> MisoString -> JSM ()
Storage.removeItem Storage
s MisoString
key
clearLocalStorage :: JSM ()
clearLocalStorage :: JSM ()
clearLocalStorage = Storage -> JSM ()
Storage.clear (Storage -> JSM ()) -> JSM Storage -> JSM ()
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< JSM Storage
Storage.localStorage
clearSessionStorage :: JSM ()
clearSessionStorage :: JSM ()
clearSessionStorage = Storage -> JSM ()
Storage.clear (Storage -> JSM ()) -> JSM Storage -> JSM ()
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< JSM Storage
Storage.sessionStorage
localStorageLength :: JSM Int
localStorageLength :: JSM Int
localStorageLength = Storage -> JSM Int
Storage.length (Storage -> JSM Int) -> JSM Storage -> JSM Int
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< JSM Storage
Storage.localStorage
sessionStorageLength :: JSM Int
sessionStorageLength :: JSM Int
sessionStorageLength = Storage -> JSM Int
Storage.length (Storage -> JSM Int) -> JSM Storage -> JSM Int
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< JSM Storage
Storage.sessionStorage