tangled
alpha
login
or
join now
stream.place
/
streamplace
Live video on the AT Protocol
74
fork
atom
overview
issues
1
pulls
pipelines
refactor to hook, clean up upload on unmount
Natalie B.
7 months ago
57a077d2
e4721c19
+53
-30
1 changed file
expand all
collapse all
unified
split
js
components
src
streamplace-store
stream.tsx
+53
-30
js/components/src/streamplace-store/stream.tsx
···
6
6
import { useUrl } from "./streamplace-store";
7
7
import { usePDSAgent } from "./xrpc";
8
8
9
9
-
const uploadThumbnail = async (
10
10
-
pdsAgent: StreamplaceAgent,
11
11
-
customThumbnail?: Blob,
12
12
-
) => {
13
13
-
if (customThumbnail) {
14
14
-
let tries = 0;
15
15
-
try {
16
16
-
let thumbnail = await pdsAgent.uploadBlob(customThumbnail);
9
9
+
import { useEffect, useRef } from "react";
17
10
18
18
-
while (
19
19
-
thumbnail.data.blob.size === 0 &&
20
20
-
customThumbnail.size !== 0 &&
21
21
-
tries < 12
22
22
-
) {
23
23
-
console.warn(
24
24
-
"Reuploading blob as blob sizes don't match! Blob size recieved is",
25
25
-
thumbnail.data.blob.size,
26
26
-
"and sent blob size is",
27
27
-
customThumbnail.size,
28
28
-
);
29
29
-
thumbnail = await pdsAgent.uploadBlob(customThumbnail);
30
30
-
}
11
11
+
const useUploadThumbnail = () => {
12
12
+
const abortRef = useRef<AbortController | null>(null);
13
13
+
14
14
+
useEffect(() => {
15
15
+
return () => {
16
16
+
// On unmount, abort any ongoing upload
17
17
+
abortRef.current?.abort();
18
18
+
};
19
19
+
}, []);
20
20
+
21
21
+
const uploadThumbnail = async (
22
22
+
pdsAgent: StreamplaceAgent,
23
23
+
customThumbnail?: Blob,
24
24
+
) => {
25
25
+
if (!customThumbnail) return undefined;
31
26
32
32
-
if (tries === 3) {
33
33
-
throw new Error(`Could not successfully upload blob (tried ${tries}x)`);
34
34
-
}
27
27
+
abortRef.current = new AbortController();
28
28
+
const { signal } = abortRef.current;
29
29
+
30
30
+
const maxTries = 3;
31
31
+
let lastError: unknown = null;
35
32
36
36
-
if (thumbnail.success) {
37
37
-
console.log("Successfully uploaded thumbnail");
38
38
-
return thumbnail.data.blob;
33
33
+
for (let tries = 0; tries < maxTries; tries++) {
34
34
+
try {
35
35
+
const thumbnail = await pdsAgent.uploadBlob(customThumbnail, {
36
36
+
signal,
37
37
+
});
38
38
+
if (
39
39
+
thumbnail.success &&
40
40
+
thumbnail.data.blob.size === customThumbnail.size
41
41
+
) {
42
42
+
console.log("Successfully uploaded thumbnail");
43
43
+
return thumbnail.data.blob;
44
44
+
} else {
45
45
+
console.warn(
46
46
+
`Blob size mismatch (attempt ${tries + 1}): received ${thumbnail.data.blob.size}, expected ${customThumbnail.size}`,
47
47
+
);
48
48
+
}
49
49
+
} catch (e) {
50
50
+
if (signal.aborted) {
51
51
+
console.warn("Upload aborted");
52
52
+
return undefined;
53
53
+
}
54
54
+
lastError = e;
55
55
+
console.warn(`Error uploading thumbnail (attempt ${tries + 1}): ${e}`);
39
56
}
40
40
-
} catch (e) {
41
41
-
throw new Error("Error uploading thumbnail: " + e);
42
57
}
43
43
-
}
58
58
+
59
59
+
throw new Error(
60
60
+
`Could not successfully upload blob after ${maxTries} attempts. Last error: ${lastError}`,
61
61
+
);
62
62
+
};
63
63
+
64
64
+
return uploadThumbnail;
44
65
};
45
66
46
67
async function createNewPost(
···
99
120
export function useCreateStreamRecord() {
100
121
let agent = usePDSAgent();
101
122
let url = useUrl();
123
123
+
const uploadThumbnail = useUploadThumbnail();
102
124
103
125
return async (
104
126
title: string,
···
206
228
export function useUpdateStreamRecord() {
207
229
let agent = usePDSAgent();
208
230
let url = useUrl();
231
231
+
const uploadThumbnail = useUploadThumbnail();
209
232
210
233
return async (
211
234
title: string,