Switch to the Fetch API
@@ -91,14 +91,16 @@ class AppController {
pointSize: FONT_SIZE,
};
- const xhr = new XMLHttpRequest;
- xhr.addEventListener('load', () => {
- this.meshes = new PathfinderMeshes(xhr.responseText);
+ window.fetch(PARTITION_FONT_ENDPOINT_URL, {
+ method: 'POST',
+ headers: { 'Content-Type': 'application/json' },
+ body: JSON.stringify(request),
+ }).then((response) => {
+ response.text().then((encodedMeshes) => {
+ this.meshes = new PathfinderMeshes(encodedMeshes);
this.meshesReceived();
- }, false);
- xhr.open('POST', PARTITION_FONT_ENDPOINT_URL, true);
- xhr.setRequestHeader('Content-Type', 'application/json');
- xhr.send(JSON.stringify(request));
+ });
+ });
}
meshesReceived() {
@@ -134,12 +136,20 @@ class PathfinderView {
}
loadShaders(): Promise<ShaderMap> {
- return new Promise((resolve, reject) => {
let shaders: ShaderMap = {};
const shaderKeys = Object.keys(SHADER_URLS);
- let shaderKeysLeft = shaderKeys.length;
- let loaded = (type: ShaderType, shaderKey: string, source: string) => {
+ let promises = [];
+ for (const shaderKey of shaderKeys) {
+ for (const typeName of ['vertex', 'fragment'] as Array<ShaderTypeName>) {
+ const type = {
+ vertex: this.gl.VERTEX_SHADER,
+ fragment: this.gl.FRAGMENT_SHADER,
+ }[typeName];
+
+ const url = SHADER_URLS[shaderKey][typeName];
+ promises.push(window.fetch(url).then((response) => {
+ return response.text().then((source) => {
const shader = this.gl.createShader(type);
if (shader == null)
throw new Error("Failed to create shader!");
@@ -148,29 +158,12 @@ class PathfinderView {
if (!(shaderKey in shaders))
shaders[shaderKey] = {};
shaders[shaderKey][type] = shader;
-
- shaderKeysLeft--;
- if (shaderKeysLeft == 0)
- resolve(shaders);
- };
-
- for (const shaderKey of shaderKeys) {
- for (const typeName of ['vertex', 'fragment'] as Array<ShaderTypeName>) {
- const type = {
- vertex: this.gl.VERTEX_SHADER,
- fragment: this.gl.FRAGMENT_SHADER,
- }[typeName];
-
- const xhr = new XMLHttpRequest;
- xhr.addEventListener('load',
- () => loaded(type, shaderKey, xhr.responseText),
- false);
- xhr.addEventListener('error', () => reject(), false);
- xhr.open('GET', SHADER_URLS[shaderKey][typeName], true);
- xhr.send();
+ });
+ }));
}
}
- });
+
+ return Promise.all(promises).then(() => shaders);
}
linkShaders(shaders: ShaderMap): Promise<ShaderProgramMap> {
