import type { ViewModel, ViewModelInstance } from '../specs/ViewModel.nitro';
import type { RiveFile } from '../specs/RiveFile.nitro';
import type { RiveViewRef } from '../index';
import type { UseViewModelInstanceFileParams, UseViewModelInstanceViewModelParams, UseViewModelInstanceRefParams } from './useViewModelInstance';
export type UseViewModelInstanceAsyncResult = {
instance: ViewModelInstance;
isLoading: false;
error: null;
} | {
instance: null;
isLoading: false;
error: Error;
} | {
instance: null;
isLoading: false;
error: null;
} | {
instance: undefined;
isLoading: true;
error: null;
};
/**
* Result of {@link useViewModelInstanceAsync} when `required: true` is set.
* The `null` (error/absent) case is removed — instead the hook throws once the
* instance resolves to `null`, leaving only the ready and loading states.
*/
type UseViewModelInstanceAsyncRequiredResult = {
instance: ViewModelInstance;
isLoading: false;
error: null;
} | {
instance: undefined;
isLoading: true;
error: null;
};
/**
* Implementation behind `useViewModelInstance(source, { async: true })` — not
* exported publicly. Creates a ViewModelInstance using the non-deprecated
* `*Async` runtime APIs, resolving off the JS thread.
*
* Because creation is asynchronous, the instance is not available on the first
* render. Consumers should guard on the result:
*
* ```tsx
* const { instance, isLoading, error } = useViewModelInstanceAsync(riveFile);
* if (isLoading || !instance) return ;
* // ...
*
* ```
*
* A `null` source resolves to a terminal `{ instance: null, isLoading: false }`
* (not perpetual loading), while an `undefined` source keeps the hook loading.
* This mirrors {@link useRiveFile} (`riveFile: undefined` while loading,
* `null` on error) and `useRive` (`riveViewRef: undefined` until the view is
* ready, `null` on failure) — so when chaining, check the upstream hook's own
* `error`, since this hook cannot observe why the source is absent:
*
* ```tsx
* const { riveFile, error: fileError } = useRiveFile(source);
* const { instance, isLoading } = useViewModelInstanceAsync(riveFile);
* if (fileError) return {fileError.message};
* if (isLoading || !instance) return ;
* ```
*
* @param source - The RiveFile, ViewModel, or RiveViewRef to get an instance from
* @param params - Configuration for which instance to retrieve
* @returns An object with `instance`, `isLoading`, and `error` (discriminated union)
*
* @example
* ```tsx
* // From RiveFile (get default instance)
* const { riveFile } = useRiveFile(require('./animation.riv'));
* const { instance, isLoading } = useViewModelInstanceAsync(riveFile);
* ```
*
* @example
* ```tsx
* // From RiveFile with specific instance name
* const { instance } = useViewModelInstanceAsync(riveFile, { instanceName: 'PersonInstance' });
* ```
*
* @example
* ```tsx
* // From RiveFile with specific ViewModel name
* const { instance } = useViewModelInstanceAsync(riveFile, { viewModelName: 'Settings' });
* ```
*
* @example
* ```tsx
* // Create a new blank instance from ViewModel
* const viewModel = await file.viewModelByNameAsync('TodoItem');
* const { instance } = useViewModelInstanceAsync(viewModel, { useNew: true });
* ```
*
* @example
* ```tsx
* // With required: true (throws once resolved to null, use with Error Boundary).
* // Note: instance is still `undefined` while loading — guard on isLoading.
* const { instance, isLoading } = useViewModelInstanceAsync(riveFile, { required: true });
* ```
*
* @example
* ```tsx
* // With onInit to set initial values before the instance is exposed or bound
* const { instance } = useViewModelInstanceAsync(riveFile, {
* onInit: (vmi) => {
* vmi.numberProperty('count')?.set(initialCount);
* }
* });
* ```
*/
export declare function useViewModelInstanceAsync(source: RiveFile, params: UseViewModelInstanceFileParams & {
required: true;
}): UseViewModelInstanceAsyncRequiredResult;
export declare function useViewModelInstanceAsync(source: RiveFile | null | undefined, params?: UseViewModelInstanceFileParams): UseViewModelInstanceAsyncResult;
export declare function useViewModelInstanceAsync(source: ViewModel, params: UseViewModelInstanceViewModelParams & {
required: true;
}): UseViewModelInstanceAsyncRequiredResult;
export declare function useViewModelInstanceAsync(source: ViewModel | null | undefined, params?: UseViewModelInstanceViewModelParams): UseViewModelInstanceAsyncResult;
export declare function useViewModelInstanceAsync(source: RiveViewRef, params: UseViewModelInstanceRefParams & {
required: true;
}): UseViewModelInstanceAsyncRequiredResult;
export declare function useViewModelInstanceAsync(source: RiveViewRef | null | undefined, params?: UseViewModelInstanceRefParams): UseViewModelInstanceAsyncResult;
export {};
//# sourceMappingURL=useViewModelInstanceAsync.d.ts.map