Skip to content

Commit 0d0779c

Browse files
fix(mobile): bound favicon request state
1 parent 6ada303 commit 0d0779c

3 files changed

Lines changed: 140 additions & 33 deletions

File tree

‎apps/mobile/src/components/ProjectFavicon.tsx‎

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -62,15 +62,21 @@ function ProjectFaviconImage(props: {
6262
() => createProjectFaviconRequest(props.cacheKey, props.faviconUrl),
6363
[props.cacheKey, props.faviconUrl],
6464
);
65+
const [activeFaviconRequest, setActiveFaviconRequest] = useState<typeof faviconRequest>(null);
6566
useLayoutEffect(() => {
66-
beginProjectFaviconRequest(faviconRequest);
67+
if (faviconRequest === null) return;
68+
69+
const endRequest = beginProjectFaviconRequest(faviconRequest);
70+
setActiveFaviconRequest(faviconRequest);
71+
return endRequest;
6772
}, [faviconRequest]);
6873

6974
const [status, setStatus] = useState<"loading" | "loaded" | "error">(() =>
7075
hasLoadedProjectFavicon(props.cacheKey) ? "loaded" : "loading",
7176
);
7277

73-
const showImage = props.faviconUrl !== null && status === "loaded";
78+
const requestIsActive = faviconRequest !== null && activeFaviconRequest === faviconRequest;
79+
const showImage = requestIsActive && status === "loaded";
7480

7581
return (
7682
<View
@@ -92,15 +98,15 @@ function ProjectFaviconImage(props: {
9298
) : null}
9399

94100
{/* Favicon image (hidden until loaded) */}
95-
{props.faviconUrl ? (
101+
{requestIsActive ? (
96102
<Image
97-
key={props.faviconUrl}
103+
key={faviconRequest.faviconUrl}
98104
source={{
99-
uri: props.faviconUrl,
100-
...(props.cacheKey ? { cacheKey: props.cacheKey } : {}),
105+
uri: faviconRequest.faviconUrl,
106+
cacheKey: faviconRequest.cacheKey,
101107
}}
102108
cachePolicy="memory-disk"
103-
recyclingKey={props.cacheKey}
109+
recyclingKey={faviconRequest.cacheKey}
104110
accessibilityLabel={`${props.projectTitle} favicon`}
105111
style={{
106112
width: props.size,

‎apps/mobile/src/components/projectFaviconCache.test.ts‎

Lines changed: 58 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,40 +15,94 @@ describe("project favicon cache", () => {
1515
const refreshedUrl = "https://environment.example/api/assets/refreshed/v1-favicon.svg";
1616

1717
const expiredRequest = createProjectFaviconRequest(cacheKey, expiredUrl);
18-
beginProjectFaviconRequest(expiredRequest);
18+
const endExpiredRequest = beginProjectFaviconRequest(expiredRequest);
1919
markProjectFaviconLoaded(expiredRequest);
2020
const refreshedRequest = createProjectFaviconRequest(cacheKey, refreshedUrl);
21-
beginProjectFaviconRequest(refreshedRequest);
21+
const endRefreshedRequest = beginProjectFaviconRequest(refreshedRequest);
2222

2323
expect(markProjectFaviconLoaded(expiredRequest)).toBe(false);
2424
expect(markProjectFaviconFailed(expiredRequest)).toBe(false);
2525
expect(hasLoadedProjectFavicon(cacheKey)).toBe(true);
2626
expect(markProjectFaviconFailed(refreshedRequest)).toBe(true);
2727
expect(hasLoadedProjectFavicon(cacheKey)).toBe(false);
28+
29+
endRefreshedRequest();
30+
endExpiredRequest();
2831
});
2932

3033
it("evicts the URL that actually failed", () => {
3134
const cacheKey = "environment-1:/workspace:v2-favicon.svg";
3235
const faviconUrl = "https://environment.example/api/assets/current/v2-favicon.svg";
3336
const request = createProjectFaviconRequest(cacheKey, faviconUrl);
34-
beginProjectFaviconRequest(request);
37+
const endRequest = beginProjectFaviconRequest(request);
3538

3639
markProjectFaviconLoaded(request);
3740

3841
expect(markProjectFaviconFailed(request)).toBe(true);
3942
expect(hasLoadedProjectFavicon(cacheKey)).toBe(false);
43+
44+
endRequest();
4045
});
4146

4247
it("does not supersede a request until the next request begins", () => {
4348
const cacheKey = "environment-1:/workspace:v3-favicon.svg";
4449
const committedUrl = "https://environment.example/api/assets/current/v3-favicon.svg";
4550
const abandonedUrl = "https://environment.example/api/assets/abandoned/v3-favicon.svg";
4651
const committedRequest = createProjectFaviconRequest(cacheKey, committedUrl);
47-
beginProjectFaviconRequest(committedRequest);
52+
const endCommittedRequest = beginProjectFaviconRequest(committedRequest);
4853

4954
createProjectFaviconRequest(cacheKey, abandonedUrl);
5055

5156
expect(markProjectFaviconLoaded(committedRequest)).toBe(true);
5257
expect(hasLoadedProjectFavicon(cacheKey)).toBe(true);
58+
59+
endCommittedRequest();
60+
});
61+
62+
it("requires a cache key before creating a URL-bearing request", () => {
63+
const firstUrl = "https://environment.example/api/assets/first/favicon.svg";
64+
const secondUrl = "https://environment.example/api/assets/second/favicon.svg";
65+
66+
expect(createProjectFaviconRequest(null, firstUrl)).toBeNull();
67+
expect(createProjectFaviconRequest(null, secondUrl)).toBeNull();
68+
});
69+
70+
it("restores the remaining active URL when a newer request ends", () => {
71+
const cacheKey = "environment-1:/workspace:v4-favicon.svg";
72+
const firstRequest = createProjectFaviconRequest(
73+
cacheKey,
74+
"https://environment.example/api/assets/first/v4-favicon.svg",
75+
);
76+
const secondRequest = createProjectFaviconRequest(
77+
cacheKey,
78+
"https://environment.example/api/assets/second/v4-favicon.svg",
79+
);
80+
const endFirstRequest = beginProjectFaviconRequest(firstRequest);
81+
const endSecondRequest = beginProjectFaviconRequest(secondRequest);
82+
83+
expect(markProjectFaviconLoaded(firstRequest)).toBe(false);
84+
endSecondRequest();
85+
expect(markProjectFaviconLoaded(firstRequest)).toBe(true);
86+
endFirstRequest();
87+
expect(markProjectFaviconLoaded(firstRequest)).toBe(false);
88+
});
89+
90+
it("bounds remembered loaded revisions", () => {
91+
const firstCacheKey = "environment-1:/workspace:revision-0";
92+
let lastCacheKey = firstCacheKey;
93+
94+
for (let revision = 0; revision < 300; revision++) {
95+
lastCacheKey = `environment-1:/workspace:revision-${revision}`;
96+
const request = createProjectFaviconRequest(
97+
lastCacheKey,
98+
`https://environment.example/api/assets/revision-${revision}/favicon.svg`,
99+
);
100+
const endRequest = beginProjectFaviconRequest(request);
101+
markProjectFaviconLoaded(request);
102+
endRequest();
103+
}
104+
105+
expect(hasLoadedProjectFavicon(firstCacheKey)).toBe(false);
106+
expect(hasLoadedProjectFavicon(lastCacheKey)).toBe(true);
53107
});
54108
});
Lines changed: 69 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,94 @@
11
export interface ProjectFaviconRequest {
2-
readonly cacheKey: string | null;
3-
readonly faviconUrl: string | null;
2+
readonly cacheKey: string;
3+
readonly faviconUrl: string;
44
}
55

6-
const currentFaviconUrls = new Map<string, string>();
7-
const loadedFaviconKeys = new Set<string>();
6+
interface ActiveFaviconRequests {
7+
readonly urls: Map<string, number>;
8+
currentUrl: string;
9+
}
10+
11+
const MAX_LOADED_FAVICONS = 256;
12+
const activeFaviconRequests = new Map<string, ActiveFaviconRequests>();
13+
const loadedFaviconKeys = new Map<string, true>();
814

15+
export function createProjectFaviconRequest(
16+
cacheKey: string,
17+
faviconUrl: string,
18+
): ProjectFaviconRequest;
19+
export function createProjectFaviconRequest(
20+
cacheKey: string | null,
21+
faviconUrl: string | null,
22+
): ProjectFaviconRequest | null;
923
export function createProjectFaviconRequest(cacheKey: string | null, faviconUrl: string | null) {
24+
if (!cacheKey || !faviconUrl) return null;
1025
return { cacheKey, faviconUrl };
1126
}
1227

1328
export function beginProjectFaviconRequest(request: ProjectFaviconRequest) {
14-
if (request.cacheKey && request.faviconUrl) {
15-
currentFaviconUrls.set(request.cacheKey, request.faviconUrl);
29+
let activeRequests = activeFaviconRequests.get(request.cacheKey);
30+
if (!activeRequests) {
31+
activeRequests = { currentUrl: request.faviconUrl, urls: new Map() };
32+
activeFaviconRequests.set(request.cacheKey, activeRequests);
1633
}
34+
35+
const activeCount = activeRequests.urls.get(request.faviconUrl) ?? 0;
36+
activeRequests.urls.delete(request.faviconUrl);
37+
activeRequests.urls.set(request.faviconUrl, activeCount + 1);
38+
activeRequests.currentUrl = request.faviconUrl;
39+
40+
let ended = false;
41+
return () => {
42+
if (ended) return;
43+
ended = true;
44+
45+
const remainingCount = (activeRequests.urls.get(request.faviconUrl) ?? 1) - 1;
46+
if (remainingCount > 0) {
47+
activeRequests.urls.set(request.faviconUrl, remainingCount);
48+
return;
49+
}
50+
51+
activeRequests.urls.delete(request.faviconUrl);
52+
if (activeRequests.urls.size === 0) {
53+
if (activeFaviconRequests.get(request.cacheKey) === activeRequests) {
54+
activeFaviconRequests.delete(request.cacheKey);
55+
}
56+
return;
57+
}
58+
59+
if (activeRequests.currentUrl === request.faviconUrl) {
60+
activeRequests.currentUrl = Array.from(activeRequests.urls.keys()).at(-1)!;
61+
}
62+
};
1763
}
1864

1965
export function hasLoadedProjectFavicon(cacheKey: string | null) {
2066
return cacheKey !== null && loadedFaviconKeys.has(cacheKey);
2167
}
2268

23-
export function markProjectFaviconLoaded(request: ProjectFaviconRequest) {
24-
if (
25-
request.cacheKey &&
26-
request.faviconUrl &&
27-
currentFaviconUrls.get(request.cacheKey) !== request.faviconUrl
28-
) {
29-
return false;
69+
function isCurrentProjectFaviconRequest(request: ProjectFaviconRequest) {
70+
return activeFaviconRequests.get(request.cacheKey)?.currentUrl === request.faviconUrl;
71+
}
72+
73+
function rememberLoadedProjectFavicon(cacheKey: string) {
74+
loadedFaviconKeys.delete(cacheKey);
75+
loadedFaviconKeys.set(cacheKey, true);
76+
77+
if (loadedFaviconKeys.size > MAX_LOADED_FAVICONS) {
78+
loadedFaviconKeys.delete(loadedFaviconKeys.keys().next().value!);
3079
}
80+
}
3181

32-
if (request.cacheKey) loadedFaviconKeys.add(request.cacheKey);
82+
export function markProjectFaviconLoaded(request: ProjectFaviconRequest) {
83+
if (!isCurrentProjectFaviconRequest(request)) return false;
84+
85+
rememberLoadedProjectFavicon(request.cacheKey);
3386
return true;
3487
}
3588

3689
export function markProjectFaviconFailed(request: ProjectFaviconRequest) {
37-
if (
38-
request.cacheKey &&
39-
request.faviconUrl &&
40-
currentFaviconUrls.get(request.cacheKey) !== request.faviconUrl
41-
) {
42-
return false;
43-
}
90+
if (!isCurrentProjectFaviconRequest(request)) return false;
4491

45-
if (request.cacheKey) loadedFaviconKeys.delete(request.cacheKey);
92+
loadedFaviconKeys.delete(request.cacheKey);
4693
return true;
4794
}

0 commit comments

Comments
 (0)