update fetch progress

This commit is contained in:
casperlamboo 2018-01-22 18:12:33 +01:00
parent f28722aec5
commit 4dc5e4849e
2 changed files with 24 additions and 11 deletions

View File

@ -2,7 +2,7 @@ import * as THREE from 'three';
import _ from 'lodash';
import React from 'react';
import PropTypes from 'proptypes';
import { centerGeometry, placeOnGround, createScene, fetchProgress, slice, TabTemplate } from './utils.js';
import { centerGeometry, placeOnGround, createScene, slice, TabTemplate } from './utils.js';
import injectSheet from 'react-jss';
import RaisedButton from 'material-ui/RaisedButton';
import FlatButton from 'material-ui/FlatButton';

View File

@ -84,20 +84,33 @@ export function createScene({ pixelRatio, muiTheme }) {
return { editorControls, scene, mesh, camera, renderer, render, box, setSize, updateCanvas, focus };
}
export function fetchProgress(url, { method = 'get', headers = {}, body = {} } = {}, onProgress) {
export function fetchProgress(url, data = {}, onProgress) {
return new Promise((resolve, reject) => {
const request = new Request(url, data);
const xhr = new XMLHttpRequest();
xhr.open(method, url);
if (headers) {
for (const key in headers) {
const header = headers[key];
xhr.setRequestHeader(key, header);
}
xhr.onload = () => {
const { status, statusText, responseURL: url } = xhr;
resolve(new Response(xhr.response, { status, statusText, url }));
}
xhr.onerror = () => reject(new TypeError('Network request failed'));
xhr.ontimeout = () => reject(new TypeError('Network request failed'));
xhr.open(request.method, url);
if (request.credentials === 'include') {
xhr.withCredentials = true
} else if (request.credentials === 'omit') {
xhr.withCredentials = false
}
xhr.onload = event => resolve(event.target.responseText);
xhr.onerror = reject;
if (xhr.upload && onProgress) xhr.upload.onprogress = onProgress;
xhr.send(body);
if (xhr.responseType) xhr.responseType = 'blob';
request.headers.forEach((value, name) => {
xhr.setRequestHeader(name, value)
});
xhr.send(data.body);
});
}