elmish / hmr

Hot Module Replacement for Elmish apps
https://elmish.github.io/hmr
Other
28 stars 9 forks source link

When using subscription every version of code is being invoked #4

Closed Novakov closed 6 years ago

Novakov commented 6 years ago

Description

When dispatching message from subscription (e.g. timer) and using HMR application is "looping" through all versions of code.

Repro code

Repository: https://github.com/Novakov/fable-elmish-hmr-bug It's Fable.Template.Elmish.React stripped down from almost everything to minimal Elmish + React + HMR app.

Expected and actual results

Expected: After changing code, newest version should be used in browser.

Actual: After changing code, every version of it is used

Related information

MangelMaxime commented 6 years ago

I am not sure, I can do something about that.

The problem, is the setupTimer is re-created each time the HMR is trigger. You could avoid this problem by deleting old timer reference.

Something like:

let setupTimer dispatch =
  let cb () =
    dispatch Do |> ignore

  #if DEBUG
  clearInterval(window?timerID)
  window?timerID <- window.setInterval(cb, 1000)
  #else
  window.setInterval(cb, 1000) |> ignore
  #endif
Novakov commented 6 years ago

Oh... haven't though of that ;)

My original problem is not with timers but very very similar (external source of events) and solution should be similar.

Thanks!