Skip to content

Init called twice when in debug mode #52

@padzikm

Description

@padzikm

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions