billstclair / elm-localstorage / 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 alias Message =
PortFunnel.InternalTypes.Message

An opaque type that represents a message to send to or receive from the JS code.

There are a number of internal messages, but the ones you can use are created by get, put, listkeys, clear, and useSessionStorage.


type Response
    = NoResponse
    | GetResponse ({ label : Label, key : Key, value : Maybe Value })
    | ListKeysResponse ({ label : Label, prefix : String, keys : List Key })

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


type State

Our internal state.


type alias Key =
PortFunnel.InternalTypes.Key

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


type alias Prefix =
PortFunnel.InternalTypes.Prefix

A convenience type for prefixes. Same as String.


type alias Value =
PortFunnel.InternalTypes.Value

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


type alias Label =
PortFunnel.InternalTypes.Label

A convenience type for labels in the store. Same as Maybe String.


type alias JSVersion =
{ v4_1 : () }

This is used to force a major version bump when the JS changes.

You'll usually not use it for anything.

Components of a PortFunnel.FunnelSpec

moduleName : String

The name of this funnel: "LocalStorage".

moduleDesc : PortFunnel.ModuleDesc Message State Response

Our module descriptor.

commander : (PortFunnel.GenericMessage -> Platform.Cmd.Cmd msg) -> Response -> Platform.Cmd.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 -> Platform.Cmd.Cmd msg) -> Message -> State -> Platform.Cmd.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.

getLabeled : String -> Key -> Message

Return a Message to get a labeled value from local storage.

Sometimes the Key alone isn't enough to mark your intention with the return value. In this case you can label the return with a string you can recognize. It will be returned in the label property of the GetResponse.

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.

listKeysLabeled : String -> Prefix -> Message

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

Sometimes the Prefix alone isn't enough to mark your intention with the return value. In this case you can label the return with a string you can recognize. It will be returned in the label property of the ListKeysResponse.

clear : Prefix -> Message

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

A prefix of "" means to remove all keys.

useSessionStorage : Basics.Bool -> Message

Use sessionStorage if the arg is True, or localStorage if False.

The default, if you don't send this message, is localStorage.

sessionStorage is cleared when the browser window is closed.

localStorage persists until explicitly cleared.

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 -> Platform.Cmd.Cmd msg

Make a simulated Cmd port.

Non-standard Functions

isLoaded : State -> Basics.Bool

Returns true if a Startup message has been processed.

This is sent by the port code after it has initialized. Your code can use this to decide whether to use your real outgoing port or the one created by makeSimulatedCmdPort.

getPrefix : State -> Prefix

Get the prefix from the state.

encode : Message -> PortFunnel.GenericMessage

Turn a Message into a GenericMessage.

decode : PortFunnel.GenericMessage -> Result String Message

Turn a GenericMessage into a Message.