@@ -138,56 +138,28 @@ export function bind_playback_rate(media, get, set = get) {
138
138
* @param {(paused: boolean) => void } set
139
139
*/
140
140
export function bind_paused ( media , get , set = get ) {
141
- var mounted = hydrating ;
142
141
var paused = get ( ) ;
143
142
144
- var callback = ( ) => {
143
+ var update = ( ) => {
145
144
if ( paused !== media . paused ) {
146
- paused = media . paused ;
147
145
set ( ( paused = media . paused ) ) ;
148
146
}
149
147
} ;
150
148
151
- if ( paused == null ) {
152
- callback ( ) ;
153
- }
154
-
155
- // Defer listening if not mounted yet so that the first canplay event doesn't cause a potentially wrong update
156
- if ( mounted ) {
157
- // If someone switches the src while media is playing, the player will pause.
158
- // Listen to the canplay event to get notified of this situation.
159
- listen ( media , [ 'play' , 'pause' , 'canplay' ] , callback , false ) ;
160
- }
149
+ // If someone switches the src while media is playing, the player will pause.
150
+ // Listen to the canplay event to get notified of this situation.
151
+ listen ( media , [ 'play' , 'pause' , 'canplay' ] , update , paused == null ) ;
161
152
162
- render_effect ( ( ) => {
163
- paused = ! ! get ( ) ;
164
-
165
- if ( paused !== media . paused ) {
166
- var toggle = ( ) => {
167
- mounted = true ;
168
- if ( paused ) {
169
- media . pause ( ) ;
170
- } else {
171
- media . play ( ) . catch ( ( ) => {
172
- set ( ( paused = true ) ) ;
173
- } ) ;
174
- }
175
- } ;
176
-
177
- if ( mounted ) {
178
- toggle ( ) ;
153
+ // Needs to be an effect to ensure media element is mounted: else, if paused is `false` (i.e. should play right away)
154
+ // a "The play() request was interrupted by a new load request" error would be thrown because the resource isn't loaded yet.
155
+ effect ( ( ) => {
156
+ if ( ( paused = ! ! get ( ) ) !== media . paused ) {
157
+ if ( paused ) {
158
+ media . pause ( ) ;
179
159
} else {
180
- // If this is the first invocation in dom mode, the media element isn't mounted yet,
181
- // and therefore its resource isn't loaded yet. We need to wait for the canplay event
182
- // in this case or else we'll get a "The play() request was interrupted by a new load request" error.
183
- media . addEventListener (
184
- 'canplay' ,
185
- ( ) => {
186
- listen ( media , [ 'play' , 'pause' , 'canplay' ] , callback , false ) ;
187
- toggle ( ) ;
188
- } ,
189
- { once : true }
190
- ) ;
160
+ media . play ( ) . catch ( ( ) => {
161
+ set ( ( paused = true ) ) ;
162
+ } ) ;
191
163
}
192
164
}
193
165
} ) ;
0 commit comments