From dd5bf955c5cf9dc7a02a80a1b2daf38dea98bca0 Mon Sep 17 00:00:00 2001 From: Ayushya Amitabh Date: Wed, 6 May 2020 13:36:52 -0400 Subject: [PATCH] event blocker --- src/VideoListener.js | 81 ++++++++++++++++++++++---------------------- 1 file changed, 40 insertions(+), 41 deletions(-) diff --git a/src/VideoListener.js b/src/VideoListener.js index 0450ed4..64ff706 100644 --- a/src/VideoListener.js +++ b/src/VideoListener.js @@ -11,10 +11,10 @@ export default class VideoListener { source: '', element: null, }; + this.isUpdating = false; this.server = { status: PLAYER_STATUS.NOT_PLAYING, }; - this.caughtUp = true; this.listener = listener; this.updateStatus = this.updateStatus.bind(this); this.firstTimeSync = this.firstTimeSync.bind(this); @@ -23,53 +23,53 @@ export default class VideoListener { } updateStatus(newStatus) { this.video = Object.assign({}, this.video, newStatus); - if (this.server.status !== this.video.status && this.caughtUp === true) { - if (this.video.showName) this.listener(this.video); + if (this.server.status !== this.video.status) { + if (this.video.showName && !this.isUpdating) this.listener(this.video); } } doUpdate(serverPlayer) { this.server = serverPlayer; - this.caughtUp = false; - if (serverPlayer.updatedBy === window.localStorage.getItem('watchPartyUserUid')) { - this.caughtUp = true; - return; - } else { - // SEEK - console.log('GETTING UPDATE', serverPlayer, this.video); - if (this.video.element) { - switch (this.video.source) { - case PLAYER_SOURCE.NETFLIX: - window.postMessage({ type: 'SEEK', time: serverPlayer.time * 1000 }, '*'); - break; - case PLAYER_SOURCE.AMAZON: - this.video.element.play(); - this.video.element.currentTime = serverPlayer.time; - break; - case PLAYER_SOURCE.DISNEY: - this.video.element.currentTime = serverPlayer.time; - break; - default: - break; + if (!this.isUpdating) { + this.isUpdating = true; + if (serverPlayer.updatedBy === window.localStorage.getItem('watchPartyUserUid')) { + return; + } else { + // SEEK + if (this.video.element) { + switch (this.video.source) { + case PLAYER_SOURCE.NETFLIX: + window.postMessage({ type: 'SEEK', time: serverPlayer.time * 1000 }, '*'); + break; + case PLAYER_SOURCE.AMAZON: + this.video.element.play(); + this.video.element.currentTime = serverPlayer.time; + break; + case PLAYER_SOURCE.DISNEY: + this.video.element.currentTime = serverPlayer.time; + break; + default: + break; + } } - } - // PLAY OR PAUSE - if (this.video.element) { - switch (serverPlayer.status) { - case PLAYER_STATUS.PAUSED: - this.video.element.pause(); - break; - case PLAYER_STATUS.NOT_PLAYING: - this.video.element.pause(); - break; - case PLAYER_STATUS.PLAYING: - this.video.element.play(); - break; - default: - break; + // PLAY OR PAUSE + if (this.video.element) { + switch (serverPlayer.status) { + case PLAYER_STATUS.PAUSED: + this.video.element.pause(); + break; + case PLAYER_STATUS.NOT_PLAYING: + this.video.element.pause(); + break; + case PLAYER_STATUS.PLAYING: + this.video.element.play(); + break; + default: + break; + } } } + this.isUpdating = false; } - this.caughtUp = true; } timeout(n) { return new Promise((resolve, reject) => { @@ -130,7 +130,6 @@ export default class VideoListener { }); } videoElement.onplay = async (ev) => { - console.log(ev); this.updateStatus({ status: PLAYER_STATUS.PLAYING, time: videoElement.currentTime,