Queue uploads

This commit is contained in:
Ivan Malison 2024-02-05 01:42:32 -07:00
parent f44a1597c0
commit 000918844a

View File

@ -24,18 +24,40 @@ type Dictionary<KeyType extends string | number | symbol, ValueType> = {
class StreamUploadManager<TCacheShape> {
client: ApolloClient<TCacheShape>;
videoId: number;
nextUploadIdToRequest: number;
highestUploadLinkObtained: number;
prefetchedUploadLinks: Dictionary<number, string>;
nextUploadIdToRequest: number = 0;
highestUploadLinkObtained: number = -1;
prefetchedUploadLinks: Dictionary<number, string> = {};
uploadQueue: Array<() => Promise<void>> = [];
isUploading: boolean = false;
constructor(client: ApolloClient<TCacheShape>, streamId: number) {
constructor(client: ApolloClient<TCacheShape>, videoId: number) {
this.client = client;
this.videoId = streamId;
this.highestUploadLinkObtained = -1;
this.prefetchedUploadLinks = {};
this.videoId = videoId;
}
enqueueUploadTask(task: () => Promise<void>) {
this.uploadQueue.push(task);
this.processUploadQueue();
}
async processUploadQueue() {
if (this.isUploading || this.uploadQueue.length === 0) {
return;
}
this.isUploading = true;
const task = this.uploadQueue.shift();
try {
if (task) await task();
} catch (error) {
console.error("Error processing upload task", error);
} finally {
this.isUploading = false;
this.processUploadQueue();
}
}
async uploadChunk({ filepath, index }: { filepath: string; index: number }) {
this.enqueueUploadTask(async () => {
const uploadUrl = await this.getUploadLink(index);
const uploadRequest = RNFS.uploadFiles({
toUrl: uploadUrl,
@ -52,27 +74,29 @@ class StreamUploadManager<TCacheShape> {
},
});
console.log(JSON.stringify(uploadRequest));
const result = await uploadRequest.promise.catch((err) =>
console.log("Upload error!", err),
);
if (response.statusCode == 200) {
const result = await uploadRequest.promise;
if (result.statusCode === 200) {
console.log(`${filepath} Uploaded`);
} else {
console.log("SERVER ERROR");
console.error("SERVER ERROR");
}
});
}
async getUploadLink(chunkId: number): Promise<string> {
if (this.prefetchedUploadLinks[chunkId]) {
return this.prefetchedUploadLinks[chunkId];
}
return this.requestUploadLink(chunkId);
}
async requestUploadLink(chunkId: number): Promise<string> {
console.log(`Requesting ${chunkId}`);
console.log(`Requesting upload link for chunk ${chunkId}`);
const result = await this.client.mutate({
mutation: gql.GetUploadLinkDocument,
variables: { videoId: this.videoId, chunkIndex: chunkId },
});
console.log(JSON.stringify(result.data));
this.prefetchedUploadLinks[chunkId] = result.data.getUploadLink.uploadUrl;
return result.data.getUploadLink.uploadUrl;
}
}
@ -140,8 +164,8 @@ export default function CameraScreen({
const device = useCameraDevice("back");
const format = useCameraFormat(device, [
{ videoResolution: { width: 3048, height: 2160 } },
{ fps: 60 },
{ videoResolution: { width: 1920, height: 1080 } },
{ fps: 30 },
]);
// TODO(#60): setOrientation should be called when changes are detected