MCPcopy Create free account
hub / github.com/async-library/react-async / reducer

Function reducer

packages/react-async/src/reducer.ts:73–118  ·  view source on GitHub ↗
(state: ReducerAsyncState<T>, action: AsyncAction<T>)

Source from the content-addressed store, hash-verified

71 } as ReducerAsyncState<T>)
72
73export const reducer = <T>(state: ReducerAsyncState<T>, action: AsyncAction<T>) => {
74 switch (action.type) {
75 case ActionTypes.start:
76 return {
77 ...state,
78 startedAt: new Date(),
79 finishedAt: undefined,
80 ...getStatusProps(StatusTypes.pending),
81 counter: action.meta.counter,
82 promise: action.meta.promise,
83 } as AsyncPending<T, ReducerBaseState<T>>
84 case ActionTypes.cancel:
85 return {
86 ...state,
87 startedAt: undefined,
88 finishedAt: undefined,
89 ...getStatusProps(getIdleStatus(state.error || state.data)),
90 counter: action.meta.counter,
91 promise: action.meta.promise,
92 } as
93 | AsyncInitial<T, ReducerBaseState<T>>
94 | AsyncFulfilled<T, ReducerBaseState<T>>
95 | AsyncRejected<T, ReducerBaseState<T>>
96 case ActionTypes.fulfill:
97 return {
98 ...state,
99 data: action.payload,
100 value: action.payload,
101 error: undefined,
102 finishedAt: new Date(),
103 ...getStatusProps(StatusTypes.fulfilled),
104 promise: action.meta.promise,
105 } as AsyncFulfilled<T, ReducerBaseState<T>>
106 case ActionTypes.reject:
107 return {
108 ...state,
109 error: action.payload,
110 value: action.payload,
111 finishedAt: new Date(),
112 ...getStatusProps(StatusTypes.rejected),
113 promise: action.meta.promise,
114 } as AsyncRejected<T, ReducerBaseState<T>>
115 default:
116 return state
117 }
118}
119
120export const dispatchMiddleware = <T>(
121 dispatch: (action: AsyncAction<T>, ...args: any[]) => void

Callers 2

constructorMethod · 0.90
useAsyncFunction · 0.90

Calls 2

getStatusPropsFunction · 0.90
getIdleStatusFunction · 0.90

Tested by

no test coverage detected