Uncaught TypeError when using --optimize

Hi All,

I’m in the finishing state of our webapp development and added the --optimize flag to elm make.
This resulted in following error:

Uncaught TypeError: Jr is not a function

I did not have this error before using the optimize flag.

By evaluating the code in Chrome dev tools I found out it is related to Browser.Dom.getElement.

Fyi: we have several rotary like sliders on some of the pages and we need the location of the slider to translate the touch coordinates in slider values.

My code:

getDomLocation : String -> Cmd Msg
getDomLocation taskId =
  ( Task.attempt
      (NewDomLocation taskId)
      (Browser.Dom.getElement taskId)
  )

getDomLocationVolumeSliders : Model -> ZoneId -> Cmd Msg
getDomLocationVolumeSliders model zoneId =
  let
    zone : Zone
    zone =
      case zoneId of
        Zone1 -> model.zone1
        Zone2 -> model.zone2
  in
    Cmd.batch
      [ getDomLocation zone.micAVolume.rotaryId
      , getDomLocation zone.micBVolume.rotaryId
      , getDomLocation zone.sourceVolume.rotaryId
      ]

init : Flags -> Url.Url -> Nav.Key -> ( Model, Cmd Msg )
init flags url key =
  ( { initialModel | layout = updateLayout flags
                   , webSocket = WebSocket.updateUrl url initialModel.webSocket
    }
  , Cmd.batch
      [ (getDomLocationVolumeSliders initialModel Zone1)
      , ( setBodyBackgroundColor
            (updateLayout flags).device
            initialModel.theme
            initialModel.activePanel
        )
      ]
  )

If I replace the Cmd.batch in getDomLocationVolumeSliders with Cmd.none the error is gone. The function getDomLocationVolumeSliders is called in several places, but as the error appeared on load, I first checked the init function. I replaced Cmd.none again with the Cmd.batch and commented the getDomLocationVolumeSliders call in the init function. The error was gone.

If I called getDomLocationVolumeSliders from other places it just worked.

I solved the error by not giving initalModel to getDomLocationVolumeSliders but the updated initialModel.

My working code: (with --optimize flag)

init : Flags -> Url.Url -> Nav.Key -> ( Model, Cmd Msg )
init flags url key =
  let
    initialModel : Model
    initialModel =
      { emptyModel | layout = updateLayout flags
                   , webSocket = WebSocket.updateUrl url emptyModel.webSocket
      }
  in
    ( initialModel
    , Cmd.batch
        [ (getDomLocationVolumeSliders initialModel Zone1)
        , ( setBodyBackgroundColor
              (updateLayout flags).device
              initialModel.theme
              initialModel.activePanel
          )
        ]
    )

I did not change function getDomLocation or function getDomLocationVolumeSliders.
So by giving getDomLocationVolumeSliders the updated intial model the error is solved. While it only takes 3 strings from the model and those strings do not change.

Strange no? Isn’t this a runtime error? I did not have any Elm compile errors.

I solved it for my implementation, so I’m happy :slight_smile:. I just wanted to discuss what I possibly did wrong or maybe it’s something wrong with the optimization steps that are applied with --optimize.

Thanks!
Jasper

Maybe this known issue: https://github.com/elm/compiler/issues/1836 ?

It looks like it’s related but then why is the issue solved when I restructure the init function?

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.