Skip to content

Commit

Permalink
Move progress logic from players to top level component
Browse files Browse the repository at this point in the history
* Now we're rendering all players at once, we don't want multiple progress loops going at once
* Renamed `update` to `progress` – makes a bit more sense
  • Loading branch information
cookpete committed Dec 24, 2015
1 parent 31b7d3d commit 17fbef8
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 19 deletions.
25 changes: 25 additions & 0 deletions src/ReactPlayer.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,20 @@ import 'array.prototype.find'
import { propTypes, defaultProps } from './props'
import players from './players'

const PROGRESS_FREQUENCY = 500

export default class ReactPlayer extends Component {
static propTypes = propTypes
static defaultProps = defaultProps
static canPlay (url) {
return players.some(player => player.canPlay(url))
}
componentDidMount () {
this.progress()
}
componentWillUnmount () {
clearTimeout(this.progressTimeout)
}
shouldComponentUpdate (nextProps) {
return (
this.props.url !== nextProps.url ||
Expand All @@ -23,6 +31,23 @@ export default class ReactPlayer extends Component {
player.seekTo(fraction)
}
}
progress = () => {
if (this.props.url && this.refs.player) {
let progress = {}
const loaded = this.refs.player.getFractionLoaded()
const played = this.refs.player.getFractionPlayed()
if (!this.prevLoaded || loaded !== this.prevLoaded) {
progress.loaded = this.prevLoaded = loaded
}
if (!this.prevPlayed || played !== this.prevPlayed) {
progress.played = this.prevPlayed = played
}
if (progress.loaded || progress.played) {
this.props.onProgress(progress)
}
}
this.progressTimeout = setTimeout(this.progress, PROGRESS_FREQUENCY)
}
renderPlayer = Player => {
const active = Player.canPlay(this.props.url)
const { youtubeConfig, soundcloudConfig, vimeoConfig, ...activeProps } = this.props
Expand Down
19 changes: 0 additions & 19 deletions src/players/Base.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,16 @@ import { Component } from 'react'

import { propTypes, defaultProps } from '../props'

const UPDATE_FREQUENCY = 500

export default class Base extends Component {
static propTypes = propTypes
static defaultProps = defaultProps
componentDidMount () {
this.update()
if (this.props.url) {
this.load(this.props.url)
}
}
componentWillUnmount () {
this.stop()
clearTimeout(this.updateTimeout)
}
componentWillReceiveProps (nextProps) {
// Invoke player methods based on incoming props
Expand All @@ -36,21 +32,6 @@ export default class Base extends Component {
shouldComponentUpdate (nextProps) {
return this.props.url !== nextProps.url
}
update = () => {
let progress = {}
const loaded = this.getFractionLoaded()
const played = this.getFractionPlayed()
if (!this.prevLoaded || loaded !== this.prevLoaded) {
progress.loaded = this.prevLoaded = loaded
}
if (!this.prevPlayed || played !== this.prevPlayed) {
progress.played = this.prevPlayed = played
}
if (progress.loaded || progress.played) {
this.props.onProgress(progress)
}
this.updateTimeout = setTimeout(this.update, UPDATE_FREQUENCY)
}
onReady = () => {
this.setVolume(this.props.volume)
if (this.props.playing || this.preloading) {
Expand Down

0 comments on commit 17fbef8

Please sign in to comment.