|
| 1 | +-- Copyright 2016 Ian D. Bollinger |
| 2 | +-- |
| 3 | +-- Licensed under the MIT license <LICENSE or |
| 4 | +-- http://opensource.org/licenses/MIT>. This file may not be copied, modified, |
| 5 | +-- or distributed except according to those terms. |
| 6 | + |
| 7 | +-- | Provides bindings for the |
| 8 | +-- | [web storage API](https://html.spec.whatwg.org/multipage/webstorage.html). |
| 9 | +module Web.Storage |
| 10 | + ( STORAGE |
| 11 | + , Storage |
| 12 | + , local |
| 13 | + , session |
| 14 | + , length |
| 15 | + , key |
| 16 | + , getItem |
| 17 | + , setItem |
| 18 | + , removeItem |
| 19 | + , clear |
| 20 | + ) where |
| 21 | + |
| 22 | +import Prelude |
| 23 | +import Control.Monad.Eff (Eff) |
| 24 | +import Control.Monad.Eff.Exception (EXCEPTION) |
| 25 | +import Data.Nullable as Nullable |
| 26 | +import Data.Nullable (Nullable) |
| 27 | +import Data.Maybe (Maybe) |
| 28 | + |
| 29 | +-- | The web storage effect. |
| 30 | +foreign import data STORAGE :: ! |
| 31 | + |
| 32 | +-- | Provides access to a list of key/value pairs. |
| 33 | +foreign import data Storage :: * |
| 34 | + |
| 35 | +-- | Storage that persists between browsing sessions. |
| 36 | +foreign import local :: forall eff. Eff (storage :: STORAGE | eff) Storage |
| 37 | + |
| 38 | +-- | Like `local` storage, but is cleared when the browser is closed. |
| 39 | +foreign import session :: forall eff. Eff (storage :: STORAGE | eff) Storage |
| 40 | + |
| 41 | +-- | The number of key/value pairs currently in storage. |
| 42 | +foreign import length |
| 43 | + :: forall eff |
| 44 | + . Storage |
| 45 | + -> Eff (storage :: STORAGE | eff) Int |
| 46 | + |
| 47 | +-- | `key n` is the name of the *n*th key in storage. |
| 48 | +-- | |
| 49 | +-- | The order of keys is user-agent defined, but is consistent so long as the |
| 50 | +-- | number of keys doesn't change. If *n* is negative or greater than or equal |
| 51 | +-- | to the number of key/value pairs in storage, this function returns |
| 52 | +-- | `Nothing`. |
| 53 | +key |
| 54 | + :: forall eff |
| 55 | + . Int |
| 56 | + -> Storage |
| 57 | + -> Eff (storage :: STORAGE | eff) (Maybe String) |
| 58 | +key index storage = Nullable.toMaybe <$> keyForeign index storage |
| 59 | + |
| 60 | +foreign import keyForeign |
| 61 | + :: forall eff |
| 62 | + . Int |
| 63 | + -> Storage |
| 64 | + -> Eff (storage :: STORAGE | eff) (Nullable String) |
| 65 | + |
| 66 | +-- | The value associated with the given key in storage. |
| 67 | +-- | |
| 68 | +-- | If the given key does not exist in storage, this function returns |
| 69 | +-- | `Nothing`. |
| 70 | +getItem |
| 71 | + :: forall eff |
| 72 | + . String |
| 73 | + -> Storage |
| 74 | + -> Eff (storage :: STORAGE | eff) (Maybe String) |
| 75 | +getItem key' storage = Nullable.toMaybe <$> getItemForeign key' storage |
| 76 | + |
| 77 | +foreign import getItemForeign |
| 78 | + :: forall eff |
| 79 | + . String |
| 80 | + -> Storage |
| 81 | + -> Eff (storage :: STORAGE | eff) (Nullable String) |
| 82 | + |
| 83 | +-- | Add a new key/value pair to storage if a pair with the given key doesn't |
| 84 | +-- | yet exist; otherwise, replace the existing value with the given key. |
| 85 | +-- | |
| 86 | +-- | If a previous value is equal to the new value, this function does nothing. |
| 87 | +-- | If this function can't set a new value, it throws an exception. Setting |
| 88 | +-- | can fail if the user has disabled web storage, or if the storage quota has |
| 89 | +-- | been exceeded, for example. |
| 90 | +foreign import setItem |
| 91 | + :: forall eff |
| 92 | + . String |
| 93 | + -> String |
| 94 | + -> Storage |
| 95 | + -> Eff (storage :: STORAGE, err :: EXCEPTION | eff) Unit |
| 96 | + |
| 97 | +-- | Remove the key/value pair with the given key from storage. |
| 98 | +-- | |
| 99 | +-- | If no pair with that key exists, this function does nothing. |
| 100 | +foreign import removeItem |
| 101 | + :: forall eff |
| 102 | + . String |
| 103 | + -> Storage |
| 104 | + -> Eff (storage :: STORAGE | eff) Unit |
| 105 | + |
| 106 | +-- | Atomically empty storage of all key/value pairs. |
| 107 | +-- | |
| 108 | +-- | If there are no key/value pairs, this function does nothing. |
| 109 | +foreign import clear |
| 110 | + :: forall eff |
| 111 | + . Storage |
| 112 | + -> Eff (storage :: STORAGE | eff) Unit |
0 commit comments