Skip to content

Commit 0af752a

Browse files
committed
fix: 🐛 修复加载两个相同图片时的加载 bug
1 parent 55bcbf4 commit 0af752a

2 files changed

Lines changed: 14 additions & 17 deletions

File tree

docs/.other/Dev.md

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
## TODO
22

3-
- 不再搞两个 _setState
4-
5-
- 用个函数打包一下 if (isDevMode) Object.assign((window as any).unsafeWindow ?? window, { request });
6-
- imageLoad ,现在图片加载是通过 index 来标识的,这会导致出现重复 url 的图片时无法正常加载
3+
- 实现图片放大的 demo
74

85
## 暂不考虑实现的功能
96

src/components/Manga/actions/imageLoad.ts

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -67,21 +67,21 @@ export const handleImgError = (url: string, e?: HTMLImageElement) => {
6767

6868
/** 需要加载的图片 */
6969
const needLoadImgList = createRootMemo(() => {
70-
const list = new Set<number>();
71-
for (const [index, img] of imgList().entries())
72-
if (img.loadType !== 'loaded' && img.src) list.add(index);
70+
const list = new Set<string>();
71+
for (const img of imgList())
72+
if (img.loadType !== 'loaded' && img.src) list.add(img.src);
7373
return list;
7474
});
7575

76-
/** 当前需要加载的图片 */
77-
const loadImgList = new Set<number>();
76+
/** 当前加载的图片 */
77+
const loadImgList = new Set<string>();
7878

7979
/** 加载指定图片。返回是否已加载完成 */
80-
const loadImg = (index: number) => {
81-
if (index === -1 || !needLoadImgList().has(index)) return true;
82-
const img = getImg(index);
80+
const loadImg = (url: string) => {
81+
const img = store.imgMap[url];
82+
if (!needLoadImgList().has(img.src)) return true;
8383
if (img.loadType === 'error') return true;
84-
loadImgList.add(index);
84+
loadImgList.add(url);
8585
return false;
8686
};
8787

@@ -123,7 +123,7 @@ const loadRangeImg = (target = 0, loadNum = 2) => {
123123
const step = start <= end ? 1 : -1;
124124

125125
while (condition()) {
126-
if (!loadImg(index)) hasUnloadedImg = true;
126+
if (!loadImg(getImg(index).src)) hasUnloadedImg = true;
127127
if (loadImgList.size >= loadNum) return index !== end || hasUnloadedImg;
128128
index += step;
129129
}
@@ -170,9 +170,9 @@ export const updateImgLoadType = singleThreaded(() => {
170170
}
171171

172172
setState((state) => {
173-
for (const index of needLoadImgList()) {
174-
const img = getImg(index, state);
175-
if (loadImgList.has(index)) {
173+
for (const url of needLoadImgList()) {
174+
const img = state.imgMap[url];
175+
if (loadImgList.has(url)) {
176176
if (img.loadType !== 'loading') {
177177
img.loadType = 'loading';
178178
if (!store.option.imgRecognition.enabled && img.width === undefined)

0 commit comments

Comments
 (0)