From 4dc5e4849e35f8d97e157c2020ee351427d598d4 Mon Sep 17 00:00:00 2001 From: casperlamboo Date: Mon, 22 Jan 2018 18:12:33 +0100 Subject: [PATCH] update fetch progress --- src/interface/index.js | 2 +- src/interface/utils.js | 33 +++++++++++++++++++++++---------- 2 files changed, 24 insertions(+), 11 deletions(-) diff --git a/src/interface/index.js b/src/interface/index.js index 10b94ff..47f8bff 100644 --- a/src/interface/index.js +++ b/src/interface/index.js @@ -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'; diff --git a/src/interface/utils.js b/src/interface/utils.js index 4f2b811..3dd601e 100644 --- a/src/interface/utils.js +++ b/src/interface/utils.js @@ -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); }); }