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

[feat] dispatch cancelable custom events #7064

Merged
merged 16 commits into from Apr 12, 2022
Merged

[feat] dispatch cancelable custom events #7064

merged 16 commits into from Apr 12, 2022

Conversation

bluwy
Copy link
Member

@bluwy bluwy commented Dec 28, 2021

Fixes #4623

Dispatching custom events can be cancelled.

const dispatch = createEventDispatcher()

const result = dispatch('foo', 'detail', { cancelable: true })
// return false if canceled via `e.preventDefault()`, otherwise always true

Before submitting the PR, please make sure you do the following

  • It's really useful if your PR references an issue where it is discussed ahead of time. In many cases, features are absent for a reason. For large changes, please create an RFC: https://github.com/sveltejs/rfcs
  • Prefix your PR title with [feat], [fix], [chore], or [docs].
  • This message body should clearly illustrate what problems it solves.
  • Ideally, include a test that fails without this PR but passes with it.

Tests

  • Run the tests with npm test and lint the project with npm run lint

@PuruVJ
Copy link
Collaborator

PuruVJ commented Dec 28, 2021

TQSM for this PR. Can you specify how we can detect whether an event was cancelled? As far as I know, you need the event object to detect event.defaultPrevented. Am I missing something?

@bluwy
Copy link
Member Author

bluwy commented Dec 28, 2021

Good point. dispatch should probably return a boolean, similar to dispatchEvent, would need to handle that too.

@bluwy
Copy link
Member Author

bluwy commented Dec 28, 2021

Updated to return a boolean depending on if it's canceled via preventDefault() or not. I tried to return an event instead, but the code doesn't always emit an event, so it's not always possible.

@bluwy
Copy link
Member Author

bluwy commented Jan 30, 2022

Updated docs, changelog and fixed the types. This should be good to go (unless there's some wording changes).

src/runtime/internal/lifecycle.ts Outdated Show resolved Hide resolved
site/content/docs/03-run-time.md Outdated Show resolved Hide resolved
@bluwy bluwy requested a review from Conduitry February 2, 2022 16:19
@bluwy
Copy link
Member Author

bluwy commented Feb 2, 2022

Conduitry, I've requested your review in case you want to take a look at this. In the last maintainer's meeting, we had agreed that this would be a safe addition to the API.

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

Successfully merging this pull request may close these issues.

Dispatch cancelable events with createEventDispatcher
3 participants