Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#213 high cpu optimization #360

Merged
merged 2 commits into from
Apr 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion hugo/layouts/partials/player.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
data-turbolinks-permanent
>
<div class="player-placeholder"></div>
<div class="podcast-player-container">
<div class="podcast-player-container" data-target="player.container">
<div class="podcast-player container-fluid d-md-flex align-items-center flex-nowrap">
<div class="podcast-player-cover podcast-cover flex-shrink-0">
<a class="cover-image cover-image-online" data-target="player.cover player.link">
Expand Down
27 changes: 20 additions & 7 deletions hugo/src/js/controllers/player_controller.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import debounce from 'lodash/debounce';
import capitalize from 'lodash/capitalize';
import { debounce, capitalize, throttle } from 'lodash';

import { Events } from '../events';
import Controller from '../base_controller';
Expand Down Expand Up @@ -28,6 +27,7 @@ export default class extends Controller {
'mute',
'unmute',
'rate',
'container',
];

static getState() {
Expand Down Expand Up @@ -74,15 +74,18 @@ export default class extends Controller {

// https://developer.mozilla.org/en-US/docs/Web/Guide/Events/Media_events
addEventListeners() {
['timeupdate', 'durationchange', 'play', 'pause', 'ended'].forEach((event) => {
['durationchange', 'play', 'pause', 'ended'].forEach((event) => {
const handlerName = `on${capitalize(event)}`;
if (this[handlerName]) this.audioTarget.addEventListener(event, this[handlerName].bind(this));
});

const updateLoadingState = debounce(
(isLoading) => this.element.classList.toggle('player-loading', isLoading),
500
);
this.audioTarget.addEventListener('timeupdate', throttle(this.onTimeupdate.bind(this), 500));

const updateLoadingState = debounce((isLoading) => {
this.element.classList.toggle('player-loading', isLoading);
this.element.classList.remove('player-loading-completed');
}, 500);

const eventsLoadingOn = ['seeking', 'waiting', 'loadstart'];
const eventsLoadingOff = ['playing', 'seeked', 'canplay', 'loadeddata', 'error'];
eventsLoadingOn.forEach((event) =>
Expand Down Expand Up @@ -136,6 +139,16 @@ export default class extends Controller {
})
);
});

this.containerTarget.addEventListener('transitionend', (e) => {
if (e.pseudoElement !== '::after' || e.propertyName !== 'opacity') {
return;
}
const style = window.getComputedStyle(e.target, ':after');
if (style.opacity === '0') {
this.element.classList.add('player-loading-completed');
}
});
}

playPodcast(detail) {
Expand Down
17 changes: 12 additions & 5 deletions hugo/src/js/controllers/podcast_progress_controller.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { throttle } from 'lodash';
import Controller from '../base_controller';
import { composeTime, getLocalStorage } from '../utils';

Expand All @@ -6,9 +7,10 @@ export default class extends Controller {

initialize() {
super.initialize();
this.subscribe(`playing-progress-${this.numberTarget.innerText}`, (podcast) => {
this.renderProgress(podcast);
});
this.subscribe(
`playing-progress-${this.numberTarget.innerText}`,
throttle(this.renderProgress.bind(this), 1000)
);
}

connect() {
Expand All @@ -24,11 +26,16 @@ export default class extends Controller {
}

this.data.set('init', '1');
this.lastPercentage = 0;
}

renderProgress(podcast) {
this.progressTarget.style.display = 'block';
this.durationTarget.innerText = composeTime(podcast.duration);
this.barTarget.style.width = `${(podcast.currentTime / podcast.duration) * 100}%`;
this.durationTarget.innerText = composeTime(podcast.currentTime);
const percentage = (podcast.currentTime / podcast.duration) * 100;
if (Math.abs(percentage - this.lastPercentage) > 0.2) {
this.barTarget.style.width = `${percentage}%`;
this.lastPercentage = percentage;
}
}
}
4 changes: 4 additions & 0 deletions hugo/src/scss/_player.scss
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ $podcast-player-cover-margin: 0.5rem;
opacity: 1;
transition-delay: 200ms;
}

.player-loading-completed & {
animation: none;
}
}
}
.podcast-player {
Expand Down
2 changes: 2 additions & 0 deletions hugo/src/scss/main.scss
Original file line number Diff line number Diff line change
Expand Up @@ -840,6 +840,7 @@ $ripple-scale-out: 3.4;
[data-target='online.time'] {
cursor: default;
user-select: none;
transform: translateZ(0);
}
.podcast-progress {
position: absolute;
Expand All @@ -850,6 +851,7 @@ $ripple-scale-out: 3.4;
background-color: $gray-300;
z-index: 3;
pointer-events: none;
transform: translateZ(0);
}
.podcast-progress-bar {
position: absolute;
Expand Down
Loading