Browse Source

Convert the `DOMCMapReaderFactory` to an ES6 class

Given that we only create *one* instance of this class per `getDocument` call, this shouldn't matter performance wise.
Jonas Jenwald 8 years ago
parent
commit
32baa6af7a
  1. 85
      src/display/dom_utils.js
  2. 13
      test/unit/test_utils.js

85
src/display/dom_utils.js

@ -52,59 +52,54 @@ DOMCanvasFactory.prototype = {
} }
}; };
var DOMCMapReaderFactory = (function DOMCMapReaderFactoryClosure() { class DOMCMapReaderFactory {
function DOMCMapReaderFactory(params) { constructor({ baseUrl = null, isCompressed = false, }) {
this.baseUrl = params.baseUrl || null; this.baseUrl = baseUrl;
this.isCompressed = params.isCompressed || false; this.isCompressed = isCompressed;
} }
DOMCMapReaderFactory.prototype = { fetch({ name, }) {
fetch(params) { if (!name) {
var name = params.name; return Promise.reject(new Error('CMap name must be specified.'));
if (!name) { }
return Promise.reject(new Error('CMap name must be specified.')); return new Promise((resolve, reject) => {
} let url = this.baseUrl + name + (this.isCompressed ? '.bcmap' : '');
return new Promise((resolve, reject) => {
var url = this.baseUrl + name + (this.isCompressed ? '.bcmap' : '');
var request = new XMLHttpRequest(); let request = new XMLHttpRequest();
request.open('GET', url, true); request.open('GET', url, true);
if (this.isCompressed) { if (this.isCompressed) {
request.responseType = 'arraybuffer'; request.responseType = 'arraybuffer';
}
request.onreadystatechange = () => {
if (request.readyState !== XMLHttpRequest.DONE) {
return;
} }
request.onreadystatechange = () => { if (request.status === 200 || request.status === 0) {
if (request.readyState !== XMLHttpRequest.DONE) { let data;
return; if (this.isCompressed && request.response) {
data = new Uint8Array(request.response);
} else if (!this.isCompressed && request.responseText) {
data = stringToBytes(request.responseText);
} }
if (request.status === 200 || request.status === 0) { if (data) {
var data; resolve({
if (this.isCompressed && request.response) { cMapData: data,
data = new Uint8Array(request.response); compressionType: this.isCompressed ?
} else if (!this.isCompressed && request.responseText) { CMapCompressionType.BINARY : CMapCompressionType.NONE,
data = stringToBytes(request.responseText); });
} return;
if (data) {
resolve({
cMapData: data,
compressionType: this.isCompressed ?
CMapCompressionType.BINARY : CMapCompressionType.NONE,
});
return;
}
} }
reject(new Error('Unable to load ' + }
(this.isCompressed ? 'binary ' : '') + reject(new Error('Unable to load ' +
'CMap at: ' + url)); (this.isCompressed ? 'binary ' : '') +
}; 'CMap at: ' + url));
};
request.send(null);
});
},
};
return DOMCMapReaderFactory; request.send(null);
})(); });
}
}
/** /**
* Optimised CSS custom property getter/setter. * Optimised CSS custom property getter/setter.

13
test/unit/test_utils.js

@ -16,20 +16,19 @@
import { CMapCompressionType } from '../../src/shared/util'; import { CMapCompressionType } from '../../src/shared/util';
class NodeCMapReaderFactory { class NodeCMapReaderFactory {
constructor(params) { constructor({ baseUrl = null, isCompressed = false, }) {
this.baseUrl = params.baseUrl || null; this.baseUrl = baseUrl;
this.isCompressed = params.isCompressed || false; this.isCompressed = isCompressed;
} }
fetch(params) { fetch({ name, }) {
var name = params.name;
if (!name) { if (!name) {
return Promise.reject(new Error('CMap name must be specified.')); return Promise.reject(new Error('CMap name must be specified.'));
} }
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
var url = this.baseUrl + name + (this.isCompressed ? '.bcmap' : ''); let url = this.baseUrl + name + (this.isCompressed ? '.bcmap' : '');
var fs = require('fs'); let fs = require('fs');
fs.readFile(url, (error, data) => { fs.readFile(url, (error, data) => {
if (error || !data) { if (error || !data) {
reject(new Error('Unable to load ' + reject(new Error('Unable to load ' +

Loading…
Cancel
Save