react-native-executorch
Version:
An easy way to run AI models in React Native with ExecuTorch
26 lines (25 loc) • 1.02 kB
JavaScript
import { ResourceFetcher } from '../utils/ResourceFetcher';
import { getError } from '../Error';
export class BaseModule {
static nativeModule;
static onDownloadProgressCallback = () => { };
static async load(sources, ...loadArgs // this can be used in derived classes to pass extra args to load method
) {
try {
const paths = await ResourceFetcher.fetch(this.onDownloadProgressCallback, ...sources);
if (paths === null || paths.length < sources.length) {
throw new Error('Download interrupted.');
}
await this.nativeModule.loadModule(...paths, ...loadArgs);
}
catch (error) {
throw new Error(getError(error));
}
}
static async forward(..._args) {
throw new Error('forward method is not implemented in the BaseModule class. Please implement it in the derived class.');
}
static onDownloadProgress(callback) {
this.onDownloadProgressCallback = callback;
}
}