@river-build/react-sdk
Version:
React Hooks for River SDK
71 lines • 2.94 kB
TypeScript
import { type Observable, type PersistedModel } from '@river-build/sdk';
export declare namespace ObservableConfig {
/**
* Configuration options for an observable.
* It can be used to configure the behavior of the `useObservable` hook.
*/
type FromObservable<Observable_> = Observable_ extends Observable<infer Data> ? FromData<Data> : never;
/**
* Create configuration options for an observable from the data type.
* It can be used to configure the behavior of the `useObservable` hook.
*/
type FromData<Data> = Data extends PersistedModel<infer UnwrappedData> ? {
/**
* Trigger the update immediately, without waiting for the first update.
* @defaultValue true
*/
fireImmediately?: boolean;
/** Callback function to be called when the data is updated. */
onUpdate?: (data: UnwrappedData) => void;
/** Callback function to be called when an error occurs. */
onError?: (error: Error) => void;
} : {
/**
* Trigger the update immediately, without waiting for the first update.
* @defaultValue true
*/
fireImmediately?: boolean;
/** Callback function to be called when the data is updated. */
onUpdate?: (data: Data) => void;
/** Callback function to be called when an error occurs. */
onError?: (error: Error) => void;
};
}
/**
* River SyncAgent models are wrapped in a PersistedModel when they are persisted.
* This type is used to extract the actual data from the model.
*/
type ObservableValue<Data> = Data extends PersistedModel<infer UnwrappedData> ? {
/** The data of the model. */
data: UnwrappedData;
/** If the model is in an error state, this will be the error. */
error: Error | undefined;
status: PersistedModel<Data>['status'];
/** True if the model is in a loading state. */
isLoading: boolean;
/** True if the model is in an error state. */
isError: boolean;
/** True if the data is loaded. */
isLoaded: boolean;
} : {
/** The data of the model. */
data: Data;
error: undefined;
/** The status of the model. For a non persisted model, this will be either `loading` or `loaded`. */
status: 'loading' | 'loaded';
/** True if the model is in a loading state. */
isLoading: boolean;
/** Non existent for a non persisted model. */
isError: false;
/** True if the data is loaded. */
isLoaded: boolean;
};
/**
* This hook subscribes to an observable and returns the value of the observable.
* @param observable - The observable to subscribe to.
* @param config - Configuration options for the observable.
* @returns The value of the observable.
*/
export declare function useObservable<T>(observable: Observable<T>, config?: ObservableConfig.FromData<T>): ObservableValue<T>;
export {};
//# sourceMappingURL=useObservable.d.ts.map