Skip to content

Commit 2b62811

Browse files
committed
Refactor player rendering logic
1 parent 00b9615 commit 2b62811

1 file changed

Lines changed: 31 additions & 25 deletions

File tree

src/ReactPlayer.js

Lines changed: 31 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -104,55 +104,61 @@ export default class ReactPlayer extends Component {
104104
}
105105
this.progressTimeout = setTimeout(this.progress, this.props.progressFrequency)
106106
}
107-
renderPlayers () {
108-
// Build array of players to render based on URL and preload config
109-
const { url } = this.props
110-
if (!url) {
111-
return []
112-
}
113-
const renderPlayers = []
107+
renderActivePlayer (url) {
108+
if (!url) return null
114109
for (let Player of SUPPORTED_PLAYERS) {
115110
if (Player.canPlay(url)) {
116-
renderPlayers.push(Player)
111+
return this.renderPlayer(Player)
117112
}
118113
}
119114
// Fall back to FilePlayer if nothing else can play the URL
120-
if (renderPlayers.length === 0) {
121-
renderPlayers.push(FilePlayer)
122-
}
115+
return this.renderPlayer(FilePlayer)
116+
}
117+
renderPlayer = Player => {
118+
return (
119+
<Player
120+
{...this.props}
121+
ref={this.ref}
122+
key={Player.displayName}
123+
config={this.config}
124+
/>
125+
)
126+
}
127+
ref = player => {
128+
this.player = player
129+
}
130+
renderPreloadPlayers (url) {
131+
if (!url) return null
123132
// Render additional players if preload config is set
133+
const preloadPlayers = []
124134
if (!YouTube.canPlay(url) && this.config.youtube.preload) {
125-
renderPlayers.push(YouTube)
135+
preloadPlayers.push(YouTube)
126136
}
127137
if (!Vimeo.canPlay(url) && this.config.vimeo.preload) {
128-
renderPlayers.push(Vimeo)
138+
preloadPlayers.push(Vimeo)
129139
}
130140
if (!DailyMotion.canPlay(url) && this.config.dailymotion.preload) {
131-
renderPlayers.push(DailyMotion)
141+
preloadPlayers.push(DailyMotion)
132142
}
133-
return renderPlayers.map(this.renderPlayer)
143+
return preloadPlayers.map(this.renderPreloadPlayer)
134144
}
135-
ref = player => {
136-
this.player = player
137-
}
138-
renderPlayer = Player => {
139-
const active = Player.canPlay(this.props.url)
140-
const props = active ? { ...this.props, ref: this.ref } : {}
145+
renderPreloadPlayer = Player => {
141146
return (
142147
<Player
143-
{...props}
144148
key={Player.displayName}
145149
config={this.config}
146150
/>
147151
)
148152
}
149153
render () {
150-
const { style, width, height } = this.props
154+
const { url, style, width, height } = this.props
151155
const otherProps = omit(this.props, SUPPORTED_PROPS, DEPRECATED_CONFIG_PROPS)
152-
const players = this.renderPlayers()
156+
const activePlayer = this.renderActivePlayer(url)
157+
const preloadPlayers = this.renderPreloadPlayers(url)
153158
return (
154159
<div style={{ ...style, width, height }} {...otherProps}>
155-
{players}
160+
{activePlayer}
161+
{preloadPlayers}
156162
</div>
157163
)
158164
}

0 commit comments

Comments
 (0)