Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Init called twice when in debug mode #52

Open
padzikm opened this issue Jan 28, 2021 · 1 comment
Open

Init called twice when in debug mode #52

padzikm opened this issue Jan 28, 2021 · 1 comment

Comments

@padzikm
Copy link

padzikm commented Jan 28, 2021

In debug mode init is called twice - from src/Debug/Navigation.ts:

function createProgram<Model, Msg, Dom>(
  stopDebuggerOn: Observable<unknown>,
  locationToMessage: (location: Location) => Msg,
  init: (location: Location) => [Model, Cmd<Msg>],
  update: (msg: Msg, model: Model) => [Model, Cmd<Msg>],
  view: (model: Model) => Html<Dom, Msg>,
  subscriptions?: (model: Model) => Sub<Msg>
): Program<Model, Msg, Dom> {
  const history = H.createHashHistory() // this is needed only to generate init model for debug$ :S

  const Debugger = runDebugger<Model, Msg>(window, stopDebuggerOn)

  const initModel = init(history.location)[0] // init called first time

  const debug$ = new BehaviorSubject<DebugData<Model, Msg>>([debugInit(), initModel])

  const p = program(locationToMessage, init, updateWithDebug(debug$, update), view, subscriptions) //init will be called second time

  // --- Run the debugger
  // --- we need to make a type assertion for `dispatch` because we cannot change the intrinsic `msg` type of `program`;
  // --- otherwise `programWithDebugger` won't be usable as a transparent extension/substitution of `Html`'s programs
  Debugger({
    debug$,
    init: initModel,
    dispatch: p.dispatch as DebuggerR<Model, Msg>['dispatch']
  })()

  return p
}

Dirty workaround is to keep init counter inside file, but I think calling init twice is a bug

@StefanoMagrassi
Copy link
Collaborator

@padzikm we need to call init the first time in order to get the initial program's Model to:

  • create the debug$ stream
  • pass the initial state to the redux-dev-tool extension (in case it is installed - but the Debugger interface is intentionally agnostic/more general so we can add other types of debugger in the future)

Which is your problem (related to a double call to init)? In theory init should not execute any side-effect, it only defines the initial Model and Cmd so it should be safe executing it twice

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants