Skip to content

Commit

Permalink
Change subscriptions based on #32.
Browse files Browse the repository at this point in the history
  • Loading branch information
dillonkearns committed Feb 8, 2018
1 parent 78f8bd0 commit ffbaa5f
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 22 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG-ELM-PACKAGE.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.

## [Unreleased]

## [10.0.0] - 2018-02-07

### Changed

* Update model to allow more flexibility based on #32.

## [9.1.0] - 2018-02-06

### Added
Expand Down
8 changes: 6 additions & 2 deletions examples/src/Subscription.elm
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,11 @@ update : Msg -> Model -> ( Model, Cmd Msg )
update msg model =
case msg of
GraphqlSubscriptionMsg graphqlSubscriptionMsg ->
Graphqelm.Subscription.update graphqlSubscriptionMsg model
let
( newModel, cmd ) =
Graphqelm.Subscription.update graphqlSubscriptionMsg model.graphqlSubscriptionModel
in
( { model | graphqlSubscriptionModel = newModel }, cmd )

SendMessage phrase ->
( model, Graphqelm.Subscription.sendMutation model.graphqlSubscriptionModel (sendChatMessage model.characterId phrase) )
Expand All @@ -228,7 +232,7 @@ update msg model =

subscriptions : Model -> Sub Msg
subscriptions model =
Graphqelm.Subscription.listen GraphqlSubscriptionMsg model
Graphqelm.Subscription.listen GraphqlSubscriptionMsg model.graphqlSubscriptionModel


main : Program Never Model Msg
Expand Down
38 changes: 18 additions & 20 deletions src/Graphqelm/Subscription.elm
Original file line number Diff line number Diff line change
Expand Up @@ -138,9 +138,9 @@ onStatusChanged onStatusChanged (Model model) =
-}
listen :
(Msg decodesTo -> msg)
-> { model | graphqlSubscriptionModel : Model msg decodesTo }
-> Model msg decodesTo
-> Sub msg
listen toMsg { graphqlSubscriptionModel } =
listen toMsg graphqlSubscriptionModel =
case graphqlSubscriptionModel of
Model model ->
Sub.batch
Expand All @@ -162,14 +162,14 @@ for and respond to Subscription communications.
-}
update :
Msg decodesTo
-> { model | graphqlSubscriptionModel : Model msg decodesTo }
-> ( { model | graphqlSubscriptionModel : Model msg decodesTo }, Cmd msg )
update msg ({ graphqlSubscriptionModel } as fullModel) =
-> Model msg decodesTo
-> ( Model msg decodesTo, Cmd msg )
update msg graphqlSubscriptionModel =
case graphqlSubscriptionModel of
Model model ->
case msg of
SendHeartbeat time ->
( fullModel |> incrementRefId
( graphqlSubscriptionModel |> incrementRefId
, WebSocket.send model.socketUrl (model.protocol.heartBeatMessage model.referenceId |> Encode.encode 0)
)

Expand All @@ -184,41 +184,41 @@ update msg ({ graphqlSubscriptionModel } as fullModel) =
case response of
Ok Protocol.HealthStatus ->
if model.status == Uninitialized then
( fullModel |> incrementRefId
( graphqlSubscriptionModel |> incrementRefId
, WebSocket.send
model.socketUrl
(documentRequest (Graphqelm.Document.serializeSubscription model.subscriptionDocument))
)
|> setStatus Connected
else
( fullModel, Cmd.none )
( graphqlSubscriptionModel, Cmd.none )

Ok (Protocol.SubscriptionDataReceived data) ->
( fullModel, Task.succeed data |> Task.perform model.onData )
( graphqlSubscriptionModel, Task.succeed data |> Task.perform model.onData )

Err errorString ->
let
_ =
Debug.log "Error" errorString
in
( fullModel, Cmd.none )
( graphqlSubscriptionModel, Cmd.none )

Ok (Protocol.Ignored ignoredContent) ->
let
_ =
Debug.log "Ignored: " ignoredContent
in
( fullModel, Cmd.none )
( graphqlSubscriptionModel, Cmd.none )


setStatus :
Status
-> ( { a | graphqlSubscriptionModel : Model msg decodesTo }, Cmd msg )
-> ( { a | graphqlSubscriptionModel : Model msg decodesTo }, Cmd msg )
setStatus newStatus ( { graphqlSubscriptionModel } as fullModel, cmds ) =
-> ( Model msg decodesTo, Cmd msg )
-> ( Model msg decodesTo, Cmd msg )
setStatus newStatus ( graphqlSubscriptionModel, cmds ) =
case graphqlSubscriptionModel of
Model model ->
( { fullModel | graphqlSubscriptionModel = Model { model | status = newStatus } }
( Model { model | status = newStatus }
, Cmd.batch
[ cmds
, case model.onStatusChanged of
Expand All @@ -231,10 +231,8 @@ setStatus newStatus ( { graphqlSubscriptionModel } as fullModel, cmds ) =
)


incrementRefId :
{ a | graphqlSubscriptionModel : Model msg decodesTo }
-> { a | graphqlSubscriptionModel : Model msg decodesTo }
incrementRefId ({ graphqlSubscriptionModel } as fullModel) =
incrementRefId : Model msg decodesTo -> Model msg decodesTo
incrementRefId graphqlSubscriptionModel =
case graphqlSubscriptionModel of
Model model ->
{ fullModel | graphqlSubscriptionModel = Model { model | referenceId = model.referenceId + 1 } }
Model { model | referenceId = model.referenceId + 1 }

0 comments on commit ffbaa5f

Please sign in to comment.