From 97266b6dfca34844f4771e17e2ad62f831d97395 Mon Sep 17 00:00:00 2001 From: Calvin Montgomery Date: Thu, 11 Apr 2019 20:43:24 -0700 Subject: [PATCH] Better fix for jank dailymotion race conditions --- package.json | 2 +- player/dailymotion.coffee | 35 ++++++++++++++++++++++------------- 2 files changed, 23 insertions(+), 14 deletions(-) diff --git a/package.json b/package.json index db63eb79..00fe4cb3 100644 --- a/package.json +++ b/package.json @@ -2,7 +2,7 @@ "author": "Calvin Montgomery", "name": "CyTube", "description": "Online media synchronizer and chat", - "version": "3.64.0", + "version": "3.64.1", "repository": { "url": "http://github.com/calzoneman/sync" }, diff --git a/player/dailymotion.coffee b/player/dailymotion.coffee index 9a8bc1f4..71239445 100644 --- a/player/dailymotion.coffee +++ b/player/dailymotion.coffee @@ -5,6 +5,7 @@ window.DailymotionPlayer = class DailymotionPlayer extends Player @setMediaProperties(data) @initialVolumeSet = false + @playbackReadyCb = null waitUntilDefined(window, 'DM', => removeOld() @@ -26,8 +27,7 @@ window.DailymotionPlayer = class DailymotionPlayer extends Player ) @dm.addEventListener('apiready', => - @dm.ready = true - @dm.apiready = true + @dmReady = true @dm.addEventListener('ended', -> if CLIENT.leader socket.emit('playNext') @@ -53,50 +53,59 @@ window.DailymotionPlayer = class DailymotionPlayer extends Player # becomes unusable and attempting to load() will corrupt it and # crash the player with an error. As a short–medium term # workaround, mark the player as "not ready" until the next - # video loads + # playback_ready event @dm.addEventListener('video_end', => - @dm.ready = false + @dmReady = false ) @dm.addEventListener('playback_ready', => - @dm.ready = true + @dmReady = true + if @playbackReadyCb + @playbackReadyCb() + @playbackReadyCb = null ) ) ) load: (data) -> @setMediaProperties(data) - if @dm and @dm.apiready + if @dm and @dmReady @dm.load(data.id) @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 - console.error('WTF? DailymotionPlayer::load() called but dm is not ready') + console.error('WTF? DailymotionPlayer::load() called but @dm is undefined') pause: -> - if @dm and @dm.ready + if @dm and @dmReady @paused = true @dm.pause() play: -> - if @dm and @dm.ready + if @dm and @dmReady @paused = false @dm.play() seekTo: (time) -> - if @dm and @dm.ready + if @dm and @dmReady @dm.seek(time) setVolume: (volume) -> - if @dm and @dm.ready + if @dm and @dmReady @dm.setVolume(volume) getTime: (cb) -> - if @dm and @dm.ready + if @dm and @dmReady cb(@dm.currentTime) else cb(0) getVolume: (cb) -> - if @dm and @dm.ready + if @dm and @dmReady if @dm.muted cb(0) else