Spaces:
Running
Running
Update src/components/loader/tw-progress-monitor.js
Browse files
src/components/loader/tw-progress-monitor.js
CHANGED
@@ -1,25 +1,32 @@
|
|
1 |
-
|
2 |
-
// グローバルな進行状況管理変数
|
3 |
let total = 0;
|
4 |
let complete = 0;
|
|
|
|
|
|
|
|
|
5 |
let state = 0;
|
|
|
6 |
let currentProgress = 0;
|
|
|
7 |
let progressHandler = (state, progress, complete, total) => {};
|
8 |
|
9 |
-
// 進行状況ハンドラを設定する関数
|
10 |
export const setProgressHandler = newHandler => {
|
11 |
progressHandler = newHandler;
|
12 |
progressHandler(state, currentProgress, complete, total);
|
13 |
};
|
14 |
|
15 |
-
|
|
|
|
|
|
|
|
|
|
|
16 |
const queueProgressHandlerUpdate = () => {
|
17 |
if (progressHandlerTimeout === null) {
|
18 |
progressHandlerTimeout = requestAnimationFrame(fireProgressHandler);
|
19 |
}
|
20 |
};
|
21 |
|
22 |
-
// 進行状況を更新する関数
|
23 |
const setProgress = progress => {
|
24 |
if (progress < 0) {
|
25 |
progress = 0;
|
@@ -31,7 +38,6 @@ const setProgress = progress => {
|
|
31 |
queueProgressHandlerUpdate();
|
32 |
};
|
33 |
|
34 |
-
// ステータスを設定する関数
|
35 |
const setState = newState => {
|
36 |
if (state === newState) {
|
37 |
return;
|
@@ -42,16 +48,16 @@ const setState = newState => {
|
|
42 |
setProgress(0);
|
43 |
};
|
44 |
|
45 |
-
|
46 |
-
|
|
|
|
|
|
|
|
|
|
|
47 |
setState(1);
|
48 |
-
|
49 |
-
// 使用するCORSプロキシURL
|
50 |
-
const proxyUrl = `https://public-soiz1-cors-proxy.hf.space/?url=` + encodeURIComponent(url); // ここで公開されているプロキシを使用
|
51 |
-
//const proxyUrl = `https://cors-anywhere.herokuapp.com/` + encodeURIComponent(url);
|
52 |
-
// const proxyUrl = `https://cors-proxy.htmldriven.com/?url=` + encodeURIComponent(url); // 別のプロキシURLを使いたい場合はこちらを使用
|
53 |
-
// const proxyUrl = `https://api.allorigins.win/raw?url=` + encodeURIComponent(url); // 別のプロキシURLを使いたい場合はこちらを使用
|
54 |
return new Promise((resolve, reject) => {
|
|
|
55 |
const xhr = new XMLHttpRequest();
|
56 |
xhr.responseType = 'blob';
|
57 |
xhr.onload = () => {
|
@@ -61,13 +67,13 @@ const fetchWithProgress = url => {
|
|
61 |
}));
|
62 |
};
|
63 |
xhr.onloadend = () => setProgress(1);
|
64 |
-
xhr.onerror = () => reject(new Error('[tw-progress-monitor] xhr failed with status
|
65 |
xhr.onprogress = e => {
|
66 |
if (e.lengthComputable) {
|
67 |
setProgress(e.loaded / e.total);
|
68 |
}
|
69 |
};
|
70 |
-
xhr.open('GET', proxyUrl);
|
71 |
xhr.send();
|
72 |
});
|
73 |
};
|
@@ -83,7 +89,6 @@ window.fetch = (url, opts) => {
|
|
83 |
return originalFetch(url, opts);
|
84 |
};
|
85 |
|
86 |
-
|
87 |
// Web Workerのメッセージ処理
|
88 |
const handleWorkerMessage = e => {
|
89 |
const data = e.data;
|
|
|
|
|
|
|
1 |
let total = 0;
|
2 |
let complete = 0;
|
3 |
+
|
4 |
+
// 0 - none
|
5 |
+
// 1 - load json
|
6 |
+
// 2 - load assets
|
7 |
let state = 0;
|
8 |
+
|
9 |
let currentProgress = 0;
|
10 |
+
|
11 |
let progressHandler = (state, progress, complete, total) => {};
|
12 |
|
|
|
13 |
export const setProgressHandler = newHandler => {
|
14 |
progressHandler = newHandler;
|
15 |
progressHandler(state, currentProgress, complete, total);
|
16 |
};
|
17 |
|
18 |
+
let progressHandlerTimeout = null;
|
19 |
+
const fireProgressHandler = () => {
|
20 |
+
progressHandler(state, currentProgress, complete, total);
|
21 |
+
progressHandlerTimeout = null;
|
22 |
+
};
|
23 |
+
|
24 |
const queueProgressHandlerUpdate = () => {
|
25 |
if (progressHandlerTimeout === null) {
|
26 |
progressHandlerTimeout = requestAnimationFrame(fireProgressHandler);
|
27 |
}
|
28 |
};
|
29 |
|
|
|
30 |
const setProgress = progress => {
|
31 |
if (progress < 0) {
|
32 |
progress = 0;
|
|
|
38 |
queueProgressHandlerUpdate();
|
39 |
};
|
40 |
|
|
|
41 |
const setState = newState => {
|
42 |
if (state === newState) {
|
43 |
return;
|
|
|
48 |
setProgress(0);
|
49 |
};
|
50 |
|
51 |
+
export const fetchWithProgress = url => {
|
52 |
+
// プロキシURLを指定
|
53 |
+
const proxyUrl = `https://public-soiz1-cors-proxy.hf.space/?url=` + encodeURIComponent(url); // ここで公開されているプロキシを使用
|
54 |
+
// const proxyUrl = `https://cors-anywhere.herokuapp.com/` + encodeURIComponent(url); // こちらを使いたい場合
|
55 |
+
// const proxyUrl = `https://cors-proxy.htmldriven.com/?url=` + encodeURIComponent(url); // 別のプロキシURL
|
56 |
+
// const proxyUrl = `https://api.allorigins.win/raw?url=` + encodeURIComponent(url); // 別のプロキシURL
|
57 |
+
|
58 |
setState(1);
|
|
|
|
|
|
|
|
|
|
|
|
|
59 |
return new Promise((resolve, reject) => {
|
60 |
+
// XMLHttpRequestの進行状況処理
|
61 |
const xhr = new XMLHttpRequest();
|
62 |
xhr.responseType = 'blob';
|
63 |
xhr.onload = () => {
|
|
|
67 |
}));
|
68 |
};
|
69 |
xhr.onloadend = () => setProgress(1);
|
70 |
+
xhr.onerror = () => reject(new Error('[tw-progress-monitor] xhr failed with status' + xhr.status));
|
71 |
xhr.onprogress = e => {
|
72 |
if (e.lengthComputable) {
|
73 |
setProgress(e.loaded / e.total);
|
74 |
}
|
75 |
};
|
76 |
+
xhr.open('GET', proxyUrl); // ここでプロキシURLを使用
|
77 |
xhr.send();
|
78 |
});
|
79 |
};
|
|
|
89 |
return originalFetch(url, opts);
|
90 |
};
|
91 |
|
|
|
92 |
// Web Workerのメッセージ処理
|
93 |
const handleWorkerMessage = e => {
|
94 |
const data = e.data;
|