Skip to content

Navigation API: allow registering regular handler from within a precommit handler #11956

@noamr

Description

@noamr

What problem are you trying to solve?

Often, the flow of navigation evolves as the navigation goes along.
This is not well represented in the navigation API, where precommit handlers and post-commit handlers are separate and defined in advance:

navigateEvent.intercept({
  precommitHandler,
  handler
})

The above API works well when those flows are separate, or if only one is needed. However, if the precommit and post-commit flows are intertwined, it's a bit awkward to use.

What solutions exist today?

It's possible to create a more integrated experience by using Promise.withResolvers():

navigation.addEventListener("navigate", async e => {
  const commit = Promise.withResolvers();
  const done = Promise.withResolvers();
  event.intercept({
    precommitHandler: () => commit.promise,
    handler: () => done.promise
  });

  await do_precommit_stuff();
  commit.resolve();
  await do_post_commit_stuff();
  done.resolve();
}

How would you solve it?

Allow registering a new handler from within the precommit handler:

  event.intercept({
    precommitHandler: (controller) => {
       do_precommit_stuff();
       controller.addHandler(async() => {
           await postcommit_stuff();
       });
       await do_more_precommit_stuff();
    }
  });

This makes registering handlers during precommit more dynamic, and doesn't require wrapping everything.

Anything else?

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    addition/proposalNew features or enhancementsneeds implementer interestMoving the issue forward requires implementers to express interest

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions