Skip to content

Commit 87eeddd

Browse files
authored
Merge pull request #63 from lamdera/copilot/fix-62
Add WebGL.Texture support for serialization and migration like File.File
2 parents 8cfc062 + 9e7f6e3 commit 87eeddd

File tree

11 files changed

+220
-4
lines changed

11 files changed

+220
-4
lines changed

extra/Lamdera/Evergreen/Snapshot.hs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -649,6 +649,7 @@ canonicalToFt version scope interfaces recursionSet canonical tvarMap =
649649

650650
-- Kernel concessions for Frontend Model and Msg
651651
("elm", "file", "File", "File") -> ("File.File", Set.singleton moduleName, Map.empty)
652+
("elm-explorations", "webgl", "WebGL.Texture", "Texture") -> ("WebGL.Texture.Texture", Set.singleton moduleName, Map.empty)
652653

653654

654655
-- @TODO improve; These aliases will show up as VirtualDom errors which might confuse users

extra/Lamdera/TypeHash.hs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -275,7 +275,7 @@ canonicalToDiffableType targetName interfaces recursionSet canonical tvarMap =
275275
kernelErrorBrowserOnly =
276276
case identifier of
277277
(author, pkg, module_, tipe) ->
278-
DError $ "must not contain the Frontent-only type `" <> tipe <> "` from " <> author <> "/" <> pkg <> ":" <> module_
278+
DError $ "must not contain the Frontend-only type `" <> tipe <> "` from " <> author <> "/" <> pkg <> ":" <> module_
279279

280280
lamderaCodecsError =
281281
case identifier of
@@ -394,6 +394,12 @@ canonicalToDiffableType targetName interfaces recursionSet canonical tvarMap =
394394
else
395395
kernelErrorBrowserOnly
396396

397+
("elm-explorations", "webgl", "WebGL.Texture", "Texture") ->
398+
if targetName `elem` ["FrontendMsg", "FrontendModel"] then
399+
DKernelBrowser "WebGL.Texture.Texture"
400+
else
401+
kernelErrorBrowserOnly
402+
397403
-- Lamdera codecs types shouldn't be used in the core type tree, as the core types must be serialisable,
398404
-- which means they need the auto-gen encoders/decoders, but those can only be generated by depending on
399405
-- qualified references to the lamdera/codecs module helpers.

extra/Lamdera/Wire3/Decoder.hs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -350,6 +350,7 @@ decoderForType ifaces cname tipe =
350350

351351
-- Frontend only JS reference types
352352
TType (Module.Canonical (Name "elm" "file") "File") "File" params -> callDecoder "decodeRef" tipe
353+
TType (Module.Canonical (Name "elm-explorations" "webgl") "WebGL.Texture") "Texture" params -> callDecoder "decodeRef" tipe
353354

354355

355356
TType moduleName typeName params ->

extra/Lamdera/Wire3/Encoder.hs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,8 @@ encoderForType depth ifaces cname tipe =
214214
-- Frontend only JS reference types
215215
TType (Module.Canonical (Name "elm" "file") "File") "File" _ ->
216216
(a (VarForeign mLamdera_Wire "encodeRef" (Forall Map.empty (TLambda tipe tLamdera_Wire_Encoder))))
217+
TType (Module.Canonical (Name "elm-explorations" "webgl") "WebGL.Texture") "Texture" _ ->
218+
(a (VarForeign mLamdera_Wire "encodeRef" (Forall Map.empty (TLambda tipe tLamdera_Wire_Encoder))))
217219

218220

219221
TType moduleName typeName params ->

extra/Lamdera/Wire3/Helpers.hs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,7 @@ isUnsupportedKernelType tipe =
123123
-- JS types we are supporting through JS ref encodings. These serialisations
124124
-- CANNOT BE DECODED OUTSIDE OF THE JS SCOPE THEY WERE ENCODED IN!
125125
TType (Module.Canonical (Name "elm" "file") "File") "File" _ -> False
126+
TType (Module.Canonical (Name "elm-explorations" "webgl") "WebGL.Texture") "Texture" _ -> False
126127

127128

128129
TAlias moduleName typeName tvars (Holey tipe) -> isUnsupportedKernelType tipe

