1
- import Howl from './Howl' ;
1
+ import Howl , { HowlXHROptions } from './Howl' ;
2
2
import Howler from './howler' ;
3
3
4
4
export const cache = { } ;
5
5
6
6
/**
7
7
* Buffer a sound from URL, Data URI or cache and decode to audio source (Web Audio API).
8
- * @param {Howl } self
9
8
*/
10
- export function loadBuffer ( self ) {
11
- var url = self . _src ;
9
+ export function loadBuffer ( self : Howl ) {
10
+ var url = self . _src as string ;
12
11
13
12
// Check if the buffer has already been cached and use it instead.
14
13
if ( cache [ url ] ) {
@@ -33,18 +32,19 @@ export function loadBuffer(self) {
33
32
} else {
34
33
// Load the buffer from the URL.
35
34
var xhr = new XMLHttpRequest ( ) ;
36
- xhr . open ( self . _xhr . method , url , true ) ;
37
- xhr . withCredentials = self . _xhr . withCredentials ;
35
+ xhr . open ( ( self . _xhr as HowlXHROptions ) . method as string , url , true ) ;
36
+ xhr . withCredentials = ( self . _xhr as HowlXHROptions )
37
+ . withCredentials as boolean ;
38
38
xhr . responseType = 'arraybuffer' ;
39
39
40
40
// Apply any custom headers to the request.
41
- if ( self . _xhr . headers ) {
42
- Object . keys ( self . _xhr . headers ) . forEach ( function ( key ) {
43
- xhr . setRequestHeader ( key , self . _xhr . headers [ key ] ) ;
41
+ if ( self . _xhr as HowlXHROptions ) {
42
+ Object . keys ( self . _xhr as HowlXHROptions ) . forEach ( function ( key ) {
43
+ xhr . setRequestHeader ( key , ( self . _xhr as HowlXHROptions ) [ key ] ) ;
44
44
} ) ;
45
45
}
46
46
47
- xhr . onload = function ( ) {
47
+ xhr . onload = ( ) => {
48
48
// Make sure we get a successful response back.
49
49
var code = ( xhr . status + '' ) [ 0 ] ;
50
50
if ( code !== '0' && code !== '2' && code !== '3' ) {
@@ -58,7 +58,7 @@ export function loadBuffer(self) {
58
58
59
59
decodeAudioData ( xhr . response , self ) ;
60
60
} ;
61
- xhr . onerror = function ( ) {
61
+ xhr . onerror = ( ) => {
62
62
// If there is an error, switch to HTML5 Audio.
63
63
if ( self . _webAudio ) {
64
64
self . _html5 = true ;
@@ -74,20 +74,20 @@ export function loadBuffer(self) {
74
74
75
75
/**
76
76
* Send the XHR request wrapped in a try/catch.
77
- * @param { Object } xhr XHR to send.
77
+ * @param xhr XHR to send.
78
78
*/
79
- function safeXhrSend ( xhr ) {
79
+ function safeXhrSend ( xhr : XMLHttpRequest ) {
80
80
try {
81
81
xhr . send ( ) ;
82
82
} catch ( e ) {
83
- xhr . onerror ( ) ;
83
+ console . error ( 'XHR Request failed: ' , e ) ;
84
84
}
85
85
}
86
86
87
87
/**
88
88
* Decode audio data from an array buffer.
89
- * @param { ArrayBuffer } arraybuffer The audio data.
90
- * @param { Howl } self
89
+ * @param arraybuffer The audio data.
90
+ * @param self
91
91
*/
92
92
function decodeAudioData ( arraybuffer : ArrayBuffer , self : Howl ) {
93
93
// Fire a load error if something broke.
@@ -98,7 +98,7 @@ function decodeAudioData(arraybuffer: ArrayBuffer, self: Howl) {
98
98
// Load the sound on success.
99
99
function success ( buffer : AudioBuffer ) {
100
100
if ( buffer && self . _sounds . length > 0 ) {
101
- cache [ self . _src ] = buffer ;
101
+ cache [ self . _src as string ] = buffer ;
102
102
loadSound ( self , buffer ) ;
103
103
} else {
104
104
error ( ) ;
@@ -118,8 +118,8 @@ function decodeAudioData(arraybuffer: ArrayBuffer, self: Howl) {
118
118
119
119
/**
120
120
* Sound is now loaded, so finish setting everything up and fire the loaded event.
121
- * @param { Howl } self
122
- * @param { Object } buffer The decoded buffer sound source.
121
+ * @param self
122
+ * @param buffer The decoded buffer sound source.
123
123
*/
124
124
function loadSound ( self : Howl , buffer ?: AudioBuffer ) {
125
125
// Set the duration.
@@ -140,6 +140,7 @@ function loadSound(self: Howl, buffer?: AudioBuffer) {
140
140
}
141
141
}
142
142
143
+ // NOTE: Maybe remove these
143
144
export const isHTMLAudioElement = ( node : any ) : node is HTMLAudioElement =>
144
145
( node as HTMLAudioElement ) . playbackRate !== undefined ;
145
146
0 commit comments