Skip to content

Commit 740e193

Browse files
committed
WIP refactor
1 parent 2371db2 commit 740e193

File tree

3 files changed

+30
-29
lines changed

3 files changed

+30
-29
lines changed

src/helpers.ts

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
1-
import Howl from './Howl';
1+
import Howl, { HowlXHROptions } from './Howl';
22
import Howler from './howler';
33

44
export const cache = {};
55

66
/**
77
* Buffer a sound from URL, Data URI or cache and decode to audio source (Web Audio API).
8-
* @param {Howl} self
98
*/
10-
export function loadBuffer(self) {
11-
var url = self._src;
9+
export function loadBuffer(self: Howl) {
10+
var url = self._src as string;
1211

1312
// Check if the buffer has already been cached and use it instead.
1413
if (cache[url]) {
@@ -33,18 +32,19 @@ export function loadBuffer(self) {
3332
} else {
3433
// Load the buffer from the URL.
3534
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;
3838
xhr.responseType = 'arraybuffer';
3939

4040
// 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]);
4444
});
4545
}
4646

47-
xhr.onload = function () {
47+
xhr.onload = () => {
4848
// Make sure we get a successful response back.
4949
var code = (xhr.status + '')[0];
5050
if (code !== '0' && code !== '2' && code !== '3') {
@@ -58,7 +58,7 @@ export function loadBuffer(self) {
5858

5959
decodeAudioData(xhr.response, self);
6060
};
61-
xhr.onerror = function () {
61+
xhr.onerror = () => {
6262
// If there is an error, switch to HTML5 Audio.
6363
if (self._webAudio) {
6464
self._html5 = true;
@@ -74,20 +74,20 @@ export function loadBuffer(self) {
7474

7575
/**
7676
* Send the XHR request wrapped in a try/catch.
77-
* @param {Object} xhr XHR to send.
77+
* @param xhr XHR to send.
7878
*/
79-
function safeXhrSend(xhr) {
79+
function safeXhrSend(xhr: XMLHttpRequest) {
8080
try {
8181
xhr.send();
8282
} catch (e) {
83-
xhr.onerror();
83+
console.error('XHR Request failed: ', e);
8484
}
8585
}
8686

8787
/**
8888
* 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
9191
*/
9292
function decodeAudioData(arraybuffer: ArrayBuffer, self: Howl) {
9393
// Fire a load error if something broke.
@@ -98,7 +98,7 @@ function decodeAudioData(arraybuffer: ArrayBuffer, self: Howl) {
9898
// Load the sound on success.
9999
function success(buffer: AudioBuffer) {
100100
if (buffer && self._sounds.length > 0) {
101-
cache[self._src] = buffer;
101+
cache[self._src as string] = buffer;
102102
loadSound(self, buffer);
103103
} else {
104104
error();
@@ -118,8 +118,8 @@ function decodeAudioData(arraybuffer: ArrayBuffer, self: Howl) {
118118

119119
/**
120120
* 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.
123123
*/
124124
function loadSound(self: Howl, buffer?: AudioBuffer) {
125125
// Set the duration.
@@ -140,6 +140,7 @@ function loadSound(self: Howl, buffer?: AudioBuffer) {
140140
}
141141
}
142142

143+
// NOTE: Maybe remove these
143144
export const isHTMLAudioElement = (node: any): node is HTMLAudioElement =>
144145
(node as HTMLAudioElement).playbackRate !== undefined;
145146

src/howl.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,12 @@ export interface SoundSpriteDefinitions {
99
[name: string]: [number, number] | [number, number, boolean];
1010
}
1111

12+
export interface HowlXHROptions {
13+
method?: string;
14+
headers?: Record<string, string>;
15+
withCredentials?: boolean;
16+
}
17+
1218
export interface HowlListeners {
1319
/**
1420
* Fires when the sound has been stopped. The first parameter is the ID of the sound.
@@ -174,11 +180,7 @@ export interface HowlOptions extends HowlListeners {
174180
* this parameter. Each is optional (method defaults to GET, headers default to undefined and
175181
* withCredentials defaults to false).
176182
*/
177-
xhr?: {
178-
method?: string;
179-
headers?: Record<string, string>;
180-
withCredentials?: boolean;
181-
};
183+
xhr?: HowlXHROptions;
182184
}
183185

184186
type HowlCallbacks = Array<{ fn: HowlCallback }>;

src/sound.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ class Sound {
3737

3838
/**
3939
* Setup the sound object, which each node attached to a Howl group is contained in.
40-
* @param {Object} howl The Howl parent group.
40+
* @param howl The Howl parent group.
4141
*/
4242
constructor(howl: Howl) {
4343
this._parent = howl;
@@ -73,7 +73,6 @@ class Sound {
7373

7474
/**
7575
* Create and setup a new sound object, whether HTML5 Audio or Web Audio.
76-
* @return {Sound}
7776
*/
7877
create() {
7978
var parent = this._parent;
@@ -116,7 +115,6 @@ class Sound {
116115

117116
/**
118117
* Reset the parameters of this sound to the original state (for recycle).
119-
* @return {Sound}
120118
*/
121119
reset() {
122120
var parent = this._parent;
@@ -159,7 +157,7 @@ class Sound {
159157
* HTML5 Audio canplaythrough listener callback.
160158
*/
161159
_loadListener() {
162-
var parent = this._parent;
160+
const parent = this._parent;
163161

164162
// Round up the duration to account for the lower precision in HTML5 Audio.
165163
parent._duration =
@@ -184,7 +182,7 @@ class Sound {
184182
* HTML5 Audio ended listener callback.
185183
*/
186184
_endListener() {
187-
var parent = this._parent;
185+
const parent = this._parent;
188186

189187
// Only handle the `ended`` event if the duration is Infinity.
190188
if (parent._duration === Infinity) {

0 commit comments

Comments
 (0)