test/Test.hs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,8 @@ import qualified Test.Ext.ElmPages.Check
2121
import qualified Test.TypeHashes
2222
import qualified Test.JsOutput
2323
import qualified Test.WebGL
24-
<<<<<<< HEAD
2524
import qualified Test.Lamdera.Live
26-
=======
2725
import qualified Test.BackwardsCompat
28-
>>>>>>> bdcb71f7 (Test Lamdera HTML injections don't happen on vanilla Elm projects)
2926

3027
import qualified Test.Lamdera.Evergreen.TestMigrationHarness
3128
import qualified Test.Lamdera.Evergreen.TestMigrationGenerator

test/Test/WebGL.hs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,4 +37,28 @@ suite = tests
3737
actual <- catchOutput $ Lamdera.Compile.makeDev project [ "src/Triangle.elm" ]
3838

3939
expectTextContains actual "Success! Compiled 1 module."
40+
41+
42+
-- @TODO currently the type restriction checks only happen in lamdera check
43+
-- We should probably move them to lamdera make now that we have the isLamdera detection
44+
-- but that's a fair bit of work – so revisit these tests when that's done
45+
46+
, pending $ scope "compile Elm app with WebGL.Texture in FrontendModel" $ do
47+
project <- io $ Lamdera.Relative.requireDir "test/scenario-webgl-texture"
48+
49+
_ <- io $ rmdir (project </> "elm-stuff")
50+
51+
actual <- catchOutput $ Lamdera.Compile.makeDev project [ "src/Frontend.elm", "src/Types.elm" ]
52+
53+
expectTextContains actual "Success! Compiled 3 modules."
54+
55+
, pending $ scope "compilation should fail with WebGL.Texture in backend contexts" $ do
56+
project <- io $ Lamdera.Relative.requireDir "test/scenario-webgl-texture"
57+
58+
_ <- io $ rmdir (project </> "elm-stuff")
59+
60+
actual <- catchOutput $ Lamdera.Compile.makeDev project [ "src/Backend.elm" ]
61+
62+
-- Should fail with kernel error for browser-only types
63+
expectTextContains actual "can only be used in the frontend"
4064
]
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
{
2+
"type": "application",
3+
"source-directories": [
4+
"src"
5+
],
6+
"elm-version": "0.19.1",
7+
"dependencies": {
8+
"direct": {
9+
"elm/browser": "1.0.2",
10+
"elm/core": "1.0.5",
11+
"elm/html": "1.0.0",
12+
"elm/json": "1.1.3",
13+
"elm/url": "1.0.0",
14+
"elm-explorations/linear-algebra": "1.0.3",
15+
"elm-explorations/webgl": "1.1.3",
16+
"lamdera/core": "1.0.0"
17+
},
18+
"indirect": {
19+
"elm/bytes": "1.0.8",
20+
"elm/file": "1.0.5",
21+
"elm/http": "2.0.0",
22+
"elm/time": "1.0.0",
23+
"elm/virtual-dom": "1.0.4",
24+
"lamdera/codecs": "1.0.0"
25+
}
26+
},
27+
"test-dependencies": {
28+
"direct": {},
29+
"indirect": {}
30+
}
31+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
module Backend exposing (..)
2+
3+
import Lamdera exposing (ClientId, SessionId)
4+
import Types exposing (..)
5+
6+
7+
type alias Model =
8+
BackendModel
9+
10+
11+
app =
12+
Lamdera.backend
13+
{ init = init
14+
, update = update
15+
, updateFromFrontend = updateFromFrontend
16+
, subscriptions = subscriptions
17+
}
18+
19+
20+
init : ( Model, Cmd BackendMsg )
21+
init =
22+
( { message = "Hello from backend!", texture = Nothing }
23+
, Cmd.none
24+
)
25+
26+
27+
update : BackendMsg -> Model -> ( Model, Cmd BackendMsg )
28+
update msg model =
29+
case msg of
30+
NoOpBackendMsg ->
31+
( model, Cmd.none )
32+
33+
34+
updateFromFrontend : SessionId -> ClientId -> ToBackend -> Model -> ( Model, Cmd BackendMsg )
35+
updateFromFrontend sessionId clientId msg model =
36+
case msg of
37+
NoOpToBackend ->
38+
( model, Cmd.none )
39+
40+
41+
subscriptions : Model -> Sub BackendMsg
42+
subscriptions model =
43+
Sub.none
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
module Frontend exposing (..)
2+
3+
import Browser exposing (UrlRequest)
4+
import Browser.Navigation as Nav
5+
import Html
6+
import Url
7+
import WebGL.Texture as Texture
8+
import Lamdera
9+
import Types exposing (..)
10+
11+
12+
type alias Model = FrontendModel
13+
14+
15+
app =
16+
Lamdera.frontend
17+
{ init = init
18+
, onUrlRequest = UrlClicked
19+
, onUrlChange = UrlChanged
20+
, update = update
21+
, updateFromBackend = updateFromBackend
22+
, subscriptions = \m -> Sub.none
23+
, view = view
24+
}
25+
26+
27+
init : Url.Url -> Nav.Key -> ( Model, Cmd FrontendMsg )
28+
init url key =
29+
( { key = key
30+
, texture = Nothing
31+
}
32+
, Cmd.none
33+
)
34+
35+
36+
update : FrontendMsg -> Model -> ( Model, Cmd FrontendMsg )
37+
update msg model =
38+
case msg of
39+
UrlClicked urlRequest ->
40+
case urlRequest of
41+
Browser.Internal url ->
42+
( model, Nav.pushUrl model.key (Url.toString url) )
43+
44+
Browser.External url ->
45+
( model, Nav.load url )
46+
47+
UrlChanged url ->
48+
( model, Cmd.none )
49+
50+
TextureLoaded result ->
51+
case result of
52+
Ok texture ->
53+
( { model | texture = Just texture }, Cmd.none )
54+
55+
Err _ ->
56+
( model, Cmd.none )
57+
58+
NoOpFrontendMsg ->
59+
( model, Cmd.none )
60+
61+
62+
updateFromBackend : ToFrontend -> Model -> ( Model, Cmd FrontendMsg )
63+
updateFromBackend msg model =
64+
case msg of
65+
NoOpToFrontend ->
66+
( model, Cmd.none )
67+
68+
69+
view : Model -> Browser.Document FrontendMsg
70+
view model =
71+
{ title = ""
72+
, body = [ Html.text "Hello" ]
73+
}

0 commit comments

Comments
 (0)