billstclair/elm-localstorage/2.0.0

NOTE: this package is not compatible with Elm 0.19.1

LocalStorage.SharedTypes

Types for the LocalStorage module.

Types

type alias Key =
String

A convenience type for keys in the store. Same as String.

type alias Value =
Value

A convenience type for values in the store. Same as Json.Encode.Value.

= GetItemOperation
| SetItemOperation
| ClearOperation
| ErrorOperation

The operation that caused your wrapper Msg to be sent.

For real ports, you'll only ever see GetItemOperation.

For simluated ports, you'll see the others as well, because you have to save the updated state.

type Ports msg
= Ports
{ getItem : Ports msg -> GetItemPort msg
, setItem : Ports msg -> SetItemPort msg
, clear : Ports msg -> ClearPort msg
, state : DictState
}

Wrap up your ports.

You'll usually create one of these with LocalStorage.realRealPorts or DictPorts.make.

Your update Msg will receive one, if you're using simulated ports.

type alias DictState =
Dict Key Value

A Dict that stores key/value pairs for the simulated ports.

type alias MsgWrapper msg =
Operation -> Maybe (Ports msg) -> Key -> Value -> msg

Your Msg, which wraps the key/value pair from a getItem return.

For real ports, you'll only care about the GetItem operation. You'll get Nothing for the Ports.

For simluated ports, you need to store the Ports in your Model.

Port Signatures

type alias GetItemPort msg =
Key -> Cmd msg

The required signature of your localStorage.getItem port.

type alias SetItemPort msg =
( Key, Value ) -> Cmd msg

The required signature of your localStorage.setItem port.

type alias ClearPort msg =
String -> Cmd msg

The required signature of your localStorage.clear port.

type alias ReceiveItemPort msg =
(Value -> msg) -> Sub msg

The required signature of your subscription to receive getItem values.

Functions

receiveWrapper : MsgWrapper msg -> Value -> msg

Use this to turn the Value coming from a receive port into a Msg.

Constants

emptyDictState : DictState

Dict.empty