Skip to content

ChimericAsyncReducer

Available from both @chimeric/react-query and @chimeric/rtk-query.

Combines queries, eager async, and sync services into a ChimericEagerAsync. The resulting object auto-executes reactively when parameters change and provides aggregated loading/error states.

import { ChimericAsyncReducer } from '@chimeric/react-query'; // or '@chimeric/rtk-query'
const getDashboard = ChimericAsyncReducer<{ userId: string }>().build({
serviceList: [
{ service: getUser, getParams: (p) => ({ id: p.userId }) },
{ service: getUserPosts, getParams: (p) => ({ userId: p.userId }) },
{ service: getNotificationCount },
],
reducer: ([user, posts, notifications]) => ({
user,
posts,
notifications,
totalPosts: posts.length,
}),
initialValueReducer: ([user, posts, notifications]) => ({
user: user ?? null,
posts: posts ?? [],
notifications: notifications ?? 0,
totalPosts: 0,
}),
});
// Idiomatic
const dashboard = await getDashboard({ userId: '123' });
// Reactive
const { data, isPending, isError } = getDashboard.useHook({ userId: '123' });
PropertyTypeDescription
serviceListArray of { service, getParams?, ... }Up to 10 services (Query, EagerAsync, or Sync)
reducer(results[], params) => TResultCombines all service results when all are loaded
initialValueReducer(resultsWithUndefined[], params) => TResultOptional. Called while any service is still pending.

Each service config:

PropertyTypeDescription
serviceChimericQuery | ChimericEagerAsync | ChimericSyncThe service to include
getParams(serviceParams) => TParamsOptional param transformer
getIdiomaticOptions() => AllIdiomaticOptionsOptional. Idiomatic-path options for queries.
getReactiveOptions() => AllReactiveOptionsOptional. Reactive-path options for queries.

The reactive path aggregates state across all services:

StateRule
isIdleAll services are idle
isPendingAny service is pending
isSuccessAll services succeeded
isErrorAny service errored
errorFirst error encountered

IdiomaticAsyncReducer and ReactiveAsyncReducer are also available for single-path usage.