Skip to content
This repository has been archived by the owner on Mar 5, 2023. It is now read-only.

Commit

Permalink
simple localstorage api
Browse files Browse the repository at this point in the history
fixes #1
  • Loading branch information
grmble committed Jun 28, 2018
1 parent 6df8f3f commit f8bb9e1
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/Bonsai/DOM.purs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ module Bonsai.DOM
, appendChild
, addEventListener
, clearElement
, defaultView
, document
, elementById
, focusElement
Expand Down
7 changes: 7 additions & 0 deletions src/Bonsai/Storage.js
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); }
};
31 changes: 31 additions & 0 deletions src/Bonsai/Storage.purs
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

0 comments on commit f8bb9e1

Please sign in to comment.