You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This module provides functions to download and work with downloaded files stored in the application's document directory inside the `react-native-executorch/` directory. These utilities can help you manage your storage and clean up the downloaded files when they are no longer needed.
5
+
This page documents the resource fetcher APIs exposed by `react-native-executorch-expo-resource-fetcher` and `react-native-executorch-bare-resource-fetcher`. These adapters handle downloading and managing model files on disk.
6
+
7
+
:::info
8
+
All examples below use `ExpoResourceFetcher`. If you're on bare React Native, replace the import with:
- 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).
42
+
- 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).
33
43
- If the fetch was interrupted by `pauseFetching` or `cancelFetching`, it returns a promise which resolves to `null`.
34
44
35
45
:::info
36
-
If the resource is an object, it will be saved as a JSON file on disk.
46
+
If the resource is an object, it will be saved as a JSON file on disk.
- 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).
108
-
- If the fetch was again interrupted by `pauseFetching` or `cancelFetching`, it returns a promise which resolves to null.
117
+
- 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).
118
+
- If the fetch was again interrupted by `pauseFetching` or `cancelFetching`, it returns a promise which resolves to `null`.
109
119
110
120
:::info
111
-
The other way to resume paused resources is to simply call `fetch` again. However, `resumeFetching` is faster.
121
+
You can also resume a paused download by calling `fetch` again with the same sources. However, `resumeFetching` is faster as it resumes from where it left off.
If the built-in `BareResourceFetcher` and `ExpoResourceFetcher` don't fit your needs, for example, you want to use a different download library, or fetch from a private server you can implement your own adapter and plug it into React Native ExecuTorch.
6
+
7
+
## The ResourceFetcherAdapter interface
8
+
9
+
Your adapter must implement the `ResourceFetcherAdapter` interface exported from `react-native-executorch`. This interface defines every method which is used under the hood by React Native ExecuTorch:
10
+
11
+
```typescript
12
+
import {
13
+
ResourceFetcherAdapter,
14
+
ResourceSource,
15
+
} from'react-native-executorch';
16
+
17
+
interfaceResourceFetcherAdapter {
18
+
fetch(
19
+
callback: (downloadProgress:number) =>void,
20
+
...sources:ResourceSource[]
21
+
):Promise<string[] |null>;
22
+
23
+
readAsString(path:string):Promise<string>;
24
+
}
25
+
```
26
+
27
+
### `fetch`
28
+
29
+
This is the core method called by every model hook and module whenever it needs to resolve a model or resource to a local file path.
30
+
31
+
-`callback` — called with a progress value between `0` and `1` as downloads proceed. Called with `1` when complete.
32
+
-`...sources` — one or more `ResourceSource` values:
33
+
-`string` — a remote URL or an absolute local file path.
34
+
-`number` — a bundled asset reference from `require('./model.pte')`.
35
+
-`object` — an inline JS object (e.g. a tokenizer config) that should be JSON-serialized and written to disk. Your adapter is responsible for stringifying it and saving it as a file, then returning the local path. This allows callers to pass configs inline instead of hosting them at a URL.
36
+
-**Returns** an array of absolute local file paths (without `file://` prefix), one per source, in the same order. Return `null` if the fetch was intentionally interrupted (e.g. cancelled by the user).
37
+
38
+
### `readAsString`
39
+
40
+
Called internally to read configuration files (e.g. tokenizer configs) that were previously downloaded via `fetch`.
41
+
42
+
-`path` — absolute path to the file on disk.
43
+
-**Returns** the file contents as a UTF-8 string.
44
+
45
+
## Example
46
+
47
+
Here's a minimal custom adapter that downloads files using the browser `fetch` API (useful for testing or web targets):
0 commit comments