Skip to content

Commit 9f8dca2

Browse files
author
Mug
committed
debug7
1 parent dff6142 commit 9f8dca2

File tree

3 files changed

+148
-105
lines changed

3 files changed

+148
-105
lines changed

public/ImgWorker.js

Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
//TODO: This file compiles to invalid JavaScript
2+
//Thus its in the public folder
3+
4+
let reportImg = (event,r,time,size,url,success) => {
5+
console.log(r.headers);
6+
const data = {
7+
url: url,
8+
success: success,
9+
cached: r != null && r.headers.get("X-Cache") == "HIT",
10+
bytes: size,
11+
duration: time
12+
};
13+
console.log("reportImg", data);
14+
15+
/*event.respondWith((async () => {
16+
return fetch(
17+
"https://api.mangadex.network/report",
18+
{
19+
method: "POST",
20+
headers: {"Content-Type": "application/json"},
21+
body: data
22+
}
23+
);
24+
})());*/
25+
}
26+
27+
function makeRequest(method, url) {
28+
return new Promise(function (resolve, reject) {
29+
let xhr = new XMLHttpRequest();
30+
xhr.responseType = "blob";
31+
xhr.open(method, url);
32+
xhr.onload = function () {
33+
if (this.status >= 200 && this.status < 300) {
34+
const cacheHit = xhr.getResponseHeader("X-Cache");
35+
console.log(cacheHit);
36+
resolve(URL.createObjectURL(xhr.response));
37+
} else {
38+
reject({
39+
status: this.status,
40+
statusText: xhr.statusText
41+
});
42+
}
43+
};
44+
xhr.onerror = function () {
45+
reject({
46+
status: this.status,
47+
statusText: xhr.statusText
48+
});
49+
};
50+
xhr.send();
51+
});
52+
}
53+
54+
let fetchImg2 = async (event,url) => {
55+
const resp = await makeRequest("GET", url);
56+
console.log(resp);
57+
return resp;
58+
}
59+
let fetchImg = async (event,url) => {
60+
const when = Date.now()
61+
var headers = null;
62+
return await fetch(url)
63+
.then((r) => {
64+
headers = r.headers;
65+
return r;
66+
})
67+
.then((r) => r.blob())
68+
.then((blob) => URL.createObjectURL(blob))
69+
.catch((r) => {
70+
/*console.log("fetchImg failed");
71+
reportImg(
72+
event,
73+
r,
74+
Date.now() - when,
75+
0,
76+
url,
77+
false
78+
);*/
79+
return null;
80+
});
81+
}
82+
83+
let sleep = (ms) => {
84+
return new Promise(resolve => setTimeout(resolve, ms));
85+
}
86+
87+
let exponentialBackoff = (i) => {
88+
return Math.min(Math.pow(2,i)+(Math.random()*1000),32000)
89+
}
90+
91+
onmessage = async (e) => {
92+
const { msg } = e.data;
93+
94+
console.log("IW Message", msg)
95+
96+
if (msg.cmd == "fetch") {
97+
var page = 0;
98+
var pages = 1;
99+
var i=0;
100+
101+
while (page<pages) {
102+
const chapterId=msg.args[0];
103+
const dlpages=msg.args[1];
104+
const JSN = await fetch(
105+
`${msg.CORS_BYPASS}https://api.mangadex.org/at-home/server/${chapterId}`,
106+
).then((rsp) => rsp.json());
107+
const imgs = msg.datasave ? JSN.chapter.dataSaver : JSN.chapter.data;
108+
console.log("IMG", imgs);
109+
110+
pages = imgs.length;
111+
for (; page<imgs.length; page++) {
112+
if (dlpages != null && !dlpages.includes(page)) {
113+
continue;
114+
}
115+
116+
const file = imgs[page];
117+
const url = await fetchImg2(e,`${msg.CORS_BYPASS}${JSN.baseUrl}/data/${JSN.chapter.hash}/${file}`);
118+
119+
if (url == null) {
120+
await sleep(exponentialBackoff(i));
121+
i += 1;
122+
break;
123+
}
124+
125+
postMessage({
126+
result: {
127+
page: page,
128+
pages: imgs.length,
129+
url: url
130+
}
131+
});
132+
}
133+
}
134+
}
135+
};

src/Chapter.js

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,6 @@ import axios from "axios";
1313
import API, { CORS_BYPASS } from "./MangaDexAPI/API";
1414
import { IntArray, APlaceholder } from "./utility";
1515

16-
// Your web worker
17-
import ImgWorker from "./ImgWorker";
18-
1916
import './css/Chapter.css';
2017

2118
import {
@@ -196,16 +193,10 @@ class ReaderMain extends React.Component {
196193
if (this.webWorker != null) {
197194
this.webWorker.terminate();
198195
}
199-
200-
//this.webWorker = new WebWorker(ImgWorker);
201-
this.webWorker=new window.Worker(ImgWorker);
202196

203-
this.webWorker.postMessage({msg: {
204-
cmd: "fetch",
205-
args: [this.props.chapter.getId()],
206-
datasave: this.props.cfg.getValue("DATASAVER"),
207-
CORS_BYPASS: CORS_BYPASS
208-
}});
197+
this.webWorker=new window.Worker("/ImgWorker.js");
198+
this.refreshCounter = 1;
199+
209200
this.webWorker.onerror = () => {
210201
console.log("WW-Error");
211202
};
@@ -239,6 +230,14 @@ class ReaderMain extends React.Component {
239230
console.log("WW-Error");
240231
}
241232
};
233+
234+
//TODO: Load on demand instead of all of them
235+
this.webWorker.postMessage({msg: {
236+
cmd: "fetch",
237+
args: [this.props.chapter.getId(),null],
238+
datasave: this.props.cfg.getValue("DATASAVER"),
239+
CORS_BYPASS: CORS_BYPASS
240+
}});
242241
}
243242

244243
componentDidMount() {
@@ -262,7 +261,8 @@ class ReaderMain extends React.Component {
262261
if (prevId != currId) {
263262
this.refreshCounter = 0;
264263
this.setState({
265-
pages: []
264+
pages: [],
265+
lpages: null
266266
});
267267
this.componentDidMount();
268268
}

src/ImgWorker.js

Lines changed: 0 additions & 92 deletions
This file was deleted.

0 commit comments

Comments
 (0)