@equinor/fusion-query
Version:
29 lines (28 loc) • 2 kB
TypeScript
import { type Actions } from './actions';
import type { QueryClientState } from './types';
/**
* Creates a reducer function for managing the state of query client operations.
* The reducer updates the state based on actions dispatched related to query transactions.
*
* The state is an object where each key represents a unique transaction ID and its value
* is a `QueryClientRequest` object that contains details about the request, its execution status,
* any errors that occurred, and the results of the query execution.
*
* The reducer handles the following actions:
* - `request`: Initializes a new query in the state with the given transaction ID.
* The state is updated with a new `QueryClientRequest` object which includes the request details,
* an empty execution log, an empty error log, and sets the status to 'idle'.
* - `execute`: Marks a query as active by updating its status to 'active' and logs the execution time.
* - `execute.success`: Removes a successfully executed query from the state, indicating the query has completed.
* - `execute.failure`: Updates a query's status to 'failed' and logs the error information.
* - `error`: Removes a query from the state due to an irrecoverable error, effectively cleaning up its state.
* - `cancel`: Removes a query from the state if it's canceled, cleaning up any associated state.
*
* This reducer is created using a builder pattern provided by `makeReducer` from '@equinor/fusion-observable',
* allowing for a declarative approach to defining how state is updated in response to actions.
*
* @param initial An optional initial state for the reducer. If not provided, defaults to an empty object.
* @returns A reducer function tailored for managing the state of query client operations.
*/
export declare const createReducer: <TArgs = unknown>(initial?: QueryClientState<TArgs>) => import("@equinor/fusion-observable").ReducerWithInitialState<QueryClientState<TArgs>, Actions>;
export default createReducer;