webamp/js/file.js
Jordan Eldredge c864300b00 Store files as instanace objects
With files encapsulated as objects, we can store them, sort them, and pass them
around. This is laying a foundation for handling playlists. (#10)

Also, we move the modification of the loading state outside of the skin
manager. This creates a better separation of concerns.
2015-01-25 13:31:27 -08:00

43 lines
1.3 KiB
JavaScript

// Custom object representing a file
// `File` is already a builtin, so we use `MyFile`
MyFile = function(){
this.reader = new FileReader();
this.url = null;
this.fileReference = null;
this.setUrl = function(url){
this.url = url;
}
this.setFileReference = function(fileReference){
this.fileReference = fileReference;
}
this.processBuffer = function(bufferHandler) {
if(this.url) {
var oReq = new XMLHttpRequest();
oReq.open("GET", this.url, true);
oReq.responseType = "arraybuffer";
oReq.onload = function (oEvent) {
var arrayBuffer = oReq.response; // Note: not oReq.responseText
bufferHandler(arrayBuffer);
};
oReq.send(null);
return
} else if(this.fileReference) {
this.reader.onload = function (e) {
var arrayBuffer = e.target.result;
bufferHandler(arrayBuffer);
};
this.reader.onerror = function (e) {
console.error(e);
};
this.reader.readAsArrayBuffer(this.fileReference);
return
}
console.error('Tried to process an unpopulated file object');
return false;
}
};