Skip to content

Commit

Permalink
Now pauses videos based on their unique youtube API id, also removes …
Browse files Browse the repository at this point in the history
…API player data on unmount
  • Loading branch information
zachberry committed Aug 19, 2021
1 parent 087c437 commit ee6008e
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 10 deletions.
21 changes: 15 additions & 6 deletions packages/obonode/obojobo-chunks-youtube/youtube-player.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const { uuid } = Common.util
// They'll all get called once it loads
const instanceCallbacksForYouTubeReady = []

const loadedVideos = []
const loadedVideos = {}

// single global hangler that notifies all registered YouTubePlayer Components
const onYouTubeIframeAPIReadyHandler = () => {
Expand All @@ -25,6 +25,10 @@ class YouTubePlayer extends React.Component {
this.player = null
this.loadVideo = this.loadVideo.bind(this)
this.onStateChange = this.onStateChange.bind(this)

this.state = {
youTubePlayerId: null
}
}

componentDidMount() {
Expand All @@ -36,6 +40,10 @@ class YouTubePlayer extends React.Component {
}
}

componentWillUnmount() {
delete loadedVideos[this.state.youTubePlayerId]
}

componentDidUpdate(prevProps) {
// Don't update this component if the video properties haven't changed
if (!this.shouldVideoUpdate(prevProps)) {
Expand Down Expand Up @@ -87,8 +95,9 @@ class YouTubePlayer extends React.Component {
onStateChange: this.onStateChange
}
})
this.setState({ youTubePlayerId: this.player.id })

loadedVideos.push(this.player)
loadedVideos[this.player.id] = this.player
}

shouldVideoUpdate(prevProps) {
Expand All @@ -109,11 +118,11 @@ class YouTubePlayer extends React.Component {
onStateChange(playerState) {
switch (playerState.data) {
case 1: // video playing
for (let i = 0; i < loadedVideos.length; i++) {
if (loadedVideos[i].getVideoUrl() !== playerState.target.getVideoUrl()) {
loadedVideos[i].pauseVideo()
Object.values(loadedVideos).forEach(video => {
if (video.id !== playerState.target.id) {
video.pauseVideo()
}
}
})
break
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,15 @@ describe('YouTubePlayer', () => {
const tree = component.html()

expect(tree).toMatchSnapshot()

component.unmount()
})

test('YouTubePlayer updates video on content change', () => {
// Mock YouTube's Iframe Player object
window.YT = {
Player: jest.fn(() => ({
cueVideoById: jest.fn(),
getVideoUrl: jest.fn(),
pauseVideo: jest.fn()
}))
}
Expand Down Expand Up @@ -70,7 +71,6 @@ describe('YouTubePlayer', () => {
Player: jest.fn(() => ({
destroy: jest.fn(),
cueVideoById: jest.fn(),
getVideoUrl: jest.fn(),
pauseVideo: jest.fn()
}))
}
Expand Down Expand Up @@ -99,13 +99,13 @@ describe('YouTubePlayer', () => {
const player1 = {
destroy: jest.fn(),
cueVideoById: jest.fn(),
getVideoUrl: () => 'url-1',
id: 1,
pauseVideo: jest.fn()
}
const player2 = {
destroy: jest.fn(),
cueVideoById: jest.fn(),
getVideoUrl: () => 'url-2',
id: 2,
pauseVideo: jest.fn()
}

Expand Down

0 comments on commit ee6008e

Please sign in to comment.