-
-
Notifications
You must be signed in to change notification settings - Fork 4.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: make media bindings more robust (#12206)
The media bindings where fragile because the "prevent rerun if update in progress"-logic was flawed: It didn't (re)set the `updating` flag correctly, because it assumed an event and a render effect would always directly follow each other, with one always being first - but that's not true. It's much more robust to instead compare the value with what's currently present in the DOM. For the very fast-firing current-time-binding a variable is used to not invoke the DOM getter as much, for the others this is not necessary. Lastly, the playback-rate-binding contained another bug where the listener was setup inside the effect and therefore reinitialized on each binding change - it needs to move into a different effect.
- Loading branch information
1 parent
266f669
commit d959d4a
Showing
9 changed files
with
135 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'svelte': patch | ||
--- | ||
|
||
fix: make media bindings more robust |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
29 changes: 29 additions & 0 deletions
29
packages/svelte/tests/runtime-browser/samples/bind-muted/_config.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import { test, ok } from '../../assert'; | ||
|
||
export default test({ | ||
mode: ['client'], | ||
async test({ assert, target }) { | ||
const audio = target.querySelector('audio'); | ||
const button = target.querySelector('button'); | ||
ok(audio); | ||
|
||
assert.equal(audio.muted, false); | ||
|
||
audio.muted = true; | ||
audio.dispatchEvent(new CustomEvent('volumechange')); | ||
await new Promise((r) => setTimeout(r, 100)); | ||
assert.equal(audio.muted, true, 'event'); | ||
|
||
button?.click(); | ||
await new Promise((r) => setTimeout(r, 100)); | ||
assert.equal(audio.muted, false, 'click 1'); | ||
|
||
button?.click(); | ||
await new Promise((r) => setTimeout(r, 100)); | ||
assert.equal(audio.muted, true, 'click 2'); | ||
|
||
button?.click(); | ||
await new Promise((r) => setTimeout(r, 100)); | ||
assert.equal(audio.muted, false, 'click 3'); | ||
} | ||
}); |
6 changes: 6 additions & 0 deletions
6
packages/svelte/tests/runtime-browser/samples/bind-muted/main.svelte
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
<script> | ||
let muted = $state(false); | ||
</script> | ||
|
||
<audio bind:muted></audio> | ||
<button onclick={() => (muted = !muted)}>toggle</button> |
29 changes: 29 additions & 0 deletions
29
packages/svelte/tests/runtime-browser/samples/bind-playbackrate/_config.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import { test, ok } from '../../assert'; | ||
|
||
export default test({ | ||
mode: ['client'], | ||
async test({ assert, target }) { | ||
const audio = target.querySelector('audio'); | ||
const button = target.querySelector('button'); | ||
ok(audio); | ||
|
||
assert.equal(audio.playbackRate, 0.5); | ||
|
||
audio.playbackRate = 1.0; | ||
audio.dispatchEvent(new CustomEvent('ratechange')); | ||
await new Promise((r) => setTimeout(r, 100)); | ||
assert.equal(audio.playbackRate, 1.0); | ||
|
||
button?.click(); | ||
await new Promise((r) => setTimeout(r, 100)); | ||
assert.equal(audio.playbackRate, 2); | ||
|
||
button?.click(); | ||
await new Promise((r) => setTimeout(r, 100)); | ||
assert.equal(audio.playbackRate, 3); | ||
|
||
button?.click(); | ||
await new Promise((r) => setTimeout(r, 100)); | ||
assert.equal(audio.playbackRate, 4); | ||
} | ||
}); |
6 changes: 6 additions & 0 deletions
6
packages/svelte/tests/runtime-browser/samples/bind-playbackrate/main.svelte
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
<script> | ||
let playbackRate = $state(0.5); | ||
</script> | ||
|
||
<audio bind:playbackRate></audio> | ||
<button onclick={() => (playbackRate += 1)}>increment</button> |
29 changes: 29 additions & 0 deletions
29
packages/svelte/tests/runtime-browser/samples/bind-volume/_config.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import { test, ok } from '../../assert'; | ||
|
||
export default test({ | ||
mode: ['client'], | ||
async test({ assert, target }) { | ||
const audio = target.querySelector('audio'); | ||
const button = target.querySelector('button'); | ||
ok(audio); | ||
|
||
assert.equal(audio.volume, 0.1); | ||
|
||
audio.volume = 0.2; | ||
audio.dispatchEvent(new CustomEvent('volumechange')); | ||
await new Promise((r) => setTimeout(r, 100)); | ||
assert.equal(audio.volume, 0.2); | ||
|
||
button?.click(); | ||
await new Promise((r) => setTimeout(r, 100)); | ||
assert.equal(audio.volume, 0.2 + 0.1); // JavaScript can't add floating point numbers correctly | ||
|
||
button?.click(); | ||
await new Promise((r) => setTimeout(r, 100)); | ||
assert.equal(audio.volume, 0.2 + 0.1 + 0.1); | ||
|
||
button?.click(); | ||
await new Promise((r) => setTimeout(r, 100)); | ||
assert.equal(audio.volume, 0.2 + 0.1 + 0.1 + 0.1); | ||
} | ||
}); |
6 changes: 6 additions & 0 deletions
6
packages/svelte/tests/runtime-browser/samples/bind-volume/main.svelte
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
<script> | ||
let volume = $state(0.1); | ||
</script> | ||
|
||
<audio bind:volume></audio> | ||
<button onclick={() => (volume += 0.1)}>increment</button> |