Skip to content

Commit b8bd828

Browse files
committed
Update remainder of the examples and test apps to new backend API
Complete updating example project codes to the backend API introduced with version 2021-08-14.
1 parent 5660da3 commit b8bd828

File tree

32 files changed

+1703
-4474
lines changed

32 files changed

+1703
-4474
lines changed

implement/example-apps/demo-backend-state/src/Backend/InterfaceToHost.elm

Lines changed: 0 additions & 404 deletions
This file was deleted.

implement/example-apps/demo-backend-state/src/Backend/Main.elm

Lines changed: 54 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,16 @@
11
module Backend.Main exposing
22
( State
3-
, interfaceToHost_initState
4-
, interfaceToHost_processEvent
5-
, processEvent
3+
, backendMain
64
)
75

8-
import Backend.InterfaceToHost as InterfaceToHost
96
import Backend.State exposing (CustomType(..), CustomTypeWithTypeParameter(..), RecursiveType(..), valueForOpaqueCustomType)
107
import Base64
118
import Bytes.Encode
129
import Common
1310
import CompilationInterface.ElmMake
1411
import CompilationInterface.GenerateJsonCoders
1512
import Dict
13+
import ElmFullstack
1614
import Json.Encode
1715
import ListDict
1816
import Set
@@ -23,72 +21,71 @@ type alias State =
2321
Backend.State.State
2422

2523

26-
interfaceToHost_processEvent : String -> State -> ( State, String )
27-
interfaceToHost_processEvent =
28-
InterfaceToHost.wrapForSerialInterface_processEvent processEvent
24+
backendMain : ElmFullstack.BackendConfig State
25+
backendMain =
26+
{ init = ( initState, [] )
27+
, subscriptions = subscriptions
28+
}
2929

3030

31-
processEvent : InterfaceToHost.AppEvent -> State -> ( State, InterfaceToHost.AppEventResponse )
32-
processEvent hostEvent stateBefore =
33-
case hostEvent of
34-
InterfaceToHost.HttpRequestEvent httpRequestEvent ->
35-
let
36-
state =
37-
{ stateBefore
38-
| httpRequestsCount = stateBefore.httpRequestsCount + 1
39-
, lastHttpRequests = httpRequestEvent :: stateBefore.lastHttpRequests |> List.take 4
40-
}
31+
subscriptions : State -> ElmFullstack.BackendSubs State
32+
subscriptions _ =
33+
{ httpRequest = updateForHttpRequestEvent
34+
, posixTimeIsPast = Nothing
35+
}
4136

42-
httpResponse =
43-
if
44-
httpRequestEvent.request.uri
45-
|> Url.fromString
46-
|> Maybe.map urlLeadsToFrontendHtmlDocument
47-
|> Maybe.withDefault False
48-
then
49-
{ statusCode = 200
50-
, bodyAsBase64 = Just CompilationInterface.ElmMake.elm_make__debug__base64____src_FrontendWeb_Main_elm
51-
, headersToAdd = []
52-
}
5337

54-
else
55-
{ statusCode = 200
56-
, bodyAsBase64 =
57-
[ Common.describeApp
58-
, "I received "
59-
++ (state.httpRequestsCount |> String.fromInt)
60-
++ " HTTP requests."
61-
, "Here is a serialized representation of the backend state:"
62-
, state |> CompilationInterface.GenerateJsonCoders.jsonEncodeBackendState |> Json.Encode.encode 4
63-
]
64-
|> String.join "\n"
65-
|> Bytes.Encode.string
66-
|> Bytes.Encode.encode
67-
|> Base64.fromBytes
68-
, headersToAdd = []
69-
}
70-
in
71-
( state
72-
, InterfaceToHost.passiveAppEventResponse
73-
|> InterfaceToHost.withCompleteHttpResponsesAdded
74-
[ { httpRequestId = httpRequestEvent.httpRequestId, response = httpResponse }
75-
]
76-
)
38+
updateForHttpRequestEvent : ElmFullstack.HttpRequestEventStruct -> State -> ( State, ElmFullstack.BackendCmds State )
39+
updateForHttpRequestEvent httpRequestEvent stateBefore =
40+
let
41+
state =
42+
{ stateBefore
43+
| httpRequestsCount = stateBefore.httpRequestsCount + 1
44+
, lastHttpRequests = httpRequestEvent :: stateBefore.lastHttpRequests |> List.take 4
45+
}
7746

78-
InterfaceToHost.TaskCompleteEvent _ ->
79-
( stateBefore, InterfaceToHost.passiveAppEventResponse )
47+
httpResponse =
48+
if
49+
httpRequestEvent.request.uri
50+
|> Url.fromString
51+
|> Maybe.map urlLeadsToFrontendHtmlDocument
52+
|> Maybe.withDefault False
53+
then
54+
{ statusCode = 200
55+
, bodyAsBase64 = Just CompilationInterface.ElmMake.elm_make__debug__base64____src_FrontendWeb_Main_elm
56+
, headersToAdd = []
57+
}
8058

