-
Notifications
You must be signed in to change notification settings - Fork 186
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
15 changed files
with
201 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
# fetchProgress | ||
|
||
> This function is still experimental | ||
A function that tracks a fetch response object and calls `onProgress` callbacks. | ||
|
||
## Usage | ||
|
||
```js | ||
import {_fetchProgress} from '@loaders.gl/core'; | ||
|
||
function onProgress(percent, {loadedBytes, totalBytes}) { | ||
console.log(`${percent}% ${Math.round(loadedBytes/1000)} of ${Math.round(totalBytes/1000)} Kbytes`); | ||
} | ||
|
||
async function main() { | ||
const response = await _fetchProgress(fetch(PROGRESS_IMAGE_URL, onProgress), | ||
const data = await response.arrayBuffer(); | ||
// At this point, onProgress will have been called one or more times. | ||
... | ||
} | ||
``` | ||
## _fetchProgress(response : Response | Promise, onProgress : function, onDone : function, onError : function) : Response | ||
`onProgress: (percent: number, {loadedBytes : number, totalBytes : number}) => void` | ||
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
// Forked from github AnthumChris/fetch-progress-indicators under MIT license | ||
/* global fetch, document, URL */ | ||
import {_fetchProgress} from '@loaders.gl/core'; | ||
|
||
const PROGRESS_IMAGE_URL = 'https://fetch-progress.anthum.com/30kbps/images/sunrise-baseline.jpg'; | ||
// 'https://raw.githubusercontent.com/uber-web/loaders.gl/master/modules/images/test/progress/sunrise-baseline.jpg'; | ||
// 'https://raw.githubusercontent.com/uber-web/loaders.gl/master/modules/images/test/data/img1-preview.jpeg'; | ||
|
||
const elStatus = document.getElementById('status'); | ||
const elProgress = document.getElementById('progress'); | ||
const elImage = document.getElementById('img'); | ||
|
||
async function main() { | ||
elStatus.innerHTML = 'Downloading with fetch()...'; | ||
|
||
try { | ||
const response = await _fetchProgress( | ||
fetch(PROGRESS_IMAGE_URL), | ||
(percent, {loadedBytes, totalBytes}) => { | ||
elProgress.innerHTML = `${percent}% ${Math.round(loadedBytes / 1000)} of ${Math.round( | ||
totalBytes / 1000 | ||
)} Kbytes`; | ||
} | ||
); | ||
const data = await response.blob(); | ||
elStatus.innerHTML = 'download completed'; | ||
elImage.src = URL.createObjectURL(data); | ||
} catch (error) { | ||
console.error(error); // eslint-disable-line | ||
elStatus.innerHTML = error; | ||
} | ||
} | ||
|
||
main(); | ||
|
||
/* | ||
unction progress(percent {loadedBytes, totalBytes}) { | ||
elProgress.innerHTML = Math.round(loadedBytes/totalBytes*100)+'%'; | ||
*/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<!doctype html> | ||
<script type="module"> | ||
import './app.js'; | ||
</script> | ||
<body> | ||
<main> | ||
<!-- Forked from github AnthumChris/fetch-progress-indicators under MIT license --> | ||
<div id="status"> </div> | ||
<h1 id="progress"> </h1> | ||
<div><button onclick="window.location.reload()">Reload</button></div> | ||
<img id="img" /> | ||
</main> | ||
</body> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
{ | ||
"license": "MIT", | ||
"description": "Minimal example of using loaders.gl with webpack.", | ||
"scripts": { | ||
"start": "webpack-dev-server --progress --hot --open -d", | ||
"start-local": "webpack-dev-server --env.local --progress --hot --open" | ||
}, | ||
"dependencies": { | ||
"@loaders.gl/core": "^1.2.2", | ||
"@loaders.gl/obj": "^1.2.2" | ||
}, | ||
"devDependencies": { | ||
"html-webpack-plugin": "^3.2.0", | ||
"webpack": "^4.3.0", | ||
"webpack-dev-server": "^3.1.1" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
const {resolve} = require('path'); | ||
|
||
const CONFIG = { | ||
mode: 'development', | ||
|
||
entry: { | ||
app: resolve('./app.js') | ||
}, | ||
|
||
node: { | ||
fs: 'empty' | ||
} | ||
}; | ||
|
||
// This line enables bundling against src in this repo rather than installed module | ||
module.exports = env => (env ? require('../webpack.config.local')(CONFIG)(env) : CONFIG); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
// Forked from github AnthumChris/fetch-progress-indicators under MIT license | ||
/* global Response, ReadableStream */ | ||
|
||
// Intercepts the Response stream and creates a new Response | ||
export default async function fetchProgress( | ||
response, | ||
onProgress, | ||
onDone = () => {}, | ||
onError = () => {} | ||
) { | ||
response = await response; | ||
if (!response.ok) { | ||
// ERROR checking needs to be done separately | ||
return response; | ||
} | ||
if (!response.body) { | ||
// 'ReadableStream not yet supported in this browser. | ||
return response; | ||
} | ||
const contentLength = response.headers.get('content-length'); | ||
const totalBytes = contentLength && parseInt(contentLength, 10); | ||
if (!(contentLength > 0)) { | ||
return response; | ||
} | ||
// Currently override only implemented in browser | ||
if (typeof ReadableStream === 'undefined') { | ||
return response; | ||
} | ||
|
||
// Create a new stream that invisbly wraps original stream | ||
const progressStream = new ReadableStream({ | ||
start(controller) { | ||
const reader = response.body.getReader(); | ||
read(controller, reader, 0, totalBytes, onProgress, onDone, onError); | ||
} | ||
}); | ||
|
||
return new Response(progressStream); | ||
} | ||
|
||
// Forward to original streams controller | ||
// TODO - this causes a crazy deep "async stack"... rewrite as async iterator? | ||
// eslint-disable-next-line max-params | ||
async function read(controller, reader, loadedBytes, totalBytes, onProgress, onDone, onError) { | ||
try { | ||
const {done, value} = await reader.read(); | ||
if (done) { | ||
onDone(); | ||
controller.close(); | ||
return; | ||
} | ||
loadedBytes += value.byteLength; | ||
const percent = Math.round((loadedBytes / totalBytes) * 100); | ||
onProgress(percent, {loadedBytes, totalBytes}); | ||
controller.enqueue(value); | ||
await read(controller, reader, loadedBytes, totalBytes, onProgress, onDone, onError); | ||
} catch (error) { | ||
controller.error(error); | ||
onError(error); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import test from 'tape-promise/tape'; | ||
import {fetchFile, _fetchProgress} from '@loaders.gl/core'; | ||
|
||
const PROGRESS_IMAGE_URL = '@loaders.gl/images/test/data/img1-preview.jpeg'; | ||
|
||
test('progress#fetchProgress', async t => { | ||
t.ok(_fetchProgress, '_fetchProgress defined'); | ||
const response = await _fetchProgress( | ||
fetchFile(PROGRESS_IMAGE_URL), | ||
(percent, {loadedBytes, totalBytes}) => { | ||
t.ok(Number.isFinite(percent)); | ||
t.ok(Number.isFinite(loadedBytes)); | ||
t.ok(Number.isFinite(totalBytes)); | ||
t.end(); | ||
} | ||
); | ||
await response.arrayBuffer(); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
# Data Licenses | ||
|
||
- progress folder images, copied under MIT license from AnthumChris/fetch-progress-indicators. |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters