All files / src/events timeUpdate.js

7.69% Statements 2/26
0% Branches 0/12
20% Functions 1/5
7.69% Lines 2/26
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197                                                                                              31x                                                                                                                                                                                                                                                                                             31x            
/**
 * Imports the config module
 * @module config
 */
import config from "../config.js";
 
/**
 * Imports the Buffered Progress Elements visual class
 * @module visual/bufferedProgressElements
 */
import BufferedProgressElements from "../visual/bufferedProgressElements.js";
 
/**
 * Imports the Time Elements visual class.
 * @module visual/timeElements
 */
import TimeElements from "../visual/timeElements.js";
 
/**
 * Imports the Song Slider Elements visual class.
 * @module visual/songSliderElements
 */
import SongSliderElements from "../visual/songSliderElements.js";
 
/**
 * Imports the Song Played Progress Elements visual class.
 * @module visual/songPlayedProgressElements
 */
import SongPlayedProgressElements from "../visual/songPlayedProgressElements.js";
 
/**
 * Imports the Time utility class
 * @module utilities/Time
 */
import Time from "../utilities/time.js";
 
/**
 * Imports the Callback utility class
 * @module utilities/Callbacks
 */
import Callbacks from "../utilities/callbacks.js";
 
/**
 * AmplitudeJS Event Handler for Time Update
 *
 * @module events/TimeUpdate
 */
let TimeUpdate = (function() {
  /**
   * When the time updates on the active song, we sync the current time displays
   *
   * HANDLER FOR: timeupdate
   *
   * @access public
   */
  function handle() {
    /*
      Computes the buffered time.
    */
    computeBufferedTime();
 
    /*
      Sync the buffered progress elements.
    */
    BufferedProgressElements.sync();
 
    /*
      Updates the current time information.
    */
    updateTimeInformation();
 
    /*
      Run time callbacks
    */
    runTimeCallbacks();
  }
 
  /**
   * Computes the buffered time
   */
  function computeBufferedTime() {
    /*
      Help from: http://jsbin.com/badimipi/1/edit?html,js,output
    */
    if (config.audio.buffered.length - 1 >= 0) {
      let bufferedEnd = config.audio.buffered.end(
        config.audio.buffered.length - 1
      );
      let duration = config.audio.duration;
 
      config.buffered = (bufferedEnd / duration) * 100;
    }
  }
 
  /**
   * Updates the current time information.
   * @access private
   */
  function updateTimeInformation() {
    /*
      If the current song is not live, then
      we can update the time information. Otherwise the
      current time updates wouldn't mean much since the time
      is infinite.
    */
    if (!config.active_metadata.live) {
      /*
        Compute the current time
      */
      let currentTime = Time.computeCurrentTimes();
 
      /*
        Compute the song completion percentage
      */
      let songCompletionPercentage = Time.computeSongCompletionPercentage();
 
      /*
        Computes the song duration
      */
      let songDuration = Time.computeSongDuration();
 
      /*
        Sync the current time elements with the current
        location of the song and the song duration elements with
        the duration of the song.
      */
      TimeElements.syncCurrentTimes(currentTime);
 
      /*
        Sync the song slider elements.
      */
      SongSliderElements.sync(
        songCompletionPercentage,
        config.active_playlist,
        config.active_index
      );
 
      /*
        Sync the song played progress elements.
      */
      SongPlayedProgressElements.sync(songCompletionPercentage);
 
      /*
        Sync the duration time elements.
      */
      TimeElements.syncDurationTimes(currentTime, songDuration);
    }
  }
 
  /**
   * Runs a callback at a certain time in the song.
   */
  function runTimeCallbacks() {
    /*
      Gets the current seconds into the song.
    */
    let currentSeconds = Math.floor(config.audio.currentTime);
 
    /*
      Checks to see if there is a callback at the certain seconds into the song.
    */
    if (
      config.active_metadata.time_callbacks != undefined &&
      config.active_metadata.time_callbacks[currentSeconds] != undefined
    ) {
      /*
        Checks to see if the callback has been run. Since the time updates more than
        one second, we don't want the callback to run X times.
      */
      if (!config.active_metadata.time_callbacks[currentSeconds].run) {
        config.active_metadata.time_callbacks[currentSeconds].run = true;
        config.active_metadata.time_callbacks[currentSeconds]();
      }
    } else {
      /*
        Iterate over all of the callbacks for a song. If the song has one, we flag
        the run as false. This occurs because we have passed the active second for
        the callback, so we flag it as not run. It will either run again if the user
        seeks back or not run in the future.
      */
      for (var seconds in config.active_metadata.time_callbacks) {
        if (config.active_metadata.time_callbacks.hasOwnProperty(seconds)) {
          config.active_metadata.time_callbacks[seconds].run = false;
        }
      }
    }
  }
  /**
   * Returns public functions
   */
  return {
    handle: handle
  };
})();
 
export default TimeUpdate;