Skip to content

Commit db98ba8

Browse files
committed
docs: complete JSDoc documentation for ResourceFetcher modules
1 parent 5f21035 commit db98ba8

3 files changed

Lines changed: 240 additions & 5 deletions

File tree

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

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,35 @@
1+
/**
2+
* Resource Fetcher for React Native applications.
3+
*
4+
* This module provides functions to download and manage files stored in the application's document directory
5+
* inside the `react-native-executorch/` directory. These utilities help manage storage and clean up downloaded
6+
* files when they are no longer needed.
7+
*
8+
* @category Utilities - General
9+
*
10+
* @remarks
11+
* **Key Functionality:**
12+
* - **Download Control**: Pause, resume, and cancel operations through:
13+
* - {@link pauseFetching} - Pause ongoing downloads
14+
* - {@link resumeFetching} - Resume paused downloads
15+
* - {@link cancelFetching} - Cancel ongoing or paused downloads
16+
* - **File Management**:
17+
* - {@link getFilesTotalSize} - Get total size of resources
18+
* - {@link listDownloadedFiles} - List all downloaded files
19+
* - {@link listDownloadedModels} - List downloaded model files (.pte)
20+
* - {@link deleteResources} - Delete downloaded resources
21+
*
22+
* **Important Notes:**
23+
* - Pause/resume/cancel operations work only for remote resources
24+
* - Most functions accept multiple `ResourceSource` arguments (string, number, or object)
25+
* - The {@link fetch} method accepts a progress callback (0-1) and returns file paths or null if interrupted
26+
*
27+
* **Technical Implementation:**
28+
* - Maintains a `downloads` Map to track active and paused downloads
29+
* - Successful downloads are automatically removed from the Map
30+
* - Uses `ResourceSourceExtended` interface for pause/resume functionality with linked-list behavior
31+
*/
32+
133
import {
234
createDownloadTask,
335
completeHandler,
@@ -62,9 +94,23 @@ interface BareResourceFetcherInterface extends ResourceFetcherAdapter {
6294
): Promise<string[] | string | null>;
6395
}
6496