81-
InterfaceToHost.ArrivedAtTimeEvent _ ->
82-
( stateBefore, InterfaceToHost.passiveAppEventResponse )
59+
else
60+
{ statusCode = 200
61+
, bodyAsBase64 =
62+
[ Common.describeApp
63+
, "I received "
64+
++ (state.httpRequestsCount |> String.fromInt)
65+
++ " HTTP requests."
66+
, "Here is a serialized representation of the backend state:"
67+
, state |> CompilationInterface.GenerateJsonCoders.jsonEncodeBackendState |> Json.Encode.encode 4
68+
]
69+
|> String.join "\n"
70+
|> Bytes.Encode.string
71+
|> Bytes.Encode.encode
72+
|> Base64.fromBytes
73+
, headersToAdd = []
74+
}
75+
in
76+
( state
77+
, [ ElmFullstack.RespondToHttpRequest { httpRequestId = httpRequestEvent.httpRequestId, response = httpResponse }
78+
]
79+
)
8380

8481

8582
urlLeadsToFrontendHtmlDocument : Url.Url -> Bool
8683
urlLeadsToFrontendHtmlDocument url =
8784
not (url.path == "/api" || (url.path |> String.startsWith "/api/"))
8885

8986

90-
interfaceToHost_initState : State
91-
interfaceToHost_initState =
87+
initState : State
88+
initState =
9289
{ httpRequestsCount = 0
9390
, lastHttpRequests = []
9491
, tuple2 = ( 123, "second element in tuple A" )

implement/example-apps/demo-backend-state/src/Backend/State.elm

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,16 @@ module Backend.State exposing
77
, valueForOpaqueCustomType
88
)
99

10-
import Backend.InterfaceToHost as InterfaceToHost
1110
import Bytes
1211
import Dict
12+
import ElmFullstack
1313
import ListDict
1414
import Set
1515

1616

1717
type alias State =
1818
{ httpRequestsCount : Int
19-
, lastHttpRequests : List InterfaceToHost.HttpRequestEventStructure
19+
, lastHttpRequests : List ElmFullstack.HttpRequestEventStruct
2020
, tuple2 : Tuple2
2121
, tuple3 : Tuple3
2222
, list_custom_type : List CustomType
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
module ElmFullstack exposing (..)
2+
3+
4+
type alias BackendConfig state =
5+
{ init : ( state, BackendCmds state )
6+
, subscriptions : state -> BackendSubs state
7+
}
8+
9+
10+
type alias BackendSubs state =
11+
{ httpRequest : HttpRequestEventStruct -> state -> ( state, BackendCmds state )
12+
, posixTimeIsPast :
13+
Maybe
14+
{ minimumPosixTimeMilli : Int
15+
, update : { currentPosixTimeMilli : Int } -> state -> ( state, BackendCmds state )
16+
}
17+
}
18+
19+
20+
type alias BackendCmds state =
21+
List (BackendCmd state)
22+
23+
24+
type BackendCmd state
25+
= RespondToHttpRequest RespondToHttpRequestStruct
26+
| CreateVolatileProcess (CreateVolatileProcessStruct state)
27+
| RequestToVolatileProcess (RequestToVolatileProcessStruct state)
28+
| TerminateVolatileProcess TerminateVolatileProcessStruct
29+
30+
31+
type alias HttpRequestEventStruct =
32+
{ httpRequestId : String
33+
, posixTimeMilli : Int
34+
, requestContext : HttpRequestContext
35+
, request : HttpRequestProperties
36+
}
37+
38+
39+
type alias HttpRequestContext =
40+
{ clientAddress : Maybe String
41+
}
42+
43+
44+
type alias HttpRequestProperties =
45+
{ method : String
46+
, uri : String
47+
, bodyAsBase64 : Maybe String
48+
, headers : List HttpHeader
49+
}
50+
51+
52+
type alias RespondToHttpRequestStruct =
53+
{ httpRequestId : String
54+
, response : HttpResponse
55+
}
56+
57+
58+
type alias HttpResponse =
59+
{ statusCode : Int
60+
, bodyAsBase64 : Maybe String
61+
, headersToAdd : List HttpHeader
62+
}
63+
64+
65+
type alias HttpHeader =
66+
{ name : String
67+
, values : List String
68+
}
69+
70+
71+
type alias CreateVolatileProcessStruct state =
72+
{ programCode : String
73+
, update : CreateVolatileProcessResult -> state -> ( state, BackendCmds state )
74+
}
75+
76+
77+
type alias CreateVolatileProcessResult =
78+
Result CreateVolatileProcessErrorStruct CreateVolatileProcessComplete
79+
80+
81+
type alias CreateVolatileProcessErrorStruct =
82+
{ exceptionToString : String
83+
}
84+
85+
86+
type alias CreateVolatileProcessComplete =
87+
{ processId : String }
88+
89+
90+
type alias RequestToVolatileProcessStruct state =
91+
{ processId : String
92+
, request : String
93+
, update : RequestToVolatileProcessResult -> state -> ( state, BackendCmds state )
94+
}
95+
96+
97+
type alias RequestToVolatileProcessResult =
98+
Result RequestToVolatileProcessError RequestToVolatileProcessComplete
99+
100+
101+
type RequestToVolatileProcessError
102+
= ProcessNotFound
103+
104+
105+
type alias RequestToVolatileProcessComplete =
106+
{ exceptionToString : Maybe String
107+
, returnValueToString : Maybe String
108+
, durationInMilliseconds : Int
109+
}
110+
111+
112+
type alias TerminateVolatileProcessStruct =
113+
{ processId : String }

0 commit comments

Comments
 (0)