billstclair/elm-localstorage/5.0.0

NOTE: the latest version is 7.3.1

PortFunnel.LocalStorage

The PortFunnelLocalStorage uses the JavaScript localStorage facility to persistently store key/value pairs.

It is a billstclair/elm-port-funnel PortFunnel funnel.

Types

type Message

Messages that can be sent/received to/from the JavaScript code.

Startup is received after the JS code loads. It sets the isLoaded flag in our state, and reports NoResponse.

Get is sent to request a read.

Put is sent to request a write or received in response to Get. A value of Nothing means remove that key/value pair when sent or no value in the store when received.

ListKeys is sent to request a list of keys.

Keys is received in response to ListKeys.

Clear is sent to clear keys starting with the prefix. No response is sent.

The Simulate<Foo> messages are returned by the simulator and simulated by the process function (not exposed, except inside of moduleDesc).

= NoResponse
| GetResponse
{ key : Key
, value : Maybe Value
}
| ListKeysResponse
{ prefix : String
, keys : List Key
}

A MessageResponse is used to return values for Get and ListKeys.

type State

Our internal state.

type alias Key =
String

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

Components of a PortFunnel.FunnelSpec

moduleName : String

The name of this funnel: "LocalStorage".

Our module descriptor.

commander : (GenericMessage -> Cmd msg) -> Response -> Cmd msg

Responsible for sending a CmdResponse back through the port.

This funnel doesn't initiate any sends, so this function always returns Cmd.none.

Initial State

initialState : Prefix -> State

The initial state.

The Prefix arg (plus a period, if non-blank) is prepended to all keys sent to the backend, and removed from keys returned. It basically give you a namespace in local storage, and usually matches your application name. It allows multiple different Elm applications to be served by the same domain without stepping on each other's state.

The state also stores a Dict, which acts as the backing store for simulation, and a flag saying whether the JavaScript code has sent its "I'm loaded" message (see isLoaded).

Sending a Message out the Cmd Port

send : (Value -> Cmd msg) -> Message -> State -> Cmd msg

Send a Message through a Cmd port.

Note that this send function requires that you pass state, but it is read-only, so you don't need to update your Model state on return.

Creating Messages

The Message type is opaque, so there are functions to create the four messages you may pass to send.

get : Key -> Message

Return a Message to get a value from local storage.

put : Key -> Maybe Value -> Message

Return a Message to put a value into local storage.

Nothing for the value means to remove the key.

listKeys : Prefix -> Message

Return a Message to list all keys beginning with a prefix.

clear : Prefix -> Message

Return a message to remove all keys beginning with a prefix.

A prefix of "" means to remove all keys.

Conversion to Strings

toString : Message -> String

Convert a Message to a nice-looking human-readable string.

toJsonString : Message -> String

Convert a Message to the same JSON string that gets sent

over the wire to the JS code.

Simulator

makeSimulatedCmdPort : (Value -> msg) -> Value -> Cmd msg

Make a simulated Cmd port.

Non-standard Functions

isLoaded : State -> Bool

Returns true if a Startup message has been processed.

This is sent by the port code after it has initialized.

getPrefix : State -> Prefix

Get the prefix from the state.

encode : Message -> GenericMessage

Turn a Message into a GenericMessage.

decode : GenericMessage -> Result String Message

Turn a GenericMessage into a Message.