import { BehaviorSubject, Observable } from 'rxjs'; import { IProstoreSsrContext } from './IProstoreSsrContext'; import { BehaviorStore } from './BehaviorStore'; import { SubscriptionManager } from './SubscriptionManager'; export interface IRequestState { /** Request result */ data: Data | null; /** Whether the request is ongoing */ loading: boolean; /** Whether the request has been completed at least once */ loaded: boolean; /** If request errored out, holds the error */ error: any; /** Latest variables used for requesting the data */ variables: Vars | null; } export declare const initialRequestState: IRequestState; export declare type ISkipQueryFn = (vars: Vars) => Data | null | undefined; export declare type IUpdateDataFn = (data: Data | null, oldData: Data | null, params: { store: any; variables: Vars; options: any; }) => Data | null; export interface IVariablesSubjectType { variables: Vars; options?: Options; } export interface IRxRequestStoreParams { /** Initial data to be held in the store state as the request result */ initialData?: Data | null; /** * This function may be used to skip the request based on the variable values. * * It should return undefined if the request should go as usual. * If something else is returned, the request is skipped and the result of the function * is saved as the store state. */ skipQuery?: ISkipQueryFn; /** * This function may be used to customize the store state after the request is completed. * * It accepts new request result, old saved data and request params and should return * new data to be saved in the store state. * * Can be used for paginated requests (see example in the docs) */ updateData?: IUpdateDataFn; /** Global SsrContext which contains states of all stores */ ssrContext?: IProstoreSsrContext | null; /** Store identifier which is used to store data in the SsrContext */ ssrId?: string | null; /** Observable which provides variables and options to the request */ variables$?: Observable>; } export interface IRequestStoreOptions { /** Optional state overrides */ changeState?: Partial>; } export declare type IObservableData = Promise | Observable; export declare type IVariablesObservable = Observable> | undefined; export declare class RxRequestStore = any> extends BehaviorStore> { /** Identifies the store in the SsrContext */ ssrId?: string | null; /** SsrContext to be used in the SSR-enabled environments */ ssrContext?: IProstoreSsrContext | null; /** Subject which holds the variables Observable */ variables$$: BehaviorSubject>; /** Observable of the request state */ state$: BehaviorSubject>; /** Skip query callback. See docs for IRequestStoreParams */ protected skipQuery: ISkipQueryFn; /** Update data callback. See docs for IRequestStoreParams */ protected updateData: IUpdateDataFn; /** Indicates whether the request data was loaded from SsrContext */ protected loadedValueFromContext: boolean; /** Tracks all subscriptions during onSubscribe and cancels them in onUnsubscribe */ _sub: SubscriptionManager; constructor({ initialData, skipQuery, ssrId, updateData, ssrContext, variables$ }: IRxRequestStoreParams); get state(): IRequestState; set state(newValue: IRequestState); setVariables$(variables$: IVariablesObservable): void; onSubscribe(): void; onUnsubscribe(): void; projectVariables: (variables$: IVariablesObservable) => Observable>; projectRequest: ({ variables, options: optionsProp }: IVariablesSubjectType) => Observable>>; performSkippableRequest(variables: Vars, options: Options): IObservableData; performRequest(variables: Vars, options: Options): IObservableData; mapRequestResult: (variables: Vars, options: Options) => (data: Data) => IRequestState; handleError: (error: any) => Partial>; tryLoadContextState(): void; updateContextState(state?: IRequestState): void; registerSsrPromise(promise: Promise>): void; } export declare type GetRxRequestStoreVars = Store extends RxRequestStore ? Vars : never; export declare type GetRxRequestStoreData = Store extends RxRequestStore ? Data : never; export declare type GetRxRequestStoreOptions = Store extends RxRequestStore ? Options : never; export declare type GetRxRequestStoreState = IRequestState, GetRxRequestStoreData>;