Skip to content
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

update of DOM listeners in FilePlayer when audio/video tags was switched #234

Merged
merged 1 commit into from Sep 13, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 37 additions & 22 deletions src/players/FilePlayer.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,31 +34,20 @@ export default class FilePlayer extends Base {
)
}
componentDidMount () {
const { playsinline, onPause, onEnded, onError } = this.props
this.player.addEventListener('canplay', this.onReady)
this.player.addEventListener('play', this.onPlay)
this.player.addEventListener('pause', () => {
if (this.mounted) {
onPause()
}
})
this.player.addEventListener('seeked', this.onSeek)
this.player.addEventListener('ended', onEnded)
this.player.addEventListener('error', onError)
if (playsinline) {
this.player.setAttribute('playsinline', '')
this.player.setAttribute('webkit-playsinline', '')
}
this.addDOMListeners()
super.componentDidMount()
}
componentDidUpdate (prevProps) {
const { url, config } = this.props
const wasAudio = AUDIO_EXTENSIONS.test(prevProps.url) || prevProps.config.file.forceAudio
const isAudio = AUDIO_EXTENSIONS.test(url) || config.file.forceAudio
if (wasAudio !== isAudio) {
this.removeDOMListeners()
Copy link
Owner

Choose a reason for hiding this comment

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

I'm not sure this is necessary. At this point, this.player is already the new <audio> or <video> element and the old one has been unmounted and removed from the DOM. We might have to do the same check in componentWillReceiveProps and run removeDOMListeners whilst the old element is still mounted? Or we might not have to run it at all, as the element is being killed anyway.

Copy link
Author

@ghost ghost Sep 12, 2017

Choose a reason for hiding this comment

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

Yes, I was trying to set this check in cwrp, but by unknown (for me) reasons it don't works: player crashes. And by some unknown reasons, when we skip removing listeners and replace one tag by another, method getEventListeners() in console of my Chrome shows that old listeners exists on fresh tag too. Possibly react make this magic, possibly it such specifical Chrome (devTools?) bug. To be honest, I did not undestand the problem deeply, but in PR I provided the only working way I found.

Copy link
Owner

Choose a reason for hiding this comment

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

I'm happy to merge the PR and will have a look at fixing it up a bit. Thanks!

Copy link
Author

Choose a reason for hiding this comment

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

super.componentWillReceiveProps(nextProps)! Exactly!

this.addDOMListeners()
}
}
componentWillUnmount () {
const { onPause, onEnded, onError } = this.props
this.player.removeEventListener('canplay', this.onReady)
this.player.removeEventListener('play', this.onPlay)
this.player.removeEventListener('pause', onPause)
this.player.removeEventListener('seeked', this.onSeek)
this.player.removeEventListener('ended', onEnded)
this.player.removeEventListener('error', onError)
this.removeDOMListeners()
super.componentWillUnmount()
}
onSeek = e => {
Expand Down Expand Up @@ -133,6 +122,32 @@ export default class FilePlayer extends Base {
ref = player => {
this.player = player
}
addDOMListeners () {
const { playsinline, onPause, onEnded, onError } = this.props
this.player.addEventListener('canplay', this.onReady)
this.player.addEventListener('play', this.onPlay)
this.player.addEventListener('pause', () => {
if (this.mounted) {
onPause()
}
})
this.player.addEventListener('seeked', this.onSeek)
this.player.addEventListener('ended', onEnded)
this.player.addEventListener('error', onError)
if (playsinline) {
this.player.setAttribute('playsinline', '')
this.player.setAttribute('webkit-playsinline', '')
}
}
removeDOMListeners () {
const { onPause, onEnded, onError } = this.props
this.player.removeEventListener('canplay', this.onReady)
this.player.removeEventListener('play', this.onPlay)
this.player.removeEventListener('pause', onPause)
this.player.removeEventListener('seeked', this.onSeek)
this.player.removeEventListener('ended', onEnded)
this.player.removeEventListener('error', onError)
}
render () {
const { url, loop, controls, config, width, height } = this.props
const useAudio = AUDIO_EXTENSIONS.test(url) || config.file.forceAudio
Expand Down