Skip to content

fix: robust handling of events in spread attributes#11942

Merged
Rich-Harris merged 1 commit intomainfrom
event-spread-fix
Jun 6, 2024
Merged

fix: robust handling of events in spread attributes#11942
Rich-Harris merged 1 commit intomainfrom
event-spread-fix

Conversation

@dummdidumm
Copy link
Member

When an event listener is removed right before it would be fired, adding a new listener comes too late for the browser and the event is swallowed. The fix is to tweak the spread attributes function to keep using the same object for updates so that we can wrap the event listener to invoke it like attributes[key](evt) instead.
No test because it seems impossible to reproduce in testing environments. This is the reproducible that fails without this fix:

<script lang="ts">
	let x = $state({
		onfocus: (e) => {
			console.log('focused');
		},
		onblur: (e) => {
			console.log('blurred');
		}
	});

	let i = 0;
	function handleFocus(node: HTMLButtonElement) {
		function changeFocused() {
			i++;
			x = {
				onfocus: (e) => {
					console.log('focused', i);
				},
				onblur: (e) => {
					console.log('blurred', i);
				}
			};
		}
		node.addEventListener('focus', changeFocused);
		node.addEventListener('blur', changeFocused);

		return {
			destroy() {
				node.removeEventListener('focus', changeFocused);
				node.removeEventListener('blur', changeFocused);
			}
		};
	}
</script>

<button>button 1</button>
<button use:handleFocus {...x}>button 2</button>

Fixes #11903

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 and linting

  • Run the tests with pnpm test and lint the project with pnpm lint

@changeset-bot
Copy link

changeset-bot bot commented Jun 6, 2024

🦋 Changeset detected

Latest commit: 66d9078

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
svelte Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

When an event listener is removed right before it would be fired, adding a new listener comes too late for the browser and the event is swallowed.
The fix is to tweak the spread attributes function to keep using the same object for updates so that we can wrap the event listener to invoke it like `attributes[key](evt)` instead.
No test because it seems impossible to reproduce in testing environments.
Fixes #11903
@Rich-Harris Rich-Harris merged commit f57836c into main Jun 6, 2024
@Rich-Harris Rich-Harris deleted the event-spread-fix branch June 6, 2024 19:39
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.

Svelte 5: Again some events are ignored under these certain circumstances.

2 participants