the-sett / elm-localstorage / LocalStorage

A minimal local storage API that mirrors the raw API very closely.

User supplied port signatures.


type alias ClearPort msg =
String -> Platform.Cmd.Cmd msg

Port to clear the whole area of local storage underneath a prefix.


type alias GetItemPort msg =
String -> Platform.Cmd.Cmd msg

Port to fetch an item by its key and prefix.


type alias SetItemPort msg =
( String
, Json.Encode.Value ) -> Platform.Cmd.Cmd ms
)

Port to set the value of an item by key and prefix.


type alias ListKeysPort msg =
String -> Platform.Cmd.Cmd msg

Port to list the available local storage keys underneath a prefix.


type alias ResponsePort msg =
(Json.Encode.Value -> msg) -> Platform.Sub.Sub msg

Subscription port to listen to all operation responses from local storage with.

Local storage handle and constructor.


type LocalStorage msg

The type of the local storage handle, required to perform operations on local storage.

make : GetItemPort msg -> SetItemPort msg -> ClearPort msg -> ListKeysPort msg -> String -> LocalStorage msg

Builds local storage using the supplied port implementations and a prefix to namespace all keys stored in that area of local storage.

Operations on local storage.

clear : LocalStorage msg -> Platform.Cmd.Cmd msg

Clears the whole area of local storage underneath a prefix.

getItem : LocalStorage msg -> String -> Platform.Cmd.Cmd msg

Fetches an item by its key and prefix.

setItem : LocalStorage msg -> String -> Json.Encode.Value -> Platform.Cmd.Cmd msg

Sets the value of an item by key and prefix.

listKeys : LocalStorage msg -> String -> Platform.Cmd.Cmd msg

Lists the available local storage keys underneath a prefix.

Responses from local storage.


type Response
    = Item String Json.Encode.Value
    | ItemNotFound String
    | KeyList (List String)
    | Error String

The possible local storage operation responses.

responseHandler : (Response -> msg) -> LocalStorage msg -> Json.Encode.Value -> msg

Creates a response handler to use with the ResponsePort subscription port.

The operation, key and JSON value message builder combined with the storage prefix are needed to create a response handler.