From 4b15d9f201455f653caadf9d8a75ed868028a497 Mon Sep 17 00:00:00 2001 From: inkfarer Date: Wed, 9 Oct 2024 07:26:12 +0300 Subject: [PATCH] Prevent re-creating caster elements on break screen when it is not necessary so caster video feeds aren't reloaded on all caster updates --- src/graphics/break/scripts/casters.ts | 108 +++++++++++++++---------- src/graphics/break/styles/casters.scss | 4 + 2 files changed, 71 insertions(+), 41 deletions(-) diff --git a/src/graphics/break/scripts/casters.ts b/src/graphics/break/scripts/casters.ts index b08f72f..940eaa2 100644 --- a/src/graphics/break/scripts/casters.ts +++ b/src/graphics/break/scripts/casters.ts @@ -3,12 +3,13 @@ import { elementById } from '../../helpers/elem'; import gsap from 'gsap'; import { getPrevious } from '../../helpers/object'; import { isBlank } from '../../helpers/string'; +import { Caster } from 'schemas'; const castersWrapper = elementById('info-bar-casters'); const twittersWrapper = elementById('info-bar-twitters'); const castersLayout = elementById('casters-layout'); -casters.on('change', newValue => { +casters.on('change', (newValue, oldValue) => { const values = Object.values(newValue); castersWrapper.innerHTML = values.reduce((existing, elem) => { @@ -24,52 +25,47 @@ casters.on('change', newValue => { gsap.set([ castersWrapper, twittersWrapper ], { scale: values.length > 2 ? 0.7 : 1 }); - const casterWidth = getCasterWidth(values.length); - castersLayout.innerHTML = values.reduce((result, elem) => { - result += ` -
- `; + const newCasterIds = Object.keys(newValue); + const oldCasterIds = oldValue == null ? null : Object.keys(oldValue); + const shouldRecreateCasterElements = + oldCasterIds == null + || newCasterIds.length !== oldCasterIds.length + || newCasterIds.some((elem, i) => oldCasterIds[i] !== elem); - if (!isBlank(elem.videoUrl)) { + if (shouldRecreateCasterElements) { + const casterWidth = getCasterWidth(values.length); + castersLayout.innerHTML = values.reduce((result, elem, i) => { result += ` -
- +
+
+ ${getCasterVisual(elem)} +
+
+ + +
`; - } else if (!isBlank(elem.imageUrl)) { - result += ` - - `; - } else { - result += ` - - `; - } - result += ` -
- - -
-
- ` + return result; + }, ''); + } else { + newCasterIds.forEach((casterId) => { + const casterElem = document.querySelector(`[data-caster-id="${casterId}"]`); + const caster = newValue[casterId]; + const oldCaster = oldValue[casterId]; + (casterElem.querySelector('.caster-name') as FittedText).text = caster.name; + (casterElem.querySelector('.caster-twitter') as FittedText).text = caster.twitter; - return result; - }, ''); + if (caster.videoUrl !== oldCaster.videoUrl || (isBlank(caster.videoUrl) && caster.imageUrl !== oldCaster.imageUrl)) { + casterElem.querySelector('.caster-visual-wrapper').innerHTML = getCasterVisual(caster); + } + }); + } }); const casterInfoLoopTl = gsap.timeline({ repeat: -1 }); @@ -94,3 +90,33 @@ function getCasterWidth(casterCount: number): number { return 625; } } + +function getCasterVisual(caster: Caster): string { + if (!isBlank(caster.videoUrl)) { + return ` +
+ +
+ `; + } else if (!isBlank(caster.imageUrl)) { + return ` + + `; + } else { + return ` + + `; + } + +} diff --git a/src/graphics/break/styles/casters.scss b/src/graphics/break/styles/casters.scss index 2c554d4..be72202 100644 --- a/src/graphics/break/styles/casters.scss +++ b/src/graphics/break/styles/casters.scss @@ -23,6 +23,10 @@ position: relative; } + .caster-visual-wrapper { + display: contents; + } + .caster-nametag { display: flex; flex-direction: column;