97+
/**
98+
* This module provides functions to download and work with downloaded files stored in the application's document directory inside the `react-native-executorch/` directory.
99+
* These utilities can help you manage your storage and clean up the downloaded files when they are no longer needed.
100+
*
101+
* @category Utilities - General
102+
*/
65103
export const BareResourceFetcher: BareResourceFetcherInterface = {
66104
downloads: new Map<ResourceSource, DownloadResource>(), //map of currently downloading (or paused) files, if the download was started by .fetch() method.
67105

106+
/**
107+
* Fetches resources (remote URLs, local files or embedded assets), downloads or stores them locally for use by React Native ExecuTorch.
108+
*
109+
* @param callback - Optional callback to track progress of all downloads, reported between 0 and 1.
110+
* @param sources - Multiple resources that can be strings, asset references, or objects.
111+
* @returns If the fetch was successful, it returns a promise which resolves to an array of local file paths for the downloaded/stored resources (without file:// prefix).
112+
* If the fetch was interrupted by `pauseFetching` or `cancelFetching`, it returns a promise which resolves to `null`.
113+
*/
68114
async fetch(
69115
callback: (downloadProgress: number) => void = () => {},
70116
...sources: ResourceSource[]
@@ -267,16 +313,35 @@ export const BareResourceFetcher: BareResourceFetcherInterface = {
267313
return result instanceof Promise ? await result : result;
268314
},
269315

316+
/**
317+
* Pauses an ongoing download of files.
318+
*
319+
* @param sources - The resource identifiers used when calling `fetch`.
320+
* @returns A promise that resolves once the download is paused.
321+
*/
270322
async pauseFetching(...sources: ResourceSource[]) {
271323
const source = this.findActive(sources);
272324
await this.pause(source);
273325
},
274326

327+
/**
328+
* Resumes a paused download of files.
329+
*
330+
* @param sources - The resource identifiers used when calling fetch.
331+
* @returns If the fetch was successful, it returns a promise which resolves to an array of local file paths for the downloaded resources (without file:// prefix).
332+
* If the fetch was again interrupted by `pauseFetching` or `cancelFetching`, it returns a promise which resolves to `null`.
333+
*/
275334
async resumeFetching(...sources: ResourceSource[]) {
276335
const source = this.findActive(sources);
277336
await this.resume(source);
278337
},
279338

339+
/**
340+
* Cancels an ongoing/paused download of files.
341+
*
342+
* @param sources - The resource identifiers used when calling `fetch()`.
343+
* @returns A promise that resolves once the download is canceled.
344+
*/
280345
async cancelFetching(...sources: ResourceSource[]) {
281346
const source = this.findActive(sources);
282347
await this.cancel(source);
@@ -294,16 +359,32 @@ export const BareResourceFetcher: BareResourceFetcherInterface = {
294359
);
295360
},
296361

362+
/**
363+
* Lists all the downloaded files used by React Native ExecuTorch.
364+
*
365+
* @returns A promise, which resolves to an array of URIs for all the downloaded files.
366+
*/
297367
async listDownloadedFiles() {
298368
const files = await RNFS.readDir(RNEDirectory);
299369
return files.map((file) => file.path);
300370
},
301371

372+
/**
373+
* Lists all the downloaded models used by React Native ExecuTorch.
374+
*
375+
* @returns A promise, which resolves to an array of URIs for all the downloaded models.
376+
*/
302377
async listDownloadedModels() {
303378
const files = await this.listDownloadedFiles();
304379
return files.filter((file: string) => file.endsWith('.pte'));
305380
},
306381

382+
/**
383+
* Deletes downloaded resources from the local filesystem.
384+
*
385+
* @param sources - The resource identifiers used when calling `fetch`.
386+
* @returns A promise that resolves once all specified resources have been removed.
387+
*/
307388
async deleteResources(...sources: ResourceSource[]) {
308389
for (const source of sources) {
309390
const filename = ResourceFetcherUtils.getFilenameFromUri(
@@ -316,6 +397,12 @@ export const BareResourceFetcher: BareResourceFetcherInterface = {
316397
}
317398
},
318399

400+
/**
401+
* Fetches the info about files size. Works only for remote files.
402+
*
403+
* @param sources - The resource identifiers (URLs).
404+
* @returns A promise that resolves to combined size of files in bytes.
405+
*/
319406
async getFilesTotalSize(...sources: ResourceSource[]) {
320407
return (await ResourceFetcherUtils.getFilesSizes(sources)).totalLength;
321408
},
@@ -470,6 +557,15 @@ export const BareResourceFetcher: BareResourceFetcherInterface = {
470557
});
471558
},
472559

560+
/**
561+
* Reads the contents of a file as a string.
562+
*
563+
* @param path - Absolute file path to read.
564+
* @returns A promise that resolves to the file contents as a string.
565+
*
566+
* @remarks
567+
* **REQUIRED**: Used internally for reading configuration files (e.g., tokenizer configs).
568+
*/
473569
async readAsString(path: string) {
474570
return await RNFS.readFile(path, 'utf8');
475571
},

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

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,35 @@
1+
/**
2+
* Resource Fetcher for Expo applications.
3+
*
4+
* This module provides functions to download and manage files stored in the application's document directory
5+
* inside the `react-native-executorch/` directory. These utilities help manage storage and clean up downloaded
6+
* files when they are no longer needed.
7+
*
8+
* @category Utilities - General
9+
*
10+
* @remarks
11+
* **Key Functionality:**
12+
* - **Download Control**: Pause, resume, and cancel operations through:
13+
* - {@link pauseFetching} - Pause ongoing downloads
14+
* - {@link resumeFetching} - Resume paused downloads
15+
* - {@link cancelFetching} - Cancel ongoing or paused downloads
16+
* - **File Management**:
17+
* - {@link getFilesTotalSize} - Get total size of resources
18+
* - {@link listDownloadedFiles} - List all downloaded files
19+
* - {@link listDownloadedModels} - List downloaded model files (.pte)
20+
* - {@link deleteResources} - Delete downloaded resources
21+
*
22+
* **Important Notes:**
23+
* - Pause/resume/cancel operations work only for remote resources
24+
* - Most functions accept multiple `ResourceSource` arguments (string, number, or object)
25+
* - The {@link fetch} method accepts a progress callback (0-1) and returns file paths or null if interrupted
26+
*
27+
* **Technical Implementation:**
28+
* - Maintains a `downloads` Map to track active and paused downloads
29+
* - Successful downloads are automatically removed from the Map
30+
* - Uses `ResourceSourceExtended` interface for pause/resume functionality with linked-list behavior
31+
*/
32+
133
import {
234
cacheDirectory,
335
copyAsync,
@@ -59,9 +91,23 @@ interface ExpoResourceFetcherInterface extends ResourceFetcherAdapter {
5991
): Promise<string[] | string | null>;
6092
}
6193

94+
/**
95+
* This module provides functions to download and work with downloaded files stored in the application's document directory inside the `react-native-executorch/` directory.
96+
* These utilities can help you manage your storage and clean up the downloaded files when they are no longer needed.
97+
*
98+
* @category Utilities - General
99+
*/
62100
export const ExpoResourceFetcher: ExpoResourceFetcherInterface = {
63101
downloads: new Map<ResourceSource, DownloadResource>(), //map of currently downloading (or paused) files, if the download was started by .fetch() method.
64102

103+
/**
104+
* Fetches resources (remote URLs, local files or embedded assets), downloads or stores them locally for use by React Native ExecuTorch.
105+
*
106+
* @param callback - Optional callback to track progress of all downloads, reported between 0 and 1.
107+
* @param sources - Multiple resources that can be strings, asset references, or objects.
108+
* @returns If the fetch was successful, it returns a promise which resolves to an array of local file paths for the downloaded/stored resources (without file:// prefix).
109+
* If the fetch was interrupted by `pauseFetching` or `cancelFetching`, it returns a promise which resolves to `null`.
110+
*/
65111
async fetch(
66112
callback: (downloadProgress: number) => void = () => {},
67113
...sources: ResourceSource[]
@@ -240,16 +286,35 @@ export const ExpoResourceFetcher: ExpoResourceFetcherInterface = {
240286
this.downloads.delete(source);
241287
},
242288

289+
/**
290+
* Pauses an ongoing download of files.
291+
*
292+
* @param sources - The resource identifiers used when calling `fetch`.
293+
* @returns A promise that resolves once the download is paused.
294+
*/
243295
async pauseFetching(...sources: ResourceSource[]) {
244296
const source = this.findActive(sources);
245297
await this.pause(source);
246298
},
247299

300+
/**
301+
* Resumes a paused download of files.
302+
*
303+
* @param sources - The resource identifiers used when calling fetch.
304+
* @returns If the fetch was successful, it returns a promise which resolves to an array of local file paths for the downloaded resources (without file:// prefix).
305+
* If the fetch was again interrupted by `pauseFetching` or `cancelFetching`, it returns a promise which resolves to `null`.
306+
*/
248307
async resumeFetching(...sources: ResourceSource[]) {
249308
const source = this.findActive(sources);
250309
await this.resume(source);
251310
},
252311

312+
/**
313+
* Cancels an ongoing/paused download of files.
314+
*
315+
* @param sources - The resource identifiers used when calling `fetch()`.
316+
* @returns A promise that resolves once the download is canceled.
317+
*/
253318
async cancelFetching(...sources: ResourceSource[]) {
254319
const source = this.findActive(sources);
255320
await this.cancel(source);
@@ -267,16 +332,32 @@ export const ExpoResourceFetcher: ExpoResourceFetcherInterface = {
267332
);
268333
},
269334

335+
/**
336+
* Lists all the downloaded files used by React Native ExecuTorch.
337+
*
338+
* @returns A promise, which resolves to an array of URIs for all the downloaded files.
339+
*/
270340
async listDownloadedFiles() {
271341
const files = await readDirectoryAsync(RNEDirectory);
272342
return files.map((file: string) => `${RNEDirectory}${file}`);
273343
},
274344

345+
/**
346+
* Lists all the downloaded models used by React Native ExecuTorch.
347+
*
348+
* @returns A promise, which resolves to an array of URIs for all the downloaded models.
349+
*/
275350
async listDownloadedModels() {
276351
const files = await this.listDownloadedFiles();
277352
return files.filter((file: string) => file.endsWith('.pte'));
278353
},
279354

355+
/**
356+
* Deletes downloaded resources from the local filesystem.
357+
*
358+
* @param sources - The resource identifiers used when calling `fetch`.
359+
* @returns A promise that resolves once all specified resources have been removed.
360+
*/
280361
async deleteResources(...sources: ResourceSource[]) {
281362
for (const source of sources) {
282363
const filename = ResourceFetcherUtils.getFilenameFromUri(
@@ -289,6 +370,12 @@ export const ExpoResourceFetcher: ExpoResourceFetcherInterface = {
289370
}
290371
},
291372

373+
/**
374+
* Fetches the info about files size. Works only for remote files.
375+
*
376+
* @param sources - The resource identifiers (URLs).
377+
* @returns A promise that resolves to combined size of files in bytes.
378+
*/
292379
async getFilesTotalSize(...sources: ResourceSource[]) {
293380
return (await ResourceFetcherUtils.getFilesSizes(sources)).totalLength;
294381
},
@@ -454,6 +541,18 @@ export const ExpoResourceFetcher: ExpoResourceFetcherInterface = {
454541
return ResourceFetcherUtils.removeFilePrefix(sourceExtended.fileUri);
455542
},
456543

544+
/**
545+
* Reads the contents of a file as a string.
546+
*
547+
* @param path - Absolute file path or file URI to read.
548+
* @returns A promise that resolves to the file contents as a string.
549+
*
550+
* @remarks
551+
* **REQUIRED**: Used internally for reading configuration files (e.g., tokenizer configs).
552+
*
553+
* **Technical Note**: Expo requires file URIs (file:// prefix), so this method
554+
* automatically converts plain paths to URIs if needed.
555+
*/
457556
async readAsString(path: string) {
458557
// Expo needs URI
459558
const uri = path.startsWith('file://') ? path : `file://${path}`;

0 commit comments

Comments
 (0)