mirror of
https://github.com/yuzu-emu/liftinstall.git
synced 2024-12-23 05:25:27 +00:00
helper: clean up functions
This commit is contained in:
parent
4bb84d98b3
commit
8e212460d8
|
@ -4,60 +4,6 @@
|
||||||
* Additional state-less helper methods.
|
* Additional state-less helper methods.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
var request_id = 0
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Makes a AJAX request.
|
|
||||||
*
|
|
||||||
* @param path The path to connect to.
|
|
||||||
* @param successCallback A callback with a JSON payload.
|
|
||||||
* @param failCallback A fail callback. Optional.
|
|
||||||
* @param data POST data. Optional.
|
|
||||||
*/
|
|
||||||
export function ajax (path, successCallback, failCallback, data) {
|
|
||||||
if (failCallback === undefined) {
|
|
||||||
failCallback = defaultFailHandler
|
|
||||||
}
|
|
||||||
|
|
||||||
console.log('Making HTTP request to ' + path)
|
|
||||||
|
|
||||||
var req = new XMLHttpRequest()
|
|
||||||
|
|
||||||
req.addEventListener('load', function () {
|
|
||||||
// The server can sometimes return a string error. Make sure we handle this.
|
|
||||||
if (this.status === 200 && this.getResponseHeader('Content-Type').indexOf('application/json') !== -1) {
|
|
||||||
successCallback(JSON.parse(this.responseText))
|
|
||||||
} else {
|
|
||||||
failCallback(this.responseText)
|
|
||||||
}
|
|
||||||
})
|
|
||||||
req.addEventListener('error', failCallback)
|
|
||||||
|
|
||||||
req.open(data == null ? 'GET' : 'POST', path + '?nocache=' + request_id++, true)
|
|
||||||
// Rocket only currently supports URL encoded forms.
|
|
||||||
req.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded')
|
|
||||||
|
|
||||||
if (data != null) {
|
|
||||||
var form = ''
|
|
||||||
|
|
||||||
for (var key in data) {
|
|
||||||
if (!Object.prototype.hasOwnProperty.call(data, key)) {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
if (form !== '') {
|
|
||||||
form += '&'
|
|
||||||
}
|
|
||||||
|
|
||||||
form += encodeURIComponent(key) + '=' + encodeURIComponent(data[key])
|
|
||||||
}
|
|
||||||
|
|
||||||
req.send(form)
|
|
||||||
} else {
|
|
||||||
req.send()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Makes a AJAX request, streaming each line as it arrives. Type should be text/plain,
|
* Makes a AJAX request, streaming each line as it arrives. Type should be text/plain,
|
||||||
* each line will be interpreted as JSON separately.
|
* each line will be interpreted as JSON separately.
|
||||||
|
@ -108,7 +54,7 @@ export function stream_ajax (path, callback, successCallback, failCallback, data
|
||||||
|
|
||||||
req.addEventListener('error', failCallback)
|
req.addEventListener('error', failCallback)
|
||||||
|
|
||||||
req.open(data == null ? 'GET' : 'POST', path + '?nocache=' + request_id++, true)
|
req.open(data == null ? 'GET' : 'POST', path + '?nocache=' + Date.now(), true)
|
||||||
// Rocket only currently supports URL encoded forms.
|
// Rocket only currently supports URL encoded forms.
|
||||||
req.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded')
|
req.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded')
|
||||||
|
|
||||||
|
@ -132,13 +78,3 @@ export function stream_ajax (path, callback, successCallback, failCallback, data
|
||||||
req.send()
|
req.send()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* The default handler if a AJAX request fails. Not to be used directly.
|
|
||||||
*
|
|
||||||
* @param e The XMLHttpRequest that failed.
|
|
||||||
*/
|
|
||||||
function defaultFailHandler (e) {
|
|
||||||
console.error('A AJAX request failed, and was not caught:')
|
|
||||||
console.error(e)
|
|
||||||
}
|
|
||||||
|
|
Loading…
Reference in a new issue