@towns-protocol/react-sdk
Version:
React Hooks for Towns Protocol SDK
59 lines • 2.56 kB
TypeScript
import { type Observable, type PersistedModel } from '@towns-protocol/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;
};
}
/**
* The value returned by the useObservable hook.
* If the observable is a PersistedModel, it will include error and status information.
*/
export type ObservableValue<T> = {
/** The data of the model. */
data: T;
/** If the model is in an error state, this will be the error. */
error: Error | undefined;
/** The status of the model. */
status: 'loading' | 'loaded' | 'error';
/** 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;
};
/**
* 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<Model, Data = Model extends PersistedModel<infer UnwrappedData> ? UnwrappedData : Model>(observable: Observable<Model>, config?: ObservableConfig.FromData<Model>): ObservableValue<Data>;
//# sourceMappingURL=useObservable.d.ts.map