UNPKG

ngrx-loading-state

Version:

NgRx Loading State consistently manages loading actions such as API fetches.

81 lines (80 loc) 4.12 kB
import { DefaultProjectorFn, MemoizedSelector } from '@ngrx/store'; import { NoIntersection } from '../utils'; import { IdLoadingActions } from './id-loading-state-actions'; import { IdFailureAction, IdLoadAction, IdLoadingStates, IdSuccessAction, WithIdLoadingStatesOnly } from './id-loading-state-types'; export declare function idLoad<LoadPayloadType extends NotIdLoadingAction<LoadPayloadType>>(): IdLoad<LoadPayloadType>; export declare function idSuccess<SuccessPayloadType extends NotIdSuccessAction<SuccessPayloadType>>(): IdSuccess<SuccessPayloadType>; export declare function idFailure<FailurePayloadType extends NotIdFailureAction<FailurePayloadType>>(): IdFailure<FailurePayloadType>; /** * Creates a set of IdLoadAction, IdSuccessAction, and IdFailureAction. Selectors and reducers are always bundled into * the same structure. * * The difference between IdLoadAction and LoadAction is that IdLoadAction is parameterized by an id field. So you can * display loading states for multiple items that are all loading in parallel. Typical use case is you have a list of items, * and you can issue load actions for each one, parameterized by an id of your own choosing, and observe the loading state * of each item, again parameterized by the id. * * @param type The "type" of the action. * @param _idLoad See usage example * @param _idSuccess See usage example * @param _idFailure See usage example * @returns An instance of LoadingActions class that bundles together actions, selectors and reducers. * @example * export const fetchItem = createIdLoadingActions( * 'Fetch Item', * // An id field is already included in each of LoadAction, SuccessAction, FailureAction * load<{}>(), * success<{ item: object }>(), * failure<{}>() * ); * * // Dispatch load action * const id = "123"; * this.store.dispatch(fetchItem.idLoad({ id })); * * // Using ngrx's createFeatureSelector to select the feature slice from global store. * const selectState = createFeatureSelector<SimpleState>(SIMPLE_FEATURE_KEY); * const selectLoadingStates = createLoadingStatesSelector(selectState); * * const fetchItemSelectors = fetchItem.createIdSelectors(selectLoadingStates); * * // Observe the loading state. * this.store.select(fetchItemSelectors.state(id)); * */ export declare function createIdLoadingActions<LoadPayloadType extends object, SuccessPayloadType extends object, FailurePayloadType extends object>(actionTypePrefix: string, _idLoad: IdLoad<LoadPayloadType>, _idSuccess: IdSuccess<SuccessPayloadType>, _idFailure: IdFailure<FailurePayloadType>): IdLoadingActions<LoadPayloadType, SuccessPayloadType, FailurePayloadType>; /** * * @param featureSelector Selector that selects the current feature slice of the store. * @returns Selector that selects the loadingStates field from the store * @example * // Using ngrx's createFeatureSelector to select the feature slice from global store. * const selectState = createFeatureSelector<SimpleState>(SIMPLE_FEATURE_KEY); * const selectLoadingStates = createLoadingStatesSelector(selectState); * * You can then use selectLoadingStates to compose other selectors. eg. * * export const fetchItem = createIdLoadingActions( * 'Fetch Item', * load<{}>(), * success<{ item: object }>(), * failure<{}>() * ); * * export const fetchItemSelectors = fetchItem.createSelectors(selectLoadingStates); * */ export declare function createIdLoadingStatesSelector<State extends WithIdLoadingStatesOnly>(featureSelector: MemoizedSelector<object, State, DefaultProjectorFn<State>>): MemoizedSelector<object, IdLoadingStates, DefaultProjectorFn<IdLoadingStates>>; declare class IdLoad<_LoadPayloadType> { type: 'ID_LOAD'; } declare class IdSuccess<_SuccessPayloadType> { type: 'ID_SUCCESS'; } declare class IdFailure<_FailurePayloadType> { type: 'ID_FAILURE'; } declare type NotIdLoadingAction<T> = NoIntersection<T, IdLoadAction>; declare type NotIdSuccessAction<T> = NoIntersection<T, IdSuccessAction>; declare type NotIdFailureAction<T> = NoIntersection<T, IdFailureAction>; export {};