Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 3fa427c

Browse files
committedOct 13, 2023
Rename AsyncActionType to AsyncActionReponse and update the schema of that type
1 parent 0535d41 commit 3fa427c

13 files changed

+58
-23
lines changed
 

‎dist/helpers/types.d.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,10 @@ export interface CreateAsyncActionReturnType {
3636
rejected: AsyncActionStatusesType;
3737
handler: CreateAsyncActionProp;
3838
}
39-
export type AsyncActionReturn<T = null> = Promise<{
39+
export type AsyncActionResponse<P = any, T = null> = Promise<{
40+
state: P;
4041
data: T;
42+
error: Error | null;
4143
status: AsyncActionStatusesType;
4244
}>;
4345
export declare const AsyncActionStatuses: {

‎dist/helpers/types.js.map

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎dist/hooks/types.d.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
import { type AsyncActionStatusesType } from '../helpers/types';
22
export type Actions = Record<string, (payload?: any) => void>;
33
export type AsyncActions<T> = Record<string, (payload?: any) => Promise<{
4-
data: T;
4+
state: T;
5+
data: any | null;
6+
error: Error | null;
57
status: AsyncActionStatusesType;
68
}>>;
79
export type Operations<P = any> = Record<string, (payload?: any) => P>;

‎dist/hooks/useAsyncActions.d.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
import { type AsyncActions } from "./types";
2-
declare const useAsyncActions: <T, P = AsyncActions<any>>(signalName: string, ...actions: string[]) => P;
2+
declare const useAsyncActions: <P = AsyncActions<any>>(signalName: string, ...actions: string[]) => P;
33
export default useAsyncActions;

‎dist/hooks/useAsyncActions.js

+15-3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎dist/hooks/useAsyncActions.js.map

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎dist/index.d.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import GXProvider from "./providers/index.js";
22
import { AsyncActionStatuses } from "./helpers/types.js";
3-
import { type AsyncActionReturn } from "./helpers/types.js";
3+
import { type AsyncActionResponse } from "./helpers/types.js";
44
import createSignal from "./helpers/createSignal.js";
55
import createStore from "./helpers/createStore.js";
66
import createAsyncAction from "./helpers/createAsyncAction.js";
@@ -11,4 +11,4 @@ import useAllSignals from "./hooks/useAllSignals.js";
1111
import useSignal from "./hooks/useSignal.js";
1212
import useOperations from "./hooks/useOperations.js";
1313
export default GXProvider;
14-
export { createSignal, createStore, createAsyncAction, useAction, useActions, useAsyncActions, useAllSignals, useSignal, useOperations, AsyncActionStatuses, AsyncActionReturn };
14+
export { createSignal, createStore, createAsyncAction, useAction, useActions, useAsyncActions, useAllSignals, useSignal, useOperations, AsyncActionStatuses, AsyncActionResponse };

‎dist/providers/reducer.js

-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎dist/providers/reducer.js.map

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎src/helpers/types.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,10 @@ export interface CreateAsyncActionReturnType {
5656
handler: CreateAsyncActionProp;
5757
}
5858

59-
export type AsyncActionReturn<T = null> = Promise<{
59+
export type AsyncActionResponse<P = any, T = null> = Promise<{
60+
state: P;
6061
data: T;
62+
error: Error | null;
6163
status: AsyncActionStatusesType;
6264
}>;
6365

‎src/hooks/types.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@ import { type AsyncActionStatusesType } from '../helpers/types'
33
export type Actions = Record<string, (payload?: any) => void>
44

55
export type AsyncActions<T> = Record<string, (payload?: any) => Promise<{
6-
data: T
6+
state: T,
7+
data: any | null,
8+
error: Error | null,
79
status: AsyncActionStatusesType
810
}>>
911

‎src/hooks/useAsyncActions.ts

+23-7
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import { type GXAsyncActionType } from "../contexts/types";
1212
import { AsyncActionStatuses } from "../helpers/types";
1313
import { BuilderCase } from "../interfaces/builderCase";
1414

15-
const useAsyncActions = <T, P = AsyncActions<any>>(
15+
const useAsyncActions = <P = AsyncActions<any>>(
1616
signalName: string,
1717
...actions: string[]
1818
) => {
@@ -25,18 +25,31 @@ const useAsyncActions = <T, P = AsyncActions<any>>(
2525
// Get Global Context
2626
const { signals, asyncDispatch } = useContext(GXContext);
2727

28+
// Get state from signals
29+
// Extract type from P generic type
30+
type StateType = P extends AsyncActions<infer U> ? U : any;
31+
32+
const state = useMemo<StateType>(() => {
33+
const signal = signals.find((signal) => signal.name === signalName);
34+
35+
if (signal) return signal.state;
36+
else throw new Error(`Signal ${signalName} not found`);
37+
}, [signals]);
38+
2839
// Refs
2940
// Define a ref to block the execution of async action callback twice
30-
const isAsyncActionCallbackRunning = useRef<{[key: string]: Boolean}>({});
41+
const isAsyncActionCallbackRunning = useRef<{ [key: string]: Boolean }>({});
3142

3243
// Async action callback
3344
const asyncActionCallback = useRef(
34-
async (action: GXAsyncActionType<T>, payload?: any) => {
45+
async (action: GXAsyncActionType<StateType>, payload?: any) => {
3546
// Prevent the execution of async action callback twice
3647
if (isAsyncActionCallbackRunning.current[action.type])
3748
return new Promise((resolve) => {
3849
resolve({
3950
status: AsyncActionStatuses.PENDING,
51+
state,
52+
error: null,
4053
data: null,
4154
});
4255
});
@@ -66,7 +79,9 @@ const useAsyncActions = <T, P = AsyncActions<any>>(
6679
});
6780

6881
return {
69-
data,
82+
state: data,
83+
data: response,
84+
error: null,
7085
status: AsyncActionStatuses.FULFILLED,
7186
};
7287
} catch (error) {
@@ -79,8 +94,9 @@ const useAsyncActions = <T, P = AsyncActions<any>>(
7994
});
8095

8196
return {
82-
data,
83-
error,
97+
state: data,
98+
data: null,
99+
error: new Error(error),
84100
status: AsyncActionStatuses.REJECTED,
85101
};
86102
} finally {
@@ -103,7 +119,7 @@ const useAsyncActions = <T, P = AsyncActions<any>>(
103119
if (signal) {
104120
if (!actions || actions.length === 0) return signal.asyncActions || [];
105121

106-
const filteredActions: Array<GXAsyncActionType<T>> = [];
122+
const filteredActions: Array<GXAsyncActionType<StateType>> = [];
107123

108124
for (const action of actions) {
109125
const actionName = `${signalName}/${action}`;

‎src/index.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import GXProvider from "./providers/index.js";
55
import { AsyncActionStatuses } from "./helpers/types.js";
66

77
// Types
8-
import { type AsyncActionReturn } from "./helpers/types.js";
8+
import { type AsyncActionResponse } from "./helpers/types.js";
99

1010
// Helpers functions
1111
import createSignal from "./helpers/createSignal.js";
@@ -33,7 +33,7 @@ export {
3333
useSignal,
3434
useOperations,
3535
AsyncActionStatuses,
36-
AsyncActionReturn
36+
AsyncActionResponse
3737
};
3838

3939
// "build": "tsc && npx babel dist --out-dir cjs --extensions '.js' --source-maps inline --copy-files",

0 commit comments

Comments
 (0)
Please sign in to comment.