Spaces:
Running
Running
Update src/components/loader/tw-progress-monitor.js
Browse files
src/components/loader/tw-progress-monitor.js
CHANGED
@@ -1,34 +1,39 @@
|
|
1 |
-
|
|
|
|
|
2 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
3 |
let total = 0;
|
4 |
let complete = 0;
|
5 |
-
|
6 |
-
// 0 - none
|
7 |
-
// 1 - load json
|
8 |
-
// 2 - load assets
|
9 |
let state = 0;
|
10 |
-
|
11 |
let currentProgress = 0;
|
12 |
-
|
13 |
let progressHandler = (state, progress, complete, total) => {};
|
14 |
|
|
|
15 |
export const setProgressHandler = newHandler => {
|
16 |
progressHandler = newHandler;
|
17 |
progressHandler(state, currentProgress, complete, total);
|
18 |
};
|
19 |
|
20 |
-
|
21 |
-
const fireProgressHandler = () => {
|
22 |
-
progressHandler(state, currentProgress, complete, total);
|
23 |
-
progressHandlerTimeout = null;
|
24 |
-
};
|
25 |
-
|
26 |
const queueProgressHandlerUpdate = () => {
|
27 |
if (progressHandlerTimeout === null) {
|
28 |
progressHandlerTimeout = requestAnimationFrame(fireProgressHandler);
|
29 |
}
|
30 |
};
|
31 |
|
|
|
32 |
const setProgress = progress => {
|
33 |
if (progress < 0) {
|
34 |
progress = 0;
|
@@ -40,6 +45,7 @@ const setProgress = progress => {
|
|
40 |
queueProgressHandlerUpdate();
|
41 |
};
|
42 |
|
|
|
43 |
const setState = newState => {
|
44 |
if (state === newState) {
|
45 |
return;
|
@@ -50,11 +56,11 @@ const setState = newState => {
|
|
50 |
setProgress(0);
|
51 |
};
|
52 |
|
53 |
-
|
|
|
54 |
setState(1);
|
55 |
-
const proxyUrl =
|
56 |
return new Promise((resolve, reject) => {
|
57 |
-
// XMLHttpRequestの進行状況処理
|
58 |
const xhr = new XMLHttpRequest();
|
59 |
xhr.responseType = 'blob';
|
60 |
xhr.onload = () => {
|
|
|
1 |
+
// 必要なモジュールをインポート
|
2 |
+
const corsAnywhere = require('cors-anywhere');
|
3 |
+
const XMLHttpRequest = require('xmlhttprequest').XMLHttpRequest;
|
4 |
|
5 |
+
// CORSプロキシサーバーの設定
|
6 |
+
const host = '127.0.0.1';
|
7 |
+
const port = 8080;
|
8 |
+
|
9 |
+
corsAnywhere.createServer({
|
10 |
+
originWhitelist: [], // すべてのオリジンを許可
|
11 |
+
requireHeaders: [],
|
12 |
+
}).listen(port, host, () => {
|
13 |
+
console.log(`CORS Anywhere server is running on ${host}:${port}`);
|
14 |
+
});
|
15 |
+
|
16 |
+
// グローバルな進行状況管理変数
|
17 |
let total = 0;
|
18 |
let complete = 0;
|
|
|
|
|
|
|
|
|
19 |
let state = 0;
|
|
|
20 |
let currentProgress = 0;
|
|
|
21 |
let progressHandler = (state, progress, complete, total) => {};
|
22 |
|
23 |
+
// 進行状況ハンドラを設定する関数
|
24 |
export const setProgressHandler = newHandler => {
|
25 |
progressHandler = newHandler;
|
26 |
progressHandler(state, currentProgress, complete, total);
|
27 |
};
|
28 |
|
29 |
+
// 進行状況更新をキューに入れる関数
|
|
|
|
|
|
|
|
|
|
|
30 |
const queueProgressHandlerUpdate = () => {
|
31 |
if (progressHandlerTimeout === null) {
|
32 |
progressHandlerTimeout = requestAnimationFrame(fireProgressHandler);
|
33 |
}
|
34 |
};
|
35 |
|
36 |
+
// 進行状況を更新する関数
|
37 |
const setProgress = progress => {
|
38 |
if (progress < 0) {
|
39 |
progress = 0;
|
|
|
45 |
queueProgressHandlerUpdate();
|
46 |
};
|
47 |
|
48 |
+
// ステータスを設定する関数
|
49 |
const setState = newState => {
|
50 |
if (state === newState) {
|
51 |
return;
|
|
|
56 |
setProgress(0);
|
57 |
};
|
58 |
|
59 |
+
// XMLHttpRequestで進行状況を監視してリソースを取得する関数
|
60 |
+
const fetchWithProgress = url => {
|
61 |
setState(1);
|
62 |
+
const proxyUrl = `http://127.0.0.1:8080/` + encodeURIComponent(url); // ローカルのCORSプロキシを使用
|
63 |
return new Promise((resolve, reject) => {
|
|
|
64 |
const xhr = new XMLHttpRequest();
|
65 |
xhr.responseType = 'blob';
|
66 |
xhr.onload = () => {
|