Skip to content

Commit e19c817

Browse files
committed
Types constraint shift for Bridge.elm should refresh on file changes else user gets stale type inference errors when applying the technique
1 parent 58c58b2 commit e19c817

File tree

2 files changed

+35
-33
lines changed

2 files changed

+35
-33
lines changed

extra/Lamdera/Constrain.hs

Lines changed: 28 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -21,43 +21,36 @@ import Lamdera.Project (lamderaCore)
2121

2222

2323
constrain pkg modu name annotation@(Can.Forall freevars t) region expected =
24-
if pkg == lamderaCore && modu == "Lamdera" && name == "sendToBackend" then
25-
do
26-
sourcePkg <- determineTypesLocation "ToBackend"
27-
let
28-
fn (Can.TVar "toBackend") = Can.TType (ModuleName.Canonical Pkg.dummyName sourcePkg) "ToBackend" []
29-
fn x = x
30-
pure $ CForeign region name (Can.Forall freevars (mapTvars fn t)) expected
31-
32-
else if pkg == lamderaCore && modu == "Lamdera" && name == "sendToFrontend" then
33-
do
34-
sourcePkg <- determineTypesLocation "ToFrontend"
35-
let
36-
fn (Can.TVar "toFrontend") = Can.TType (ModuleName.Canonical Pkg.dummyName sourcePkg) "ToFrontend" []
37-
fn x = x
38-
pure $ CForeign region name (Can.Forall freevars (mapTvars fn t)) expected
39-
40-
else if pkg == lamderaCore && modu == "Lamdera" && name == "broadcast" then
41-
do
42-
sourcePkg <- determineTypesLocation "ToFrontend"
43-
let
44-
fn (Can.TVar "toFrontend") = Can.TType (ModuleName.Canonical Pkg.dummyName sourcePkg) "ToFrontend" []
45-
fn x = x
46-
pure $ CForeign region name (Can.Forall freevars (mapTvars fn t)) expected
24+
if pkg == lamderaCore && modu == "Lamdera" then
25+
26+
case name of
27+
"sendToBackend" -> do
28+
sourcePkg <- determineTypesLocation "ToBackend"
29+
let
30+
fn (Can.TVar "toBackend") = Can.TType (ModuleName.Canonical Pkg.dummyName sourcePkg) "ToBackend" []
31+
fn x = x
32+
pure $ CForeign region name (Can.Forall freevars (mapTvars fn t)) expected
33+
34+
"sendToFrontend" -> do
35+
sourcePkg <- determineTypesLocation "ToFrontend"
36+
let
37+
fn (Can.TVar "toFrontend") = Can.TType (ModuleName.Canonical Pkg.dummyName sourcePkg) "ToFrontend" []
38+
fn x = x
39+
pure $ CForeign region name (Can.Forall freevars (mapTvars fn t)) expected
40+
41+
"broadcast" -> do
42+
sourcePkg <- determineTypesLocation "ToFrontend"
43+
let
44+
fn (Can.TVar "toFrontend") = Can.TType (ModuleName.Canonical Pkg.dummyName sourcePkg) "ToFrontend" []
45+
fn x = x
46+
pure $ CForeign region name (Can.Forall freevars (mapTvars fn t)) expected
47+
48+
_ ->
49+
pure $ CForeign region name annotation expected
4750

4851
else
4952
pure $ CForeign region name annotation expected
5053

51-
-- @LEGACY, left here in case we run into issues, but likely no longer needed.
52-
-- Since we don't have type annotations on codecs, we have exposed top-level
53-
-- things without type annotations, which means that we see bugs in the type
54-
-- inference engine that shouldn't be there. This is a hotfix for such a bug,
55-
-- where the forall. part of the type doesn't hold all the tvars used in the
56-
-- type, so we inject any missning tvars and hope it works out.
57-
-- @LAMDERA todo legacy from Haskell transpilation, re-evalutate and remove
58-
-- else
59-
-- pure $ CForeign region name (Can.Forall (freevars <> getFreevars t) t) expected -- NOTE: prefer freevars over getFreevars if there's a conflict
60-
6154

6255
determineTypesLocation :: Text -> IO N.Name
6356
determineTypesLocation tipe = do
@@ -94,6 +87,8 @@ determineTypesLocation tipe = do
9487
typeLocations :: IORef (Map.Map Text N.Name)
9588
typeLocations = unsafePerformIO $ newIORef Map.empty
9689

90+
resetTypeLocations :: IO ()
91+
resetTypeLocations = atomicModifyIORef typeLocations (\m -> (Map.empty, ()))
9792

9893
rememberTypeLocation :: Text -> N.Name -> IO ()
9994
rememberTypeLocation str d = atomicModifyIORef typeLocations (\m -> (Map.insert str d m, ()))

terminal/src/Develop.hs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ import qualified Stuff
4242

4343
import Lamdera
4444
import qualified Lamdera.CLI.Live as Live
45+
import qualified Lamdera.Constrain
4546
import qualified Lamdera.ReverseProxy
4647
import qualified Lamdera.TypeHash
4748
import qualified Lamdera.PostCompile
@@ -92,6 +93,12 @@ runWithRoot root (Flags maybePort) =
9293
Sentry.asyncUpdateJsOutput sentryCache $ do
9394
debug_ $ "🛫 recompile triggered by: " ++ show events
9495
harness <- Live.prepareLocalDev root
96+
let
97+
typesRootChanged = events & filter (\event ->
98+
stringContains "src/Types.elm" event
99+
|| stringContains "src/Bridge.elm" event
100+
) & length & (\v -> v > 0)
101+
onlyWhen typesRootChanged Lamdera.Constrain.resetTypeLocations
95102
compileToBuilder harness
96103
-- Simultaneously tell all clients to refresh. All those HTTP
97104
-- requests will open but block on the cache mVar, and then release

0 commit comments

Comments
 (0)