Add send message event as Task

4-compiler-bug
Bram 2024-05-25 17:03:42 +02:00
parent 4f08dd1176
commit 7a75bffbfb
3 changed files with 56 additions and 5 deletions

View File

@ -1,7 +1,7 @@
module Internal.Api.Chain exposing
( TaskChain, CompleteChain
, IdemChain, toTask
, fail, succeed, andThen
, fail, succeed, andThen, catchWith
)
{-|
@ -27,7 +27,7 @@ avoid leaking values passing through the API in unexpected ways.
## Operations
@docs fail, succeed, andThen
@docs fail, succeed, andThen, catchWith
-}
@ -127,7 +127,7 @@ andThen f2 f1 =
{-| When an error has occurred, "fix" it with an artificial task chain result.
-}
catchWith : (err -> TaskChainPiece u a b) -> TaskChain err u a b -> TaskChain err u a b
catchWith : (err -> TaskChainPiece u a b) -> TaskChain err u a b -> TaskChain err2 u a b
catchWith onErr f =
onError (\e -> succeed <| onErr e) f
@ -173,7 +173,7 @@ onError onErr f =
|> Task.onError
(\old ->
{ contextChange = identity
, logs = old.logs
, logs = old.logs -- TODO: Log caught errors
, messages = old.messages
}
|> succeed

View File

@ -1,4 +1,7 @@
module Internal.Api.Task exposing (Task, run)
module Internal.Api.Task exposing
( Task, run
, sendMessageEvent
)
{-|
@ -17,6 +20,11 @@ up-to-date.
@docs Task, run
## Tasks
@docs sendMessageEvent
-}
import Internal.Api.BaseUrl.Api
@ -24,8 +32,10 @@ import Internal.Api.Chain as C
import Internal.Api.LoginWithUsernameAndPassword.Api
import Internal.Api.Now.Api
import Internal.Api.Request as Request
import Internal.Api.SendMessageEvent.Api
import Internal.Api.Versions.Api
import Internal.Config.Log exposing (Log, log)
import Internal.Tools.Json as Json
import Internal.Values.Context as Context exposing (APIContext)
import Internal.Values.Envelope exposing (EnvelopeUpdate(..))
import Internal.Values.Room exposing (RoomUpdate(..))
@ -128,6 +138,25 @@ getVersions c =
Internal.Api.Versions.Api.versions c
finishTask : UFTask {} b -> Task
finishTask uftask =
uftask
|> C.andThen
(C.succeed
{ messages = []
, logs = []
, contextChange = Context.reset
}
)
|> C.catchWith
(\_ ->
{ messages = [] -- TODO: Maybe categorize errors?
, logs = [ log.warn "Encountered unhandled error" ]
, contextChange = Context.reset
}
)
{-| Establish a Task Chain context where the base URL and supported list of
versions are known.
-}
@ -147,6 +176,15 @@ makeVBA =
|> C.andThen getAccessToken
{-| Send a message event to a room.
-}
sendMessageEvent : { content : Json.Value, eventType : String, roomId : String, transactionId : String } -> Task
sendMessageEvent input =
makeVBA
|> C.andThen (Internal.Api.SendMessageEvent.Api.sendMessageEvent input)
|> finishTask
{-| Transform a completed task into a Cmd.
-}
run : (Backpack -> msg) -> Task -> APIContext {} -> Cmd msg

View File

@ -6,6 +6,7 @@ module Internal.Values.Context exposing
, setNow, getNow
, setTransaction, getTransaction
, Versions, setVersions, getVersions
, reset
)
{-| The Context is the set of variables that the user (mostly) cannot control.
@ -53,6 +54,11 @@ information that can be inserted.
@docs Versions, setVersions, getVersions
### Reset
@docs reset
-}
import Internal.Config.Leaks as L
@ -317,6 +323,13 @@ mostPopularToken c =
|> Maybe.map .value
{-| Reset the phantom type of the Context, effectively forgetting all values.
-}
reset : APIContext a -> APIContext {}
reset (APIContext c) =
APIContext c
{-| Get an inserted access token.
-}
getAccessToken : APIContext { a | accessToken : () } -> String