Better fix for jank dailymotion race conditions

This commit is contained in:
Calvin Montgomery 2019-04-11 20:43:24 -07:00
parent a3a2daff4c
commit 97266b6dfc
2 changed files with 23 additions and 14 deletions

View File

@ -2,7 +2,7 @@
"author": "Calvin Montgomery", "author": "Calvin Montgomery",
"name": "CyTube", "name": "CyTube",
"description": "Online media synchronizer and chat", "description": "Online media synchronizer and chat",
"version": "3.64.0", "version": "3.64.1",
"repository": { "repository": {
"url": "http://github.com/calzoneman/sync" "url": "http://github.com/calzoneman/sync"
}, },

View File

@ -5,6 +5,7 @@ window.DailymotionPlayer = class DailymotionPlayer extends Player
@setMediaProperties(data) @setMediaProperties(data)
@initialVolumeSet = false @initialVolumeSet = false
@playbackReadyCb = null
waitUntilDefined(window, 'DM', => waitUntilDefined(window, 'DM', =>
removeOld() removeOld()
@ -26,8 +27,7 @@ window.DailymotionPlayer = class DailymotionPlayer extends Player
) )
@dm.addEventListener('apiready', => @dm.addEventListener('apiready', =>
@dm.ready = true @dmReady = true
@dm.apiready = true
@dm.addEventListener('ended', -> @dm.addEventListener('ended', ->
if CLIENT.leader if CLIENT.leader
socket.emit('playNext') socket.emit('playNext')
@ -53,50 +53,59 @@ window.DailymotionPlayer = class DailymotionPlayer extends Player
# becomes unusable and attempting to load() will corrupt it and # becomes unusable and attempting to load() will corrupt it and
# crash the player with an error. As a shortmedium term # crash the player with an error. As a shortmedium term
# workaround, mark the player as "not ready" until the next # workaround, mark the player as "not ready" until the next
# video loads # playback_ready event
@dm.addEventListener('video_end', => @dm.addEventListener('video_end', =>
@dm.ready = false @dmReady = false
) )
@dm.addEventListener('playback_ready', => @dm.addEventListener('playback_ready', =>
@dm.ready = true @dmReady = true
if @playbackReadyCb
@playbackReadyCb()
@playbackReadyCb = null
) )
) )
) )
load: (data) -> load: (data) ->
@setMediaProperties(data) @setMediaProperties(data)
if @dm and @dm.apiready if @dm and @dmReady
@dm.load(data.id) @dm.load(data.id)
@dm.seek(data.currentTime) @dm.seek(data.currentTime)
else if @dm
# TODO: Player::load() needs to be made asynchronous in the future
console.log('Warning: load() called before DM is ready, queueing callback')
@playbackReadyCb = () =>
@dm.load(data.id)
@dm.seek(data.currentTime)
else else
console.error('WTF? DailymotionPlayer::load() called but dm is not ready') console.error('WTF? DailymotionPlayer::load() called but @dm is undefined')
pause: -> pause: ->
if @dm and @dm.ready if @dm and @dmReady
@paused = true @paused = true
@dm.pause() @dm.pause()
play: -> play: ->
if @dm and @dm.ready if @dm and @dmReady
@paused = false @paused = false
@dm.play() @dm.play()
seekTo: (time) -> seekTo: (time) ->
if @dm and @dm.ready if @dm and @dmReady
@dm.seek(time) @dm.seek(time)
setVolume: (volume) -> setVolume: (volume) ->
if @dm and @dm.ready if @dm and @dmReady
@dm.setVolume(volume) @dm.setVolume(volume)
getTime: (cb) -> getTime: (cb) ->
if @dm and @dm.ready if @dm and @dmReady
cb(@dm.currentTime) cb(@dm.currentTime)
else else
cb(0) cb(0)
getVolume: (cb) -> getVolume: (cb) ->
if @dm and @dm.ready if @dm and @dmReady
if @dm.muted if @dm.muted
cb(0) cb(0)
else else