Skip to content

Commit

Permalink
Lint fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
cookpete committed Aug 18, 2019
1 parent 7a47436 commit ae0f230
Show file tree
Hide file tree
Showing 26 changed files with 554 additions and 308 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"clean": "rimraf lib demo coverage",
"start": "webpack-dev-server --config webpack/config.babel.js",
"lint": "standard --verbose | snazzy",
"lint:fix": "standard --fix",
"test": "cross-env NODE_ENV=test ava",
"test:coverage": "cross-env NODE_ENV=test nyc ava",
"test:codecov": "nyc report --reporter=json && codecov -f coverage/coverage-final.json",
Expand Down
49 changes: 33 additions & 16 deletions src/Player.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export default class Player extends Component {
this.player.load(this.props.url)
this.progress()
}

componentWillUnmount () {
clearTimeout(this.progressTimeout)
clearTimeout(this.durationCheckTimeout)
Expand All @@ -33,6 +34,7 @@ export default class Player extends Component {
}
this.mounted = false
}

componentWillReceiveProps (nextProps) {
// Invoke player methods based on incoming props
const { url, playing, volume, muted, playbackRate, pip, loop, activePlayer } = this.props
Expand Down Expand Up @@ -79,22 +81,27 @@ export default class Player extends Component {
this.player.setLoop(nextProps.loop)
}
}

getDuration () {
if (!this.isReady) return null
return this.player.getDuration()
}

getCurrentTime () {
if (!this.isReady) return null
return this.player.getCurrentTime()
}

getSecondsLoaded () {
if (!this.isReady) return null
return this.player.getSecondsLoaded()
}

getInternalPlayer = (key) => {
if (!this.player) return null
return this.player[key]
}

