-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathloader.js
54 lines (47 loc) · 1.13 KB
/
loader.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
/**
* Returns a LoaderBase abstract base class.
* @constructor
**/
function LoaderBase() {}
/**
* Read a ROM to memory.
**/
LoaderBase.prototype.read_rom = function() {
throw new Error('Method not implemented.');
}
/**
* Returns a new HTTPLoader instance.
* @constructor
**/
function HTTPLoader() {
LoaderBase.call(this);
}
HTTPLoader.prototype = LoaderBase.prototype;
HTTPLoader.prototype.constructor = HTTPLoader;
/**
* Get an array buffer of ROM data from the server.
**/
HTTPLoader.prototype.get = function(path, callback) {
var xhr = new XMLHttpRequest();
xhr.open('GET', path);
xhr.responseType = 'arraybuffer';
xhr.send();
xhr.onload = function(e) {
var arrayBuffer = xhr.response;
if (arrayBuffer) {
var byteArray = new Uint8Array(arrayBuffer);
callback(byteArray);
}
}
};
/**
* Download a ROM from the server.
**/
HTTPLoader.prototype.read_rom = function(rom_name, callback) {
/* Hit server for ROM data */
this.get('roms/' + rom_name + '/bin', function(rom_buffer) {
/* Call the next function in the init sequence */
callback(rom_buffer);
});
};
exports.HTTPLoader = HTTPLoader;