Skip to content

Commit b45a62e

Browse files
committed
Call every second
1 parent fa4b92b commit b45a62e

1 file changed

Lines changed: 14 additions & 12 deletions

File tree

components/system/Desktop/Wallpapers/handlers.ts

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import {
99
HIGH_PRIORITY_REQUEST,
1010
MILLISECONDS_IN_DAY,
1111
MILLISECONDS_IN_HOUR,
12+
MILLISECONDS_IN_SECOND,
1213
} from "utils/constants";
1314
import {
1415
jsonFetch,
@@ -104,37 +105,38 @@ export const wallpaperHandler: Record<string, WallpaperHandler> = {
104105
method: "POST",
105106
}
106107
);
107-
let wallpaperUrl = "";
108-
109-
for (let a = 0; a < MAX_RETRIES; a++) {
108+
const maybeFetchArtwork = async (attempt = 1): Promise<string> => {
110109
try {
111-
// eslint-disable-next-line no-await-in-loop
112110
const { data: [{ image_id } = {}] = [] } = await fetchArtwork();
113111

114112
if (image_id) {
115113
const url = `https://www.artic.edu/iiif/2/${image_id}/full/1686,/0/default.jpg`;
116-
117-
// eslint-disable-next-line no-await-in-loop
118114
const { ok } = await fetch(url, {
119115
...HIGH_PRIORITY_REQUEST,
120116
method: "HEAD",
121117
});
122118

123-
if (ok) {
124-
wallpaperUrl = url;
125-
break;
126-
}
119+
if (ok) return url;
127120
}
128121
} catch {
129122
// Ignore failure to get wallpaper
130123
}
131-
}
124+
125+
return attempt < MAX_RETRIES
126+
? await new Promise((resolve) => {
127+
setTimeout(
128+
() => resolve(maybeFetchArtwork(attempt + 1)),
129+
MILLISECONDS_IN_SECOND
130+
);
131+
})
132+
: "";
133+
};
132134

133135
return {
134136
fallbackBackground: "",
135137
newWallpaperFit: "fit",
136138
updateTimeout: MILLISECONDS_IN_HOUR,
137-
wallpaperUrl,
139+
wallpaperUrl: await maybeFetchArtwork(),
138140
};
139141
},
140142
LOREM_PICSUM: () => ({

0 commit comments

Comments
 (0)