Commit 042a0024 authored by Luciano Pacheco's avatar Luciano Pacheco Committed by Commit Bot

Audio Player: let/const and arrow function

Convert `var` to let/const as pointed by presumit.

Convert an anonymous function to arrow function to avoid the `.bind(this)`.

No change in behavior.

Test: Manually tested the audio player (just in case).
Change-Id: I73be16d039476b9fe9a087798f596a521b1bdabc
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2525884
Auto-Submit: Luciano Pacheco <lucmult@chromium.org>
Reviewed-by: default avatarJeremie Boulic <jboulic@chromium.org>
Commit-Queue: Jeremie Boulic <jboulic@chromium.org>
Cr-Commit-Position: refs/heads/master@{#825293}
parent 39353c4f
......@@ -121,7 +121,7 @@ Polymer({
this.$.audio.addEventListener('ended', this.onAudioEnded.bind(this));
this.$.audio.addEventListener('error', this.onAudioError.bind(this));
var onAudioStatusUpdatedBound = this.onAudioStatusUpdate_.bind(this);
const onAudioStatusUpdatedBound = this.onAudioStatusUpdate_.bind(this);
this.$.audio.addEventListener('timeupdate', onAudioStatusUpdatedBound);
this.$.audio.addEventListener('ended', onAudioStatusUpdatedBound);
this.$.audio.addEventListener('play', onAudioStatusUpdatedBound);
......@@ -147,10 +147,10 @@ Polymer({
* @param {number} oldValue old value.
*/
currentTrackIndexChanged: function(newValue, oldValue) {
var currentTrackUrl = '';
let currentTrackUrl = '';
if (oldValue != newValue) {
var currentTrack = this.$.trackList.getCurrentTrack();
const currentTrack = this.$.trackList.getCurrentTrack();
if(currentTrack && currentTrack != this.$.trackInfo.track){
this.$.trackInfo.track = currentTrack;
this.$.trackInfo.artworkAvailable = !!currentTrack.artworkUrl;
......@@ -176,7 +176,7 @@ Polymer({
playingChanged: function(newValue, oldValue) {
if (newValue) {
if (!this.$.audio.src) {
var currentTrack = this.$.trackList.getCurrentTrack();
const currentTrack = this.$.trackList.getCurrentTrack();
if (currentTrack && currentTrack.url != this.$.audio.src) {
this.$.audio.src = currentTrack.url;
}
......@@ -284,13 +284,14 @@ Polymer({
advance_: function(forward, repeat) {
this.cancelAutoAdvance_();
var nextTrackIndex = this.$.trackList.getNextTrackIndex(forward, true);
var isNextTrackAvailable =
const nextTrackIndex = this.$.trackList.getNextTrackIndex(forward, true);
const isNextTrackAvailable =
(this.$.trackList.getNextTrackIndex(forward, repeat) !== -1);
this.playing = isNextTrackAvailable;
var shouldFireEvent = this.$.trackList.currentTrackIndex === nextTrackIndex;
const shouldFireEvent =
this.$.trackList.currentTrackIndex === nextTrackIndex;
this.$.trackList.currentTrackIndex = nextTrackIndex;
this.$.audio.currentTime = 0;
this.time = 0;
......@@ -319,29 +320,27 @@ Polymer({
*/
scheduleAutoAdvance_: function(forward, repeat) {
this.cancelAutoAdvance_();
var currentTrackIndex = this.currentTrackIndex;
var timerId = setTimeout(
function() {
// If the other timer is scheduled, do nothing.
if (this.autoAdvanceTimer_ !== timerId) {
return;
}
this.autoAdvanceTimer_ = null;
// If the track has been changed since the advance was scheduled, do
// nothing.
if (this.currentTrackIndex !== currentTrackIndex) {
return;
}
// We are advancing only if the next track is not known to be invalid.
// This prevents an endless auto-advancing in the case when all tracks
// are invalid (we will only visit each track once).
this.advance_(forward, repeat);
}.bind(this),
3000);
const currentTrackIndex = this.currentTrackIndex;
const timerId = setTimeout(() => {
// If the other timer is scheduled, do nothing.
if (this.autoAdvanceTimer_ !== timerId) {
return;
}
this.autoAdvanceTimer_ = null;
// If the track has been changed since the advance was scheduled, do
// nothing.
if (this.currentTrackIndex !== currentTrackIndex) {
return;
}
// We are advancing only if the next track is not known to be invalid.
// This prevents an endless auto-advancing in the case when all tracks
// are invalid (we will only visit each track once).
this.advance_(forward, repeat);
}, 3000);
this.autoAdvanceTimer_ = timerId;
},
......@@ -376,7 +375,7 @@ Polymer({
this.cancelAutoAdvance_();
this.$.trackList.tracks = tracks;
var currentTrack = this.$.trackList.getCurrentTrack();
const currentTrack = this.$.trackList.getCurrentTrack();
if (currentTrack && currentTrack.url != this.$.audio.src) {
this.$.audio.src = currentTrack.url;
this.$.audio.play();
......@@ -402,7 +401,7 @@ Polymer({
this.$.trackInfo.track.url === this.tracks[index].url){
this.$.trackInfo.notifyPath('track.title', this.tracks[index].title);
this.$.trackInfo.notifyPath('track.artist', this.tracks[index].artist);
var artworkUrl = this.tracks[index].artworkUrl;
const artworkUrl = this.tracks[index].artworkUrl;
if (artworkUrl) {
this.$.trackInfo.notifyPath('track.artworkUrl',
this.tracks[index].artworkUrl);
......
......@@ -11,7 +11,7 @@
* active: boolean
* }}
*/
var TrackInfo;
let TrackInfo;
(function() {
'use strict';
......@@ -99,7 +99,7 @@ var TrackInfo;
}
if (0 <= newValue && newValue < this.tracks.length) {
var currentPlayOrder = this.playOrder.indexOf(newValue);
const currentPlayOrder = this.playOrder.indexOf(newValue);
if (currentPlayOrder !== -1) {
// Success
this.set('tracks.' + newValue + '.active', true);
......@@ -146,8 +146,8 @@ var TrackInfo;
* @param {Event} event Click event.
*/
trackClicked: function(event) {
var index = ~~event.currentTarget.getAttribute('index');
var track = this.tracks[index];
const index = ~~event.currentTarget.getAttribute('index');
const track = this.tracks[index];
if (track) {
this.selectTrack(track);
}
......@@ -159,12 +159,12 @@ var TrackInfo;
* @private
*/
ensureTrackInViewport_: function(trackIndex) {
var trackElement = this.$$('.track[index="' + trackIndex + '"]');
const trackElement = this.$$('.track[index="' + trackIndex + '"]');
if (trackElement) {
var viewTop = this.scrollTop;
var viewHeight = this.clientHeight;
var elementTop = trackElement.offsetTop - this.offsetTop;
var elementHeight = trackElement.offsetHeight;
const viewTop = this.scrollTop;
const viewHeight = this.clientHeight;
const elementTop = trackElement.offsetTop - this.offsetTop;
const elementHeight = trackElement.offsetHeight;
if (elementTop <= viewTop) {
// Adjust the tops.
......@@ -231,8 +231,8 @@ var TrackInfo;
* track.
*/
selectTrack: function(track) {
var index = -1;
for (var i = 0; i < this.tracks.length; i++) {
let index = -1;
for (let i = 0; i < this.tracks.length; i++) {
if (this.tracks[i].url === track.url) {
index = i;
break;
......@@ -276,21 +276,21 @@ var TrackInfo;
return -1;
}
var defaultTrackIndex =
const defaultTrackIndex =
forward ? this.playOrder[0] : this.playOrder[this.tracks.length - 1];
var currentPlayOrder = this.playOrder.indexOf(this.currentTrackIndex);
const currentPlayOrder = this.playOrder.indexOf(this.currentTrackIndex);
console.assert(
(0 <= currentPlayOrder && currentPlayOrder < this.tracks.length),
'Insufficient TrackList.playOrder. The current track is not on the ' +
'track list.');
var newPlayOrder = currentPlayOrder + (forward ? +1 : -1);
const newPlayOrder = currentPlayOrder + (forward ? +1 : -1);
if (newPlayOrder === -1 || newPlayOrder === this.tracks.length) {
return cyclic ? defaultTrackIndex : -1;
}
var newTrackIndex = this.playOrder[newPlayOrder];
const newTrackIndex = this.playOrder[newPlayOrder];
console.assert(
(0 <= newTrackIndex && newTrackIndex < this.tracks.length),
'Insufficient TrackList.playOrder. New Play Order: ' + newPlayOrder);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment