-
-
Notifications
You must be signed in to change notification settings - Fork 4.6k
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
Conversation
🦋 Changeset detectedLatest commit: a0623d4 The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
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 |
I vaguely remember having implemented this - have you tested a media element nested inside a component? |
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} |
There was a problem hiding this 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
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. |
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. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should this be the expected behavior? This can be really annoying to deal with.
For context, that is the situation: https://svelte-5-preview.vercel.app/#H4sIAAAAAAAACqVSwW7bMAz9FUKXJoARNzns4MUBti_oYbd5KByZtoXKpCFSHQrD_z4oStd1wbBDT4QeH98jHrWY3nkUU31fDLUTmsp8mWdTGH2Z00Oe0SuawgjHYBNyFBvcrKeGGvWooG5CqOH-8ysgwUINFL3_Dc1tFOyghr71ggmmRi2TKMy-fYEaNiTBbqE-wZJ6jb6TbfQqKsFegVtJXf_WbWPneJ_U_1ROnc3dqDpLVZatVccku4F58LizPJXCkTopn_dl69swSS6P1rN92vEw3G3_7Xb4uNs5Dh4fNRK-MzuWb8HT8WIGZ0ddZWMISPrNTVgvKbY14zmhesl1hUWCXU_H8jKaVc5RlQmYrHf2qV7eQlsv931Ix8lW-7RB5v9n9nAze7iZDaeGLgQdEXoXRDO1gJY6OGPPAcEpIHVS5D-SmKwjBmBC2MjMzmOoEuun8x6I8xm2r_pfWcfrAmMr8AkELVMnDZnCTNy53mFnKg0R1x_rL2WeCyQJAwAA
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
And with the canplay handler, it needs a setTimeout to work via the binded pause
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:
paused
doesn't cause a media element withbind:paused
to start playingThis fixes it, as far as I can tell. I've tested
mount
andhydrate
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 whatevercanplay
timing edge cases we'd encounter.Before submitting the PR, please make sure you do the following
feat:
,fix:
,chore:
, ordocs:
.Tests and linting
pnpm test
and lint the project withpnpm lint