progress = () => {
if (this.props.url && this.player && this.isReady) {
const playedSeconds = this.getCurrentTime() || 0
Expand All @@ -119,6 +126,7 @@ export default class Player extends Component {
}
this.progressTimeout = setTimeout(this.progress, this.props.progressFrequency || this.props.progressInterval)
}

seekTo (amount, type) {
// When seeking before player is ready, store value and seek later
if (!this.isReady && amount !== 0) {
Expand All @@ -139,7 +147,8 @@ export default class Player extends Component {
}
this.player.seekTo(amount)
}
onReady = () => {

handleReady = () => {
if (!this.mounted) return
this.isReady = true
this.isLoading = false
Expand All @@ -154,9 +163,10 @@ export default class Player extends Component {
} else if (playing) {
this.player.play()
}
this.onDurationCheck()
this.handleDurationCheck()
}
onPlay = () => {

handlePlay = () => {
this.isPlaying = true
this.isLoading = false
const { onStart, onPlay, playbackRate } = this.props
Expand All @@ -172,15 +182,17 @@ export default class Player extends Component {
this.seekTo(this.seekOnPlay)
this.seekOnPlay = null
}
this.onDurationCheck()
this.handleDurationCheck()
}
onPause = (e) => {

handlePause = (e) => {
this.isPlaying = false
if (!this.isLoading) {
this.props.onPause(e)
}
}
onEnded = () => {

handleEnded = () => {
const { activePlayer, loop, onEnded } = this.props
if (activePlayer.loopOnEnded && loop) {
this.seekTo(0)
Expand All @@ -190,11 +202,13 @@ export default class Player extends Component {
onEnded()
}
}
onError = (...args) => {

handleError = (...args) => {
this.isLoading = false
this.props.onError(...args)
}
onDurationCheck = () => {

handleDurationCheck = () => {
clearTimeout(this.durationCheckTimeout)
const duration = this.getDuration()
if (duration) {
Expand All @@ -203,19 +217,22 @@ export default class Player extends Component {
this.onDurationCalled = true
}
} else {
this.durationCheckTimeout = setTimeout(this.onDurationCheck, 100)
this.durationCheckTimeout = setTimeout(this.handleDurationCheck, 100)
}
}
onLoaded = () => {

handleLoaded = () => {
// Sometimes we know loading has stopped but onReady/onPlay are never called
// so this provides a way for players to avoid getting stuck
this.isLoading = false
}

ref = player => {
if (player) {
this.player = player
}
}

render () {
const Player = this.props.activePlayer
if (!Player) {
Expand All @@ -225,12 +242,12 @@ export default class Player extends Component {
<Player
{...this.props}
ref={this.ref}
onReady={this.onReady}
onPlay={this.onPlay}
onPause={this.onPause}
onEnded={this.onEnded}
onLoaded={this.onLoaded}
onError={this.onError}
onReady={this.handleReady}
onPlay={this.handlePlay}
onPause={this.handlePause}
onEnded={this.handleEnded}
onLoaded={this.handleLoaded}
onError={this.handleError}
/>
)
}
Expand Down
5 changes: 5 additions & 0 deletions src/Preview.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,23 @@ export default class Preview extends Component {
state = {
image: null
}

componentDidMount () {
this.mounted = true
this.fetchImage(this.props)
}

componentWillReceiveProps (nextProps) {
const { url, light } = this.props
if (url !== nextProps.url || light !== nextProps.light) {
this.fetchImage(nextProps)
}
}

componentWillUnmount () {
this.mounted = false
}

fetchImage ({ url, light }) {
if (typeof light === 'string') {
this.setState({ image: light })
Expand All @@ -35,6 +39,7 @@ export default class Preview extends Component {
}
})
}

render () {
const { onClick } = this.props
const { image } = this.state
Expand Down
37 changes: 29 additions & 8 deletions src/ReactPlayer.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,41 +16,48 @@ export default class ReactPlayer extends Component {
static addCustomPlayer = player => {
customPlayers.push(player)
}

static removeCustomPlayers = () => {
customPlayers = []
}

static displayName = 'ReactPlayer'
static propTypes = propTypes
static defaultProps = defaultProps
static canPlay = url => {
for (let Player of [ ...customPlayers, ...players ]) {
for (const Player of [...customPlayers, ...players]) {
if (Player.canPlay(url)) {
return true
}
}
return false
}

static canEnablePIP = url => {
for (let Player of [ ...customPlayers, ...players ]) {
for (const Player of [...customPlayers, ...players]) {
if (Player.canEnablePIP && Player.canEnablePIP(url)) {
return true
}
}
return false
}

config = getConfig(this.props, defaultProps, true)
state = {
showPreview: !!this.props.light
}

componentDidMount () {
if (this.props.progressFrequency) {
const message = 'ReactPlayer: %cprogressFrequency%c is deprecated, please use %cprogressInterval%c instead'
console.warn(message, 'font-weight: bold', '', 'font-weight: bold', '')
}
}

shouldComponentUpdate (nextProps, nextState) {
return !isEqual(this.props, nextProps) || !isEqual(this.state, nextState)
}

componentWillUpdate (nextProps) {
const { light } = this.props
this.config = getConfig(nextProps, defaultProps)
Expand All @@ -61,50 +68,62 @@ export default class ReactPlayer extends Component {
this.setState({ showPreview: false })
}
}
onClickPreview = () => {

handleClickPreview = () => {
this.setState({ showPreview: false })
}

showPreview = () => {
this.setState({ showPreview: true })
}

getDuration = () => {
if (!this.player) return null
return this.player.getDuration()
}

getCurrentTime = () => {
if (!this.player) return null
return this.player.getCurrentTime()
}

getSecondsLoaded = () => {
if (!this.player) return null
return this.player.getSecondsLoaded()
}

getInternalPlayer = (key = 'player') => {
if (!this.player) return null
return this.player.getInternalPlayer(key)
}

seekTo = (fraction, type) => {
if (!this.player) return null
this.player.seekTo(fraction, type)
}
onReady = () => {

handleReady = () => {
this.props.onReady(this)
}

getActivePlayer (url) {
for (let Player of [ ...customPlayers, ...players ]) {
for (const Player of [...customPlayers, ...players]) {
if (Player.canPlay(url)) {
return Player
}
}
// Fall back to FilePlayer if nothing else can play the URL
return FilePlayer
}

wrapperRef = wrapper => {
this.wrapper = wrapper
}

activePlayerRef = player => {
this.player = player
}

renderActivePlayer (url, activePlayer) {
if (!url) return null
return (
Expand All @@ -114,26 +133,28 @@ export default class ReactPlayer extends Component {
ref={this.activePlayerRef}
config={this.config}
activePlayer={activePlayer}
onReady={this.onReady}
onReady={this.handleReady}
/>
)
}

sortPlayers (a, b) {
// Retain player order to prevent weird iframe behaviour when switching players
if (a && b) {
return a.key < b.key ? -1 : 1
}
return 0
}

render () {
const { url, controls, style, width, height, light, wrapper: Wrapper } = this.props
const showPreview = this.state.showPreview && url
const otherProps = omit(this.props, SUPPORTED_PROPS, DEPRECATED_CONFIG_PROPS)
const activePlayer = this.getActivePlayer(url)
const renderedActivePlayer = this.renderActivePlayer(url, activePlayer)
const preloadPlayers = renderPreloadPlayers(url, controls, this.config)
const players = [ renderedActivePlayer, ...preloadPlayers ].sort(this.sortPlayers)
const preview = <Preview url={url} light={light} onClick={this.onClickPreview} />
const players = [renderedActivePlayer, ...preloadPlayers].sort(this.sortPlayers)
const preview = <Preview url={url} light={light} onClick={this.handleClickPreview} />
return (
<Wrapper ref={this.wrapperRef} style={{ ...style, width, height }} {...otherProps}>
{showPreview ? preview : players}
Expand Down

0 comments on commit ae0f230

Please sign in to comment.