This repository has been archived by the owner on Mar 5, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fixes #1
- Loading branch information
Showing
3 changed files
with
39 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
"use strict"; | ||
|
||
exports.primitives = | ||
{ getItem: function (k, win) { return win.localStorage.getItem(k); } | ||
, setItem: function (k, v, win) { win.localStorage.setItem(k, v); } | ||
, removeItem: function (k, win) { win.localStorage.removeItem(k); } | ||
}; |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
-- | Wrapper for window.localStorage | ||
module Bonsai.Storage where | ||
|
||
import Prelude | ||
|
||
import Bonsai.DOM (Window(..)) | ||
import Data.Function.Uncurried (Fn2, Fn3, runFn2, runFn3) | ||
import Data.Maybe (Maybe(..), maybe) | ||
import Foreign (F, Foreign, readNullOrUndefined, readString) | ||
|
||
foreign import primitives | ||
:: { getItem :: Fn2 String Foreign Foreign | ||
, setItem :: Fn3 String String Foreign Unit | ||
, removeItem :: Fn2 String Foreign Unit} | ||
|
||
-- | Get a string value for the the key from storage. | ||
getItem :: String -> Window -> F (Maybe String) | ||
getItem key (Window win) = do | ||
runFn2 primitives.getItem key win # | ||
readNullOrUndefined >>= | ||
maybe (pure Nothing) (map Just <<< readString) | ||
|
||
-- | Put the string value at key into the store | ||
setItem :: String -> String -> Window -> F Unit | ||
setItem key val (Window win) = | ||
pure $ runFn3 primitives.setItem key val win | ||
|
||
-- | Remove the key from the store | ||
removeItem :: String -> Window -> F Unit | ||
removeItem key (Window win) = | ||
pure $ runFn2 primitives.removeItem key win |