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({ ...@@ -121,7 +121,7 @@ Polymer({
this.$.audio.addEventListener('ended', this.onAudioEnded.bind(this)); this.$.audio.addEventListener('ended', this.onAudioEnded.bind(this));
this.$.audio.addEventListener('error', this.onAudioError.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('timeupdate', onAudioStatusUpdatedBound);
this.$.audio.addEventListener('ended', onAudioStatusUpdatedBound); this.$.audio.addEventListener('ended', onAudioStatusUpdatedBound);
this.$.audio.addEventListener('play', onAudioStatusUpdatedBound); this.$.audio.addEventListener('play', onAudioStatusUpdatedBound);
...@@ -147,10 +147,10 @@ Polymer({ ...@@ -147,10 +147,10 @@ Polymer({
* @param {number} oldValue old value. * @param {number} oldValue old value.
*/ */
currentTrackIndexChanged: function(newValue, oldValue) { currentTrackIndexChanged: function(newValue, oldValue) {
var currentTrackUrl = ''; let currentTrackUrl = '';
if (oldValue != newValue) { if (oldValue != newValue) {
var currentTrack = this.$.trackList.getCurrentTrack(); const currentTrack = this.$.trackList.getCurrentTrack();
if(currentTrack && currentTrack != this.$.trackInfo.track){ if(currentTrack && currentTrack != this.$.trackInfo.track){
this.$.trackInfo.track = currentTrack; this.$.trackInfo.track = currentTrack;
this.$.trackInfo.artworkAvailable = !!currentTrack.artworkUrl; this.$.trackInfo.artworkAvailable = !!currentTrack.artworkUrl;
...@@ -176,7 +176,7 @@ Polymer({ ...@@ -176,7 +176,7 @@ Polymer({
playingChanged: function(newValue, oldValue) { playingChanged: function(newValue, oldValue) {
if (newValue) { if (newValue) {
if (!this.$.audio.src) { if (!this.$.audio.src) {
var currentTrack = this.$.trackList.getCurrentTrack(); const currentTrack = this.$.trackList.getCurrentTrack();
if (currentTrack && currentTrack.url != this.$.audio.src) { if (currentTrack && currentTrack.url != this.$.audio.src) {
this.$.audio.src = currentTrack.url; this.$.audio.src = currentTrack.url;
} }
...@@ -284,13 +284,14 @@ Polymer({ ...@@ -284,13 +284,14 @@ Polymer({
advance_: function(forward, repeat) { advance_: function(forward, repeat) {
this.cancelAutoAdvance_(); this.cancelAutoAdvance_();
var nextTrackIndex = this.$.trackList.getNextTrackIndex(forward, true); const nextTrackIndex = this.$.trackList.getNextTrackIndex(forward, true);
var isNextTrackAvailable = const isNextTrackAvailable =
(this.$.trackList.getNextTrackIndex(forward, repeat) !== -1); (this.$.trackList.getNextTrackIndex(forward, repeat) !== -1);
this.playing = isNextTrackAvailable; this.playing = isNextTrackAvailable;
var shouldFireEvent = this.$.trackList.currentTrackIndex === nextTrackIndex; const shouldFireEvent =
this.$.trackList.currentTrackIndex === nextTrackIndex;
this.$.trackList.currentTrackIndex = nextTrackIndex; this.$.trackList.currentTrackIndex = nextTrackIndex;
this.$.audio.currentTime = 0; this.$.audio.currentTime = 0;
this.time = 0; this.time = 0;
...@@ -319,10 +320,9 @@ Polymer({ ...@@ -319,10 +320,9 @@ Polymer({
*/ */
scheduleAutoAdvance_: function(forward, repeat) { scheduleAutoAdvance_: function(forward, repeat) {
this.cancelAutoAdvance_(); this.cancelAutoAdvance_();
var currentTrackIndex = this.currentTrackIndex; const currentTrackIndex = this.currentTrackIndex;
var timerId = setTimeout( const timerId = setTimeout(() => {
function() {
// If the other timer is scheduled, do nothing. // If the other timer is scheduled, do nothing.
if (this.autoAdvanceTimer_ !== timerId) { if (this.autoAdvanceTimer_ !== timerId) {
return; return;
...@@ -340,8 +340,7 @@ Polymer({ ...@@ -340,8 +340,7 @@ Polymer({
// This prevents an endless auto-advancing in the case when all tracks // This prevents an endless auto-advancing in the case when all tracks
// are invalid (we will only visit each track once). // are invalid (we will only visit each track once).
this.advance_(forward, repeat); this.advance_(forward, repeat);
}.bind(this), }, 3000);
3000);
this.autoAdvanceTimer_ = timerId; this.autoAdvanceTimer_ = timerId;
}, },
...@@ -376,7 +375,7 @@ Polymer({ ...@@ -376,7 +375,7 @@ Polymer({
this.cancelAutoAdvance_(); this.cancelAutoAdvance_();
this.$.trackList.tracks = tracks; this.$.trackList.tracks = tracks;
var currentTrack = this.$.trackList.getCurrentTrack(); const currentTrack = this.$.trackList.getCurrentTrack();
if (currentTrack && currentTrack.url != this.$.audio.src) { if (currentTrack && currentTrack.url != this.$.audio.src) {
this.$.audio.src = currentTrack.url; this.$.audio.src = currentTrack.url;
this.$.audio.play(); this.$.audio.play();
...@@ -402,7 +401,7 @@ Polymer({ ...@@ -402,7 +401,7 @@ Polymer({
this.$.trackInfo.track.url === this.tracks[index].url){ this.$.trackInfo.track.url === this.tracks[index].url){
this.$.trackInfo.notifyPath('track.title', this.tracks[index].title); this.$.trackInfo.notifyPath('track.title', this.tracks[index].title);
this.$.trackInfo.notifyPath('track.artist', this.tracks[index].artist); this.$.trackInfo.notifyPath('track.artist', this.tracks[index].artist);
var artworkUrl = this.tracks[index].artworkUrl; const artworkUrl = this.tracks[index].artworkUrl;
if (artworkUrl) { if (artworkUrl) {
this.$.trackInfo.notifyPath('track.artworkUrl', this.$.trackInfo.notifyPath('track.artworkUrl',
this.tracks[index].artworkUrl); this.tracks[index].artworkUrl);
......
...@@ -11,7 +11,7 @@ ...@@ -11,7 +11,7 @@
* active: boolean * active: boolean
* }} * }}
*/ */
var TrackInfo; let TrackInfo;
(function() { (function() {
'use strict'; 'use strict';
...@@ -99,7 +99,7 @@ var TrackInfo; ...@@ -99,7 +99,7 @@ var TrackInfo;
} }
if (0 <= newValue && newValue < this.tracks.length) { if (0 <= newValue && newValue < this.tracks.length) {
var currentPlayOrder = this.playOrder.indexOf(newValue); const currentPlayOrder = this.playOrder.indexOf(newValue);
if (currentPlayOrder !== -1) { if (currentPlayOrder !== -1) {
// Success // Success
this.set('tracks.' + newValue + '.active', true); this.set('tracks.' + newValue + '.active', true);
...@@ -146,8 +146,8 @@ var TrackInfo; ...@@ -146,8 +146,8 @@ var TrackInfo;
* @param {Event} event Click event. * @param {Event} event Click event.
*/ */
trackClicked: function(event) { trackClicked: function(event) {
var index = ~~event.currentTarget.getAttribute('index'); const index = ~~event.currentTarget.getAttribute('index');
var track = this.tracks[index]; const track = this.tracks[index];
if (track) { if (track) {
this.selectTrack(track); this.selectTrack(track);
} }
...@@ -159,12 +159,12 @@ var TrackInfo; ...@@ -159,12 +159,12 @@ var TrackInfo;
* @private * @private
*/ */
ensureTrackInViewport_: function(trackIndex) { ensureTrackInViewport_: function(trackIndex) {
var trackElement = this.$$('.track[index="' + trackIndex + '"]'); const trackElement = this.$$('.track[index="' + trackIndex + '"]');
if (trackElement) { if (trackElement) {
var viewTop = this.scrollTop; const viewTop = this.scrollTop;
var viewHeight = this.clientHeight; const viewHeight = this.clientHeight;
var elementTop = trackElement.offsetTop - this.offsetTop; const elementTop = trackElement.offsetTop - this.offsetTop;
var elementHeight = trackElement.offsetHeight; const elementHeight = trackElement.offsetHeight;
if (elementTop <= viewTop) { if (elementTop <= viewTop) {
// Adjust the tops. // Adjust the tops.
...@@ -231,8 +231,8 @@ var TrackInfo; ...@@ -231,8 +231,8 @@ var TrackInfo;
* track. * track.
*/ */
selectTrack: function(track) { selectTrack: function(track) {
var index = -1; let index = -1;
for (var i = 0; i < this.tracks.length; i++) { for (let i = 0; i < this.tracks.length; i++) {
if (this.tracks[i].url === track.url) { if (this.tracks[i].url === track.url) {
index = i; index = i;
break; break;
...@@ -276,21 +276,21 @@ var TrackInfo; ...@@ -276,21 +276,21 @@ var TrackInfo;
return -1; return -1;
} }
var defaultTrackIndex = const defaultTrackIndex =
forward ? this.playOrder[0] : this.playOrder[this.tracks.length - 1]; 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( console.assert(
(0 <= currentPlayOrder && currentPlayOrder < this.tracks.length), (0 <= currentPlayOrder && currentPlayOrder < this.tracks.length),
'Insufficient TrackList.playOrder. The current track is not on the ' + 'Insufficient TrackList.playOrder. The current track is not on the ' +
'track list.'); 'track list.');
var newPlayOrder = currentPlayOrder + (forward ? +1 : -1); const newPlayOrder = currentPlayOrder + (forward ? +1 : -1);
if (newPlayOrder === -1 || newPlayOrder === this.tracks.length) { if (newPlayOrder === -1 || newPlayOrder === this.tracks.length) {
return cyclic ? defaultTrackIndex : -1; return cyclic ? defaultTrackIndex : -1;
} }
var newTrackIndex = this.playOrder[newPlayOrder]; const newTrackIndex = this.playOrder[newPlayOrder];
console.assert( console.assert(
(0 <= newTrackIndex && newTrackIndex < this.tracks.length), (0 <= newTrackIndex && newTrackIndex < this.tracks.length),
'Insufficient TrackList.playOrder. New Play Order: ' + newPlayOrder); '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