Skip to content

Instantly share code, notes, and snippets.

@drager
Created September 27, 2017 16:55
Show Gist options
  • Save drager/55f8db784807368d81e48c315a1b8ed2 to your computer and use it in GitHub Desktop.
Save drager/55f8db784807368d81e48c315a1b8ed2 to your computer and use it in GitHub Desktop.
type alias Sessions =
Dict SessionId Connection
type alias Connection =
{ host : String
, portNumber : Int
, username : String
, connected : Bool
}
type alias SessionId =
String
update : Msg -> Model -> ( Model, Cmd Msg )
update msg model =
case msg of
Connect connectionInfo (Ok sessionId) ->
let
a =
Debug.log "CONNECTION" (storageEncoder sessionId { connectionInfo | connected = True } |> toString)
in
( { model | sessions = Dict.insert sessionId { connectionInfo | connected = True } model.sessions }
, Cmd.batch
[ pushItemInLocalStorage ( storageKey, (storageEncoder sessionId connectionInfo) )
, pushItemInSessionStorage ( storageKey, (storageEncoder sessionId { connectionInfo | connected = True }) )
]
)
connectionDecoder : Decode.Decoder Connection
connectionDecoder =
Decode.map4 Connection
(Decode.field "host" Decode.string)
(Decode.field "port" Decode.int)
(Decode.field "username" Decode.string)
(Decode.field "connected" Decode.bool)
storageDecoder : Decode.Decoder Sessions
storageDecoder =
Decode.dict connectionDecoder
storageEncoder : SessionId -> Connection -> Encode.Value
storageEncoder sessionId connection =
let
_ =
Debug.log "In encoder before" (connection |> toString)
log =
Debug.log "In encoder" ([ ( sessionId, (connectionEncoder connection) ) ] |> toString)
attributes =
[ ( sessionId, (connectionEncoder connection) ) ]
in
Encode.object attributes
-- LOGS
-- In encoder: "[(\"ecd14412-7b0c-4e62-91bd-889fb17ccf96\",{ host = \"localhost\", port = 5000, username = \"test\"})]"
-- In encoder before: "{ host = \"localhost\", portNumber = 5000, username = \"test\", connected = True }"
-- CONNECTION: "{ ecd14412-7b0c-4e62-91bd-889fb17ccf96 = { host = \"localhost\", port = 5000, username = \"test\" } }"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment