Skip to content
This repository has been archived by the owner on Jan 20, 2018. It is now read-only.

Initial command obliterates previous state #18

Closed
mattrrichard opened this issue Nov 7, 2016 · 4 comments
Closed

Initial command obliterates previous state #18

mattrrichard opened this issue Nov 7, 2016 · 4 comments

Comments

@mattrrichard
Copy link

mattrrichard commented Nov 7, 2016

On start up, my app randomizes its state as part of its init command:

main =
    let 
        startupCmd = 
            Random.generate NewModel modelGenerator
    in
        Html.program
            { init = ( emptyModel, startupCmd )
            , update = update 
            , view = view
            , subscriptions = subscriptions
            }

The problem here is that its NewModel message ticks after the app's previous state is injected.

Is this something that can be worked around by elm-hot-reload? If not, what about a way to signal to the app that it has been hot reloaded? That way I could simply not issue the startup command in those cases. e.g. :

main = 
    let
        startupCmd = 
            Random.generate NewModel modelGenerator

        init flags =
            if flags.hotSwapped then
                ( emptyModel, Cmd.none )
            else
                ( emptyModel, startupCmd )
    in
        Html.programWithFlags
            { init = init
            , update = update startupCmd discoCmd
            , view = view
            , subscriptions = subscriptions
            }
@mattrrichard mattrrichard changed the title Startup Random command obliterates previous state Inittial command obliterates previous state Nov 7, 2016
@mattrrichard mattrrichard changed the title Inittial command obliterates previous state Initial command obliterates previous state Nov 7, 2016
@fluxxu
Copy link
Owner

fluxxu commented Nov 7, 2016

Hi,

I added this and published it to npm:
https://github.com/fluxxu/elm-hot-loader/blob/master/hmr.js#L164

Do you think this can solve your problem?

@mattrrichard
Copy link
Author

I think something close to that would work, but I don't believe that __hotSwapped is a valid record field name.

@fluxxu
Copy link
Owner

fluxxu commented Nov 10, 2016

I think I came out a better way to solve your problem.
Now you can register callback to patch flags/state:
https://github.com/fluxxu/elm-hot-loader-starter/blob/master/src/index.js#L19
So you can

elm.hot.subscribe(function (event, context) {
  if (event === 'swap') {
    context.flags.hotSwapped = true
  }
})

Then your Main.init will get flags with hotSwapped === true
Please upgrade elm-hot-loader to 0.4.x to use this.

@mattrrichard
Copy link
Author

Finally got to test this, and yep it works well for me. Thanks!

AndrewKvalheim added a commit to AndrewKvalheim/collinear that referenced this issue Mar 31, 2017
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants