Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/therustmonk/elm-mould
Elm client library for mould server
https://github.com/therustmonk/elm-mould
Last synced: 2 days ago
JSON representation
Elm client library for mould server
- Host: GitHub
- URL: https://github.com/therustmonk/elm-mould
- Owner: therustmonk
- License: other
- Created: 2016-10-18T09:07:47.000Z (about 8 years ago)
- Default Branch: master
- Last Pushed: 2017-09-06T22:06:08.000Z (over 7 years ago)
- Last Synced: 2025-01-02T21:12:28.052Z (6 days ago)
- Language: Elm
- Size: 19.5 KB
- Stars: 2
- Watchers: 2
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Mould client for Elm
This package is a client library to interact with [mould][] server.
[mould]: https://github.com/DenisKolodin/mould
## Declare a service
For example, let's declare service of: https://github.com/DenisKolodin/mould-auth
```elm
module MouldAuth exposing (..)import Json.Encode as JS
import Json.Decode as JD exposing (field)
import Mould.Client as Clienttype alias Model = Client.Model () Response
type alias Msg = Client.Msg Request ()
type alias Login = String
type alias Password = Stringtype Request
= DoLogin Login Password
| DoLogout
| ChangePassword Passwordtype Response
= LoginValid Bool
| LoggedOut
| PasswordChangedprotocol : Request -> Client.Call () Response
protocol req =
case req of
DoLogin login password ->
{ action = "do-login"
, payload = JS.object
[ ("login", JS.string login)
, ("password", JS.string password)
]
, decoder = Client.OutOnly <| JD.map LoginValid (field "success" JD.bool)
}
DoLogout ->
{ action = "do-logout"
, payload = JS.null
, decoder = Client.Explicit LoggedOut
}
ChangePassword password ->
{ action = "change-password"
, payload = JS.object
[ ("password", JS.string password)
]
, decoder = Client.Explicit PasswordChanged
}flow () = JS.null
update = Client.update protocol flow
```## Raw usage
```elm
subscriptions : Model -> Sub Msg
subscriptions model =
Sub.batch
[ Mould.interact model.url Interact
, Mould.listen model.url Notification
]type Msg
= Interact Mould.Response
| Notification Mould.NotificationtaskAuthByTokenId = "token-auth-request"
taskAuthByPasswordId = "credential-auth-request"taskIds =
[ taskAuthByTokenId
, taskAuthByPasswordId
]doAuth : Model -> (Model, Cmd Msg)
doAuth model =
let
authCmd =
if model.connected == True then
case model.method of
Just (ByToken token) ->
Mould.send model.url taskAuthByTokenId (authorizeByTokenRequest token)
Just (ByPassword login password) ->
Mould.send model.url taskAuthByPasswordId (authorizeByPasswordRequest login password)
Nothing ->
Cmd.none
else
Cmd.none
in
(model, authCmd)update : Msg -> Model -> (Model, Cmd Msg)
update msg model =
case msg of
Notification notif ->
case notif of
Mould.ConnectionEstablished ->
doAuth { model | connected = True }
Mould.ConnectionLost ->
({ model | authorized = False, connected = False }, Cmd.none)
Mould.BeginTask tag ->
({ model | task = Just tag }, Cmd.none)
Mould.EndTask tag ->
({ model | task = Nothing }, Cmd.none)
_ ->
(model, Cmd.none)
Interact (Mould.Asking tag) ->
if List.member tag taskIds then
(model, Mould.answer model.url tag Nothing)
else
(model, Cmd.none)
Interact (Mould.Done tag) ->
if List.member tag taskIds then
({ model | authorized = True }, Cmd.none)
else
(model, Cmd.none)
Interact response ->
(model, Cmd.none)
```