Skip to content

Commit 39ed0fb

Browse files
committed
update util.request
1 parent b640d4d commit 39ed0fb

File tree

3 files changed

+88
-59
lines changed

3 files changed

+88
-59
lines changed

js/midi/loader.js

Lines changed: 10 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -134,14 +134,15 @@ MIDI.Player = MIDI.Player || {};
134134
var instruments = opts.instruments;
135135
var onprogress = opts.onprogress;
136136
var onerror = opts.onerror;
137-
var onload = opts.callback;
137+
var onsuccess = opts.onsuccess;
138138
///
139139
var length = instruments.length;
140140
var pending = length;
141141
var waitForEnd = function() {
142142
if (!--pending) {
143143
onprogress && onprogress('load', 1.0);
144144
root[context].connect(opts);
145+
onsuccess && onsuccess();
145146
}
146147
};
147148
///
@@ -150,7 +151,7 @@ MIDI.Player = MIDI.Player || {};
150151
if (MIDI.Soundfont[instrumentId]) { // already loaded
151152
waitForEnd();
152153
} else { // needs to be requested
153-
sendRequest(instruments[i], audioFormat, function(fpoint) {
154+
sendRequest(instruments[i], audioFormat, function(evt, progress) {
154155
onprogress && onprogress('load', fpoint + (pending - 1) / length, instrumentId);
155156
}, function() {
156157
waitForEnd();
@@ -159,34 +160,22 @@ MIDI.Player = MIDI.Player || {};
159160
};
160161
};
161162

162-
var sendRequest = function(instrumentId, audioFormat, onprogress, onload, onerror) {
163+
var sendRequest = function(instrumentId, audioFormat, onprogress, onsuccess, onerror) {
163164
var soundfontPath = root.soundfontUrl + instrumentId + '-' + audioFormat + '.js';
164165
if (root.USE_XHR) {
165166
root.util.request({
166167
url: soundfontPath,
168+
format: 'text',
167169
onerror: onerror,
168-
onprogress: function(event) {
169-
if (!this.totalBytes) { // requires server to send Content-Length-Raw - actual bytes non-gzipped
170-
var rawLength = this.getResponseHeader('Content-Length-Raw');
171-
if (rawLength) {
172-
this.totalBytes = parseInt(rawLength);
173-
} else {
174-
this.totalBytes = event.total;
175-
}
176-
}
177-
///
178-
if (this.totalBytes) {
179-
onprogress(event.loaded / this.totalBytes);
180-
}
181-
},
182-
onload: function(response) {
170+
onprogress: onprogress,
171+
onsuccess: function(event, responseText) {
183172
var script = document.createElement('script');
184173
script.language = 'javascript';
185174
script.type = 'text/javascript';
186-
script.text = response.responseText;
175+
script.text = responseText;
187176
document.body.appendChild(script);
188177
///
189-
onload();
178+
onsuccess();
190179
}
191180
});
192181
} else {
@@ -195,7 +184,7 @@ MIDI.Player = MIDI.Player || {};
195184
verify: 'MIDI.Soundfont["' + instrumentId + '"]',
196185
onerror: onerror,
197186
callback: function() {
198-
onload();
187+
onsuccess();
199188
}
200189
});
201190
}

js/midi/plugin.webaudio.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,7 @@
257257
///
258258
if (-- bufferPending[instrumentId] === 0) {
259259
var percent = index / 87;
260-
console.log(MIDI.GM.byId[instrumentId], 'processing: ', percent);
260+
// console.log(MIDI.GM.byId[instrumentId], 'processing: ', percent);
261261
soundfont.isLoaded = true;
262262
waitForEnd(instrument);
263263
}

js/util/dom_request_xhr.js

Lines changed: 77 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,23 @@
11
/*
22
----------------------------------------------------------
3-
util/Request : 0.1.1 : 2014-10-17
3+
util/Request : 0.1.1 : 2015-03-26
44
----------------------------------------------------------
55
util.request({
66
url: './dir/something.extension',
7-
body: 'test!',
8-
onerror: function(event) {
9-
console.log(event);
7+
data: 'test!',
8+
format: 'text', // text | xml | json | binary
9+
responseType: 'text', // arraybuffer | blob | document | json | text
10+
headers: {},
11+
withCredentials: true, // true | false
12+
///
13+
onerror: function(evt, percent) {
14+
console.log(evt);
1015
},
11-
onload: function(response) {
12-
console.log(response.responseText);
16+
onsuccess: function(evt, responseText) {
17+
console.log(responseText);
1318
},
14-
onprogress: function(event) {
15-
var percent = event.loaded / event.total * 100 >> 0;
19+
onprogress: function(evt, percent) {
20+
percent = Math.round(percent * 100);
1621
loader.create('thread', 'loading... ', percent);
1722
}
1823
});
@@ -24,41 +29,43 @@ if (typeof MIDI === 'undefined') MIDI = {};
2429

2530
var util = root.util || (root.util = {});
2631

27-
util.request = function(params, onload, onerror, onprogress) { 'use strict';
28-
if (typeof(params) === 'string') params = {url: params};
32+
util.request = function(opts, onsuccess, onerror, onprogress) { 'use strict';
33+
if (typeof opts === 'string') opts = {url: opts};
34+
///
35+
var data = opts.data;
36+
var url = opts.url;
37+
var method = opts.method || (opts.data ? 'POST' : 'GET');
38+
var format = opts.format;
39+
var headers = opts.headers;
40+
var responseType = opts.responseType;
41+
var withCredentials = opts.withCredentials || false;
2942
///
30-
var body = params.body;
31-
var url = params.url;
32-
var headers = params.headers;
33-
var responseType = params.responseType;
34-
var withCredentials = params.withCredentials;
35-
var asBinaryString = params.asBinaryString;
36-
var onload = onload || params.onload;
37-
var onerror = onerror || params.onerror;
38-
var onprogress = onprogress || params.onprogress;
43+
var onsuccess = onsuccess || opts.onsuccess;
44+
var onerror = onerror || opts.onerror;
45+
var onprogress = onprogress || opts.onprogress;
3946
///
40-
if (typeof(NodeFS) !== 'undefined' && root.loc.isLocalUrl(url)) {
41-
NodeFS.readFile(url, 'utf8', function(error, data) {
42-
if (error) {
43-
onerror && onerror(error, false);
47+
if (typeof NodeFS !== 'undefined' && root.loc.isLocalUrl(url)) {
48+
NodeFS.readFile(url, 'utf8', function(err, res) {
49+
if (err) {
50+
onerror && onerror(err);
4451
} else {
45-
onload && onload({responseText: data});
52+
onsuccess && onsuccess({responseText: res});
4653
}
4754
});
4855
return;
4956
}
5057
///
5158
var xhr = new XMLHttpRequest();
52-
xhr.open(body ? 'POST' : 'GET', url, true);
59+
xhr.open(method, url, true);
5360
///
5461
if (headers) {
55-
for (var key in headers) {
56-
xhr.setRequestHeader(key, headers[key]);
62+
for (var type in headers) {
63+
xhr.setRequestHeader(type, headers[type]);
5764
}
58-
} else if (body) { // set the default headers for POST
65+
} else if (data) { // set the default headers for POST
5966
xhr.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
6067
}
61-
if (asBinaryString) { //- default to responseType="blob" when supported
68+
if (format === 'binary') { //- default to responseType="blob" when supported
6269
if (xhr.overrideMimeType) {
6370
xhr.overrideMimeType('text/plain; charset=x-user-defined');
6471
}
@@ -73,26 +80,59 @@ if (typeof MIDI === 'undefined') MIDI = {};
7380
xhr.onerror = onerror;
7481
}
7582
if (onprogress && xhr.upload && 'onprogress' in xhr.upload) {
76-
if (body) {
77-
xhr.upload.onprogress = onprogress;
83+
if (data) {
84+
xhr.upload.onprogress = function(evt) {
85+
onprogress.call(xhr, evt, event.loaded / event.total);
86+
};
7887
} else {
79-
xhr.onprogress = onprogress;
88+
xhr.addEventListener('progress', function(evt) {
89+
var totalBytes = 0;
90+
if (evt.lengthComputable) {
91+
totalBytes = evt.total;
92+
} else if (xhr.totalBytes) {
93+
totalBytes = xhr.totalBytes;
94+
} else {
95+
var rawBytes = parseInt(xhr.getResponseHeader('Content-Length-Raw'));
96+
if (isFinite(rawBytes)) {
97+
xhr.totalBytes = totalBytes = rawBytes;
98+
} else {
99+
return;
100+
}
101+
}
102+
onprogress.call(xhr, evt, evt.loaded / totalBytes);
103+
});
80104
}
81105
}
82106
///
83-
xhr.onreadystatechange = function(event) {
107+
xhr.onreadystatechange = function(evt) {
84108
if (xhr.readyState === 4) { // The request is complete
85109
if (xhr.status === 200 || // Response OK
86110
xhr.status === 304 || // Not Modified
87-
xhr.status === 0 && root.client && root.client.cordova // Cordova quirk
111+
xhr.status === 308 || // Permanent Redirect
112+
xhr.status === 0 && root.client.cordova // Cordova quirk
88113
) {
89-
onload && onload(xhr);
114+
if (onsuccess) {
115+
var res;
116+
if (format === 'xml') {
117+
res = evt.target.responseXML;
118+
} else if (format === 'text') {
119+
res = evt.target.responseText;
120+
} else if (format === 'json') {
121+
try {
122+
res = JSON.parse(evt.target.response);
123+
} catch(err) {
124+
onerror && onerror.call(xhr, evt);
125+
}
126+
}
127+
///
128+
onsuccess.call(xhr, evt, res);
129+
}
90130
} else {
91-
onerror && onerror(event, false);
131+
onerror && onerror.call(xhr, evt);
92132
}
93133
}
94134
};
95-
xhr.send(body);
135+
xhr.send(data);
96136
return xhr;
97137
};
98138

0 commit comments

Comments
 (0)