Skip to content

Commit

Permalink
Non-Lamdera compilations should also have the lamdera/container exten…
Browse files Browse the repository at this point in the history
…sions
  • Loading branch information
supermario committed Oct 23, 2024
1 parent 1671eab commit 78bcc90
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 10 deletions.
21 changes: 12 additions & 9 deletions extra/Lamdera/Injection.hs
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,13 @@ type Mains = Map.Map ModuleName.Canonical Opt.Main
graphModifications :: Mode.Mode -> Mains -> Map.Map Opt.Global Opt.Node -> Map.Map Opt.Global Opt.Node
graphModifications mode mains graph = do
if mains & mainsInclude ["LocalDev"]
then graph & Map.mapWithKey (modify $ isProdMode mode)
then graph & Map.mapWithKey (modify $ isOptimizedMode mode)
-- & inspect
else graph


modify :: Bool -> Opt.Global -> Opt.Node -> Opt.Node
modify isProd v n =
modify isOptimized v n =
case (v, n) of
(Opt.Global (ModuleName.Canonical (Pkg.Name "elm" "kernel") "Http") name, Opt.Kernel chunks deps) ->
let newChunks =
Expand All @@ -58,7 +58,7 @@ modify isProd v n =
bs
& Text.decodeUtf8
& Text.replace "var _Http_toTask =" "var _Http_toTask_REPLACED ="
& (<>) (modifiedHttp_toTask isProd)
& (<>) (modifiedHttp_toTask isOptimized)
& Text.encodeUtf8
& Elm.Kernel.JS
_ ->
Expand All @@ -83,8 +83,8 @@ behaviors and limitations.
See extra/Lamdera/ReverseProxy.hs for the proxy itself
-}
modifiedHttp_toTask isProd =
onlyIf (not isProd)
modifiedHttp_toTask isOptimized =
onlyIf (not isOptimized)
-- Identical to original except for alterIfProxyRequired addition
[text|

Expand Down Expand Up @@ -191,7 +191,7 @@ injections outputType mode =
*like with Dict and Set, the rest of the equals kernel code behavior is unchanged.
-}
equalsOverride =
if isProdMode mode then
if isOptimizedMode mode then
[text|
if (x.$$ < 0)
{
Expand Down Expand Up @@ -233,7 +233,7 @@ injections outputType mode =

lamderaContainersExtensions_ =
Ext.Common.bsToText lamderaContainersExtensions
& Text.replace "$equalsOverride" equalsOverride
& Text.replace "// equals override injection marker" equalsOverride
in
case outputType of
-- NotLamdera was added when we fixed the hot loading of a new app version in the browser.
Expand All @@ -244,6 +244,9 @@ injections outputType mode =
-- and does not help the garbage collector enough for a killed app to be fully garbage collected.
NotLamdera ->
[text|

$lamderaContainersExtensions_

function _Platform_initialize(flagDecoder, args, init, update, subscriptions, stepperBuilder)
{
var result = A2(_Json_run, flagDecoder, _Json_wrap(args ? args['flags'] : undefined));
Expand Down Expand Up @@ -908,8 +911,8 @@ onlyIf cond t =
else ""


isProdMode :: Mode.Mode -> Bool
isProdMode mode =
isOptimizedMode :: Mode.Mode -> Bool
isOptimizedMode mode =
case mode of
Mode.Dev _ -> False
Mode.Prod _ -> True
Expand Down
2 changes: 1 addition & 1 deletion extra/Lamdera/Injection/lamdera-containers-extensions.js
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ function _Utils_eqHelp(x, y, depth, stack)
return true;
}

$equalsOverride
// equals override injection marker

for (var key in x)
{
Expand Down
28 changes: 28 additions & 0 deletions test/scenario-alltypes/src/Frontend.elm
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
module Frontend exposing (..)

import Array
import Browser exposing (UrlRequest(..))
import Browser.Navigation as Nav
import Dict
import Env
import Html
import Html.Attributes as Attr
import Lamdera
import SeqDict exposing (SeqDict)
import SeqSet exposing (SeqSet)
import Set
import Test.Wire_Alias_2_Record
import Time
import Types exposing (..)
import Url

Expand All @@ -31,6 +38,27 @@ init url key =
let
_ =
Env.configBoth

allTypes : Test.Wire_Alias_2_Record.AllTypes
allTypes =
{ int = 42
, float = 3.14
, bool = True
, char = 'a'
, string = "Hello, Elm!"
, listInt = [ 1, 2, 3, 4, 5 ]
, setFloat = Set.fromList [ 1.1, 2.2, 3.3 ]
, arrayString = Array.fromList [ "one", "two", "three" ]
, dict = Dict.fromList [ ( "key1", [ 1, 2 ] ), ( "key2", [ 3, 4 ] ) ]
, time = Time.millisToPosix 0
, order = Basics.LT
, unit = ()
, seqDict = SeqDict.fromList [ ( "a", 1 ), ( "b", 2 ) ]
, seqSet = SeqSet.fromList [ 1, 2, 3 ]
}

printout =
Debug.log "Test.Wire_Alias_2_Record.AllTypes" allTypes
in
( { key = key
, message = "Welcome to Lamdera! You're looking at the auto-generated base implementation. Check out src/Frontend.elm to start coding! " ++ Env.configFEOnly
Expand Down
4 changes: 4 additions & 0 deletions test/scenario-alltypes/src/Test/Wire_Alias_2_Record.elm
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ module Test.Wire_Alias_2_Record exposing (..)
import Array exposing (Array)
import Dict exposing (Dict)
import Lamdera.Wire3
import SeqDict exposing (SeqDict)
import SeqSet exposing (SeqSet)
import Set exposing (Set)
import Test.External exposing (..)
import Time
Expand All @@ -26,6 +28,8 @@ type alias AllTypes =

-- , externalCustomBasic : ExternalCustomBasic
-- , externalCustom : ExternalCustom Int
, seqDict : SeqDict String Int
, seqSet : SeqSet Int
}


Expand Down

0 comments on commit 78bcc90

Please sign in to comment.