Skip to content

Commit

Permalink
feat: enable reuse of MediaElement
Browse files Browse the repository at this point in the history
The filter now resets its internal state whenever
src is set on the attached media element.
  • Loading branch information
martinstark committed Jul 1, 2024
1 parent 9593b56 commit b4f3f9e
Showing 1 changed file with 9 additions and 0 deletions.
9 changes: 9 additions & 0 deletions src/media-event-filter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,14 @@ export const getMediaEventFilter = ({
const isNotReady = (): boolean =>
allowResumeAfterEnded ? state.loading : state.loading || state.ended;

// Reset state of media event filter when a new src is
// attached to the media element.
const onEmptied = (): void => {
state = {
...initialState,
};
}

const onCanPlayThrough = (): void => {
if (!state.loading) {
// Recover from Safari "mute" micro buffer triggered by "waiting"
Expand Down Expand Up @@ -455,6 +463,7 @@ export const getMediaEventFilter = ({
[MediaEvent.play, onPlay],
[MediaEvent.pause, onPause],
[MediaEvent.ratechange, onRatechange],
[MediaEvent.emptied, onEmptied],
];

EventHandlerPairs.forEach(([event, handler]) =>
Expand Down

0 comments on commit b4f3f9e

Please sign in to comment.