Skip to content

Conversation

dummdidumm
Copy link
Member

  • ensure we only have a single document listener per event+runtime
  • add <svelte:body> listeners to before_init similar to the document/window elements
  • move some code into events.js where it belongs

follow-up to #12376 which isn't released yet, therefore no changeset

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

- ensure we only have a single document listener per event+runtime
- add `<svelte:body>` listeners to `before_init` similar to the document/window elements
- move some code into `events.js` where it belongs
Copy link

changeset-bot bot commented Jul 10, 2024

🦋 Changeset detected

Latest commit: bf45ba2

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

@trueadm
Copy link
Contributor

trueadm commented Jul 10, 2024

I'm unsure about this. Ideally we'd increment a counter for each root, and remove the document listeners when the count goes to zero – otherwise we're retaining a document event listener for no reason.

@dummdidumm
Copy link
Member Author

There's no way that we can intercept unmounting, because it's not required to unmount if everything that has references to the instance is garbage collected.so the trade-off is lingering document listeners vs duplicate document listeners if you mount more than once.

@trueadm
Copy link
Contributor

trueadm commented Jul 10, 2024

@dummdidumm What do you mean by intercepting unmounting? If someone doesn't unmount their root, it will cause a memory leak. Tested it just now with this case locally.

@Rich-Harris
Copy link
Member

Yeah, people need to unmount their components properly, you can't rely on GC. Though whether or not they bother to do that, given n delegated events and m roots, it's better to have n listeners than n * m.

I added the counter, and if people do unmount their components listeners are once again removed, but if they don't then there's now a limit to the damage they can do, so this seems to be the best of both worlds

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.

3 participants