Skip to content

fix: add media listeners immediately when using bind:paused #13502

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

Merged
merged 2 commits into from
Oct 7, 2024

Conversation

Rich-Harris
Copy link
Member

I'm a little nervous about this because the code I deleted looks like it's doing something useful, and it's basically impossible to test for, but:

  • I don't really understand what, and
  • Right now, media elements are broken — toggling paused doesn't cause a media element with bind:paused to start playing

This fixes it, as far as I can tell. I've tested mount and hydrate with media elements that are rendered immediately, media elements that are rendered after a state change, and media elements with an artificial network delay that I would expect to trigger whatever canplay timing edge cases we'd encounter.

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

Copy link

changeset-bot bot commented Oct 4, 2024

🦋 Changeset detected

Latest commit: a0623d4

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

@dummdidumm
Copy link
Member

I vaguely remember having implemented this - have you tested a media element nested inside a component?

@Rich-Harris
Copy link
Member Author

Yep, I took the entire contents of that tutorial exercise, put it in the sandbox, and tested it like so:

<script>
  import App from './App.svelte';

  let show = $state(false);
</script>

<button onclick={() => show = !show}>toggle</button>

{#if show}
  <App />
{/if}

Copy link
Member

@dummdidumm dummdidumm left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This fails with the described error for me if I set let paused = $state(false), i.e. request the media to play immediately (make sure to have interacted with the document first).

Investigating other solutions

@dummdidumm
Copy link
Member

Fixed by making it an effect instead of a render effect (the commit messages is wrong, oops), that way the media element is ensured to be part of the dom.

@dummdidumm dummdidumm merged commit c6af26f into main Oct 7, 2024
8 of 9 checks passed
@dummdidumm dummdidumm deleted the bind-paused branch October 7, 2024 12:42
listen(media, ['play', 'pause', 'canplay'], callback, false);
}
// If someone switches the src while media is playing, the player will pause.
// Listen to the canplay event to get notified of this situation.
Copy link
Contributor

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