All files / src/utilities callbacks.js

46.15% Statements 24/52
25% Branches 1/4
12.5% Functions 3/24
46.15% Lines 24/52
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 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238                                  31x                 159x               159x               159x               159x               159x               159x               159x               159x               159x               159x               159x               159x               159x               159x               159x               159x               159x               159x               159x               159x               159x                                   853x                                                   31x              
/**
 * Imports the config module
 * @module config
 */
import config from "../config.js";
 
/**
 * Imports the debug module
 * @module utilities/debug
 */
import Debug from "./debug.js";
 
/**
 * AmplitudeJS Callback Utility
 *
 * @module utilities/callbacks
 */
let Callbacks = (function() {
  /**
   * Initializes the callbacks for the player.
   */
  function initialize() {
    /*
      Event: abort
      https://www.w3schools.com/tags/av_event_abort.asp
    */
    config.audio.addEventListener("abort", function() {
      run("abort");
    });
 
    /*
      Event: error
      https://www.w3schools.com/tags/av_event_error.asp
    */
    config.audio.addEventListener("error", function() {
      run("error");
    });
 
    /*
      Event: loadeddata
      https://www.w3schools.com/tags/av_event_loadeddata.asp
    */
    config.audio.addEventListener("loadeddata", function() {
      run("loadeddata");
    });
 
    /*
      Event: loadedmetadata
      https://www.w3schools.com/tags/av_event_loadedmetadata.asp
    */
    config.audio.addEventListener("loadedmetadata", function() {
      run("loadedmetadata");
    });
 
    /*
      Event: loadstart
      https://www.w3schools.com/tags/av_event_loadstart.asp
    */
    config.audio.addEventListener("loadstart", function() {
      run("loadstart");
    });
 
    /*
      Event: pause
      https://www.w3schools.com/tags/av_event_pause.asp
    */
    config.audio.addEventListener("pause", function() {
      run("pause");
    });
 
    /*
      Event: playing
      https://www.w3schools.com/tags/av_event_playing.asp
    */
    config.audio.addEventListener("playing", function() {
      run("playing");
    });
 
    /*
      Event: play
      https://www.w3schools.com/tags/av_event_play.asp
    */
    config.audio.addEventListener("play", function() {
      run("play");
    });
 
    /*
      Event: progress
      https://www.w3schools.com/tags/av_event_progress.asp
    */
    config.audio.addEventListener("progress", function() {
      run("progress");
    });
 
    /*
      Event: ratechange
      https://www.w3schools.com/tags/av_event_ratechange.asp
    */
    config.audio.addEventListener("ratechange", function() {
      run("ratechange");
    });
 
    /*
      Event: seeked
      https://www.w3schools.com/tags/av_event_seeked.asp
    */
    config.audio.addEventListener("seeked", function() {
      run("seeked");
    });
 
    /*
      Event: seeking
      https://www.w3schools.com/tags/av_event_seeking.asp
    */
    config.audio.addEventListener("seeking", function() {
      run("seeking");
    });
 
    /*
      Event: stalled
      https://www.w3schools.com/tags/av_event_stalled.asp
    */
    config.audio.addEventListener("stalled", function() {
      run("stalled");
    });
 
    /*
      Event: suspend
      https://www.w3schools.com/tags/av_event_suspend.asp
    */
    config.audio.addEventListener("suspend", function() {
      run("suspend");
    });
 
    /*
      Event: timeupdate
      https://www.w3schools.com/tags/av_event_timeupdate.asp
    */
    config.audio.addEventListener("timeupdate", function() {
      run("timeupdate");
    });
 
    /*
      Event: volumechange
      https://www.w3schools.com/tags/av_event_volumechange.asp
    */
    config.audio.addEventListener("volumechange", function() {
      run("volumechange");
    });
 
    /*
      Event: waiting
      https://www.w3schools.com/tags/av_event_waiting.asp
    */
    config.audio.addEventListener("waiting", function() {
      run("waiting");
    });
 
    /*
      Event: canplay
      https://www.w3schools.com/tags/av_event_canplay.asp
    */
    config.audio.addEventListener("canplay", function() {
      run("canplay");
    });
 
    /*
      Event: canplaythrough
      https://www.w3schools.com/tags/av_event_canplaythrough.asp
    */
    config.audio.addEventListener("canplaythrough", function() {
      run("canplaythrough");
    });
 
    /*
      Event: durationchange
      https://www.w3schools.com/tags/av_event_durationchange.asp
    */
    config.audio.addEventListener("durationchange", function() {
      run("durationchange");
    });
 
    /*
      Event: ended
      https://www.w3schools.com/tags/av_event_ended.asp
    */
    config.audio.addEventListener("ended", function() {
      run("ended");
    });
  }
 
  /**
   * Runs a user defined callback method
   *
   * Public Accessor: Callbacks.run( callbackName )
   *
   * @access public
   * @param {string} callbackName - The name of the callback we are going to run.
   */
  function run(callbackName) {
    /*
      Checks to see if a user defined a callback method for the
      callback we are running.
    */
    Iif (config.callbacks[callbackName]) {
      /*
        Build the callback function
      */
      let callbackFunction = config.callbacks[callbackName];
 
      /*
        Write a debug message stating the callback we are running
      */
      Debug.writeMessage("Running Callback: " + callbackName);
 
      /*
        Run the callback function and catch any errors
      */
      try {
        callbackFunction();
      } catch (error) {
        if (error.message == "CANCEL EVENT") {
          throw error;
        } else {
          Debug.writeMessage("Callback error: " + error.message);
        }
      }
    }
  }
 
  return {
    initialize: initialize,
    run: run
  };
})();
 
export default Callbacks;