Skip to content

Commit 5f21035

Browse files
committed
refactor: extract download completion logic and improve type safety
1 parent 450a50d commit 5f21035

1 file changed

Lines changed: 50 additions & 47 deletions

File tree

packages/bare-resource-fetcher/src/ResourceFetcher.ts

Lines changed: 50 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,10 @@ interface BareResourceFetcherInterface extends ResourceFetcherAdapter {
3434
sourceExtended: ResourceSourceExtended,
3535
result: string | string[]
3636
): string[] | Promise<string[] | null>;
37+
completeDownload(
38+
extendedInfo: ResourceSourceExtended,
39+
source: ResourceSource
40+
): Promise<string[] | null>;
3741
pause(source: ResourceSource): Promise<void>;
3842
resume(source: ResourceSource): Promise<string[] | null>;
3943
cancel(source: ResourceSource): Promise<void>;
@@ -164,7 +168,13 @@ export const BareResourceFetcher: BareResourceFetcherInterface = {
164168
},
165169

166170
async pause(source: ResourceSource) {
167-
const resource = this.downloads.get(source)!;
171+
const resource = this.downloads.get(source);
172+
if (!resource) {
173+
throw new RnExecutorchError(
174+
RnExecutorchErrorCode.NotFound,
175+
'No active download found for the given source'
176+
);
177+
}
168178
switch (resource.status) {
169179
case DownloadStatus.PAUSED:
170180
throw new RnExecutorchError(
@@ -203,33 +213,9 @@ export const BareResourceFetcher: BareResourceFetcherInterface = {
203213
return new Promise((resolve, reject) => {
204214
resource.task
205215
.done(async () => {
206-
if (
207-
!this.downloads.has(source) ||
208-
this.downloads.get(source)!.status === DownloadStatus.PAUSED
209-
) {
210-
resolve(null);
211-
return;
212-
}
213-
await RNFS.moveFile(
214-
resource.extendedInfo.cacheFileUri!,
215-
resource.extendedInfo.fileUri!
216-
);
217-
this.downloads.delete(source);
218-
ResourceFetcherUtils.triggerHuggingFaceDownloadCounter(
219-
resource.extendedInfo.uri!
220-
);
221-
222-
// Get the filename from the fileUri
223-
const filename = resource.extendedInfo.fileUri!.split('/').pop();
224-
if (filename) {
225-
completeHandler(filename);
226-
}
227-
228-
const result = this.returnOrStartNext(
216+
const result = await this.completeDownload(
229217
resource.extendedInfo,
230-
ResourceFetcherUtils.removeFilePrefix(
231-
resource.extendedInfo.fileUri!
232-
)
218+
source
233219
);
234220
resolve(result);
235221
})
@@ -242,11 +228,45 @@ export const BareResourceFetcher: BareResourceFetcherInterface = {
242228
},
243229

244230
async cancel(source: ResourceSource) {
245-
const resource = this.downloads.get(source)!;
231+
const resource = this.downloads.get(source);
232+
if (!resource) {
233+
throw new RnExecutorchError(
234+
RnExecutorchErrorCode.NotFound,
235+
'No active download found for the given source'
236+
);
237+
}
246238
resource.task.stop();
247239
this.downloads.delete(source);
248240
},
249241

242+
async completeDownload(
243+
extendedInfo: ResourceSourceExtended,
244+
source: ResourceSource
245+
): Promise<string[] | null> {
246+
// Check if download was cancelled or paused
247+
if (
248+
!this.downloads.has(source) ||
249+
this.downloads.get(source)!.status === DownloadStatus.PAUSED
250+
) {
251+
return null;
252+
}
253+
254+
await RNFS.moveFile(extendedInfo.cacheFileUri!, extendedInfo.fileUri!);
255+
this.downloads.delete(source);
256+
ResourceFetcherUtils.triggerHuggingFaceDownloadCounter(extendedInfo.uri!);
257+
258+
const filename = extendedInfo.fileUri!.split('/').pop();
259+
if (filename) {
260+
await completeHandler(filename);
261+
}
262+
263+
const result = this.returnOrStartNext(
264+
extendedInfo,
265+
ResourceFetcherUtils.removeFilePrefix(extendedInfo.fileUri!)
266+
);
267+
return result instanceof Promise ? await result : result;
268+
},
269+
250270
async pauseFetching(...sources: ResourceSource[]) {
251271
const source = this.findActive(sources);
252272
await this.pause(source);
@@ -422,26 +442,9 @@ export const BareResourceFetcher: BareResourceFetcherInterface = {
422442
);
423443
})
424444
.done(async () => {
425-
if (
426-
!this.downloads.has(source) ||
427-
this.downloads.get(source)!.status === DownloadStatus.PAUSED
428-
) {
429-
resolve(null);
430-
return;
431-
}
432-
await RNFS.moveFile(
433-
sourceExtended.cacheFileUri!,
434-
sourceExtended.fileUri!
435-
);
436-
this.downloads.delete(source);
437-
ResourceFetcherUtils.triggerHuggingFaceDownloadCounter(uri);
438-
439-
// Complete the download job
440-
completeHandler(filename);
441-
442-
const nextResult = this.returnOrStartNext(
445+
const nextResult = await this.completeDownload(
443446
sourceExtended,
444-
ResourceFetcherUtils.removeFilePrefix(sourceExtended.fileUri!)
447+
source
445448
);
446449
resolve(nextResult);
447450
})

0 commit comments

Comments
 (0)