From 6d3bcc65701752fb9b08191a1a31007d36a9d91f Mon Sep 17 00:00:00 2001 From: dilane3 Date: Tue, 3 Oct 2023 18:19:44 +0100 Subject: [PATCH 1/4] ADD: Adding new hook called useAllSignals --- dist/hooks/useAllSignals.d.ts | 1 + dist/hooks/useAllSignals.js | 14 ++++++++ dist/hooks/useAllSignals.js.map | 1 + dist/hooks/useAsyncActions.js | 58 +++++++------------------------ dist/hooks/useAsyncActions.js.map | 2 +- dist/index.d.ts | 23 ++++++------ dist/index.js | 4 ++- dist/index.js.map | 2 +- dist/interfaces/builder.d.ts | 17 +++++++-- dist/interfaces/builder.js | 13 +++++++ dist/interfaces/builder.js.map | 2 +- dist/providers/index.js | 14 +++----- dist/providers/index.js.map | 2 +- src/hooks/useAllSignals.tsx | 9 +++++ src/index.ts | 28 ++++++++------- src/providers/index.tsx | 2 +- 16 files changed, 105 insertions(+), 87 deletions(-) create mode 100644 dist/hooks/useAllSignals.d.ts create mode 100644 dist/hooks/useAllSignals.js create mode 100644 dist/hooks/useAllSignals.js.map create mode 100644 src/hooks/useAllSignals.tsx diff --git a/dist/hooks/useAllSignals.d.ts b/dist/hooks/useAllSignals.d.ts new file mode 100644 index 0000000..b872684 --- /dev/null +++ b/dist/hooks/useAllSignals.d.ts @@ -0,0 +1 @@ +export default function useAllSignals(): import("../contexts/types").GXSignalType[]; diff --git a/dist/hooks/useAllSignals.js b/dist/hooks/useAllSignals.js new file mode 100644 index 0000000..5f3704f --- /dev/null +++ b/dist/hooks/useAllSignals.js @@ -0,0 +1,14 @@ +"use strict"; +var __importDefault = (this && this.__importDefault) || function (mod) { + return (mod && mod.__esModule) ? mod : { "default": mod }; +}; +Object.defineProperty(exports, "__esModule", { value: true }); +const react_1 = require("react"); +const contexts_1 = __importDefault(require("../contexts")); +function useAllSignals() { + // Global state that manage all signals + const { signals } = (0, react_1.useContext)(contexts_1.default); + return signals; +} +exports.default = useAllSignals; +//# sourceMappingURL=useAllSignals.js.map \ No newline at end of file diff --git a/dist/hooks/useAllSignals.js.map b/dist/hooks/useAllSignals.js.map new file mode 100644 index 0000000..296cc17 --- /dev/null +++ b/dist/hooks/useAllSignals.js.map @@ -0,0 +1 @@ +{"version":3,"file":"useAllSignals.js","sourceRoot":"","sources":["../../src/hooks/useAllSignals.tsx"],"names":[],"mappings":";;;;;AAAA,iCAAkC;AAClC,2DAAmC;AAEnC,SAAwB,aAAa;IACnC,uCAAuC;IACvC,MAAM,EAAE,OAAO,EAAE,GAAG,IAAA,kBAAU,EAAC,kBAAS,CAAC,CAAA;IAEzC,OAAO,OAAO,CAAA;AAChB,CAAC;AALD,gCAKC"} \ No newline at end of file diff --git a/dist/hooks/useAsyncActions.js b/dist/hooks/useAsyncActions.js index 3c5d7dd..9f09baa 100644 --- a/dist/hooks/useAsyncActions.js +++ b/dist/hooks/useAsyncActions.js @@ -21,14 +21,18 @@ const useAsyncActions = (signalName, ...actions) => { } // Get Global Context const { signals, asyncDispatch } = (0, react_1.useContext)(contexts_1.default); + // Async action callback const asyncActionCallback = (0, react_1.useCallback)((action, payload) => __awaiter(void 0, void 0, void 0, function* () { + // Dispatch pending action asyncDispatch({ type: action.type, isAsync: true, status: types_1.AsyncActionStatuses.PENDING, }); try { + // Execute async action const response = yield action.steps.asyncAction.handler(payload); + // Dispatch fulfilled action const data = asyncDispatch({ type: action.type, isAsync: true, @@ -41,6 +45,7 @@ const useAsyncActions = (signalName, ...actions) => { }; } catch (error) { + // Dispatch rejected action const data = asyncDispatch({ type: action.type, isAsync: true, @@ -61,7 +66,6 @@ const useAsyncActions = (signalName, ...actions) => { * @returns */ const handleGetAsyncActions = (signalName) => { - console.log("handleGetAsyncActions"); const signal = signals.find((signal) => signal.name === signalName); if (signal) { if (!actions || actions.length === 0) @@ -80,7 +84,11 @@ const useAsyncActions = (signalName, ...actions) => { else throw new Error(`Signal ${signalName} not found`); }; - const asyncActions = (0, react_1.useMemo)(() => { + /** + * Format async actions + * @returns + */ + const handleFormatAsyncActions = () => { // Get actions const nonFormattedActions = handleGetAsyncActions(signalName); // Formatted actions @@ -95,50 +103,8 @@ const useAsyncActions = (signalName, ...actions) => { ]; }); return Object.fromEntries(formattedActions); - // for (const action of nonFormattedActions) { - // // Get action name - // const actionName = action.type.split("/")[1]; - // console.log("OUiiiiiiiiiii"); - // formattedActions[actionName] = async (payload?: any) => { - // console.log("dedannnnnnnnnns"); - // asyncDispatch({ - // type: action.type, - // isAsync: true, - // status: AsyncActionStatuses.PENDING, - // }); - // try { - // const response = await ( - // action.steps as BuilderCase - // ).asyncAction.handler(payload); - // const data = asyncDispatch({ - // type: action.type, - // isAsync: true, - // status: AsyncActionStatuses.FULFILLED, - // payload: response, - // }); - // console.log("yoooooooooo"); - // return { - // data, - // status: AsyncActionStatuses.FULFILLED, - // }; - // } catch (error) { - // const data = asyncDispatch({ - // type: action.type, - // isAsync: true, - // status: AsyncActionStatuses.REJECTED, - // payload: error, - // }); - // return { - // data, - // error, - // status: AsyncActionStatuses.REJECTED, - // }; - // } - // }; - // } - // return formattedActions; - }, []); - return asyncActions; + }; + return handleFormatAsyncActions(); }; exports.default = useAsyncActions; //# sourceMappingURL=useAsyncActions.js.map \ No newline at end of file diff --git a/dist/hooks/useAsyncActions.js.map b/dist/hooks/useAsyncActions.js.map index 89402ad..2f42518 100644 --- a/dist/hooks/useAsyncActions.js.map +++ b/dist/hooks/useAsyncActions.js.map @@ -1 +1 @@ -{"version":3,"file":"useAsyncActions.js","sourceRoot":"","sources":["../../src/hooks/useAsyncActions.ts"],"names":[],"mappings":";;;;;;;;;;;;;;AAAA,iCAA8E;AAE9E,2DAAoC;AAEpC,4CAAuD;AAGvD,MAAM,eAAe,GAAG,CACtB,UAAkB,EAClB,GAAG,OAAiB,EACpB,EAAE;IACF,IAAI,CAAC,UAAU,IAAI,OAAO,UAAU,KAAK,QAAQ,EAAE;QACjD,MAAM,IAAI,KAAK,CACb,2DAA2D,CAC5D,CAAC;KACH;IAED,qBAAqB;IACrB,MAAM,EAAE,OAAO,EAAE,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QAC9C,uBAAuB;QACvB,kDAAkD;QAElD,kCAAkC;QAElC,8DAA8D;QAC9D,sCAAsC;QACtC,sBAAsB;QACtB,2BAA2B;QAC3B,uBAAuB;QACvB,6CAA6C;QAC7C,UAAU;QAEV,YAAY;QACZ,iCAAiC;QACjC,2CAA2C;QAC3C,wCAAwC;QAExC,qCAAqC;QACrC,6BAA6B;QAC7B,yBAAyB;QACzB,iDAAiD;QACjD,6BAA6B;QAC7B,YAAY;QAEZ,oCAAoC;QAEpC,iBAAiB;QACjB,gBAAgB;QAChB,iDAAiD;QACjD,WAAW;QACX,wBAAwB;QACxB,qCAAqC;QACrC,6BAA6B;QAC7B,yBAAyB;QACzB,gDAAgD;QAChD,0BAA0B;QAC1B,YAAY;QAEZ,iBAAiB;QACjB,gBAAgB;QAChB,iBAAiB;QACjB,gDAAgD;QAChD,WAAW;QACX,QAAQ;QACR,OAAO;QACP,IAAI;QAEJ,2BAA2B;IAC7B,CAAC,EAAE,EAAE,CAAC,CAAC;IAEP,OAAO,YAAY,CAAC;AACtB,CAAC,CAAC;AAEF,kBAAe,eAAe,CAAC"} \ No newline at end of file +{"version":3,"file":"useAsyncActions.js","sourceRoot":"","sources":["../../src/hooks/useAsyncActions.ts"],"names":[],"mappings":";;;;;;;;;;;;;;AAAA,iCAA8E;AAE9E,2DAAoC;AAEpC,4CAAuD;AAGvD,MAAM,eAAe,GAAG,CACtB,UAAkB,EAClB,GAAG,OAAiB,EACpB,EAAE;IACF,IAAI,CAAC,UAAU,IAAI,OAAO,UAAU,KAAK,QAAQ,EAAE;QACjD,MAAM,IAAI,KAAK,CACb,2DAA2D,CAC5D,CAAC;KACH;IAED,qBAAqB;IACrB,MAAM,EAAE,OAAO,EAAE,aAAa,EAAE,GAAG,IAAA,kBAAU,EAAC,kBAAS,CAAC,CAAC;IAEzD,wBAAwB;IACxB,MAAM,mBAAmB,GAAG,IAAA,mBAAW,EACrC,CAAO,MAA4B,EAAE,OAAa,EAAE,EAAE;QACpD,0BA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644 --- a/dist/index.d.ts +++ b/dist/index.d.ts @@ -1,12 +1,13 @@ -import GXProvider from './providers/index.js'; -import { AsyncActionStatuses } from './helpers/types.js'; -import createSignal from './helpers/createSignal.js'; -import createStore from './helpers/createStore.js'; -import createAsyncAction from './helpers/createAsyncAction.js'; -import useAction from './hooks/useAction.js'; -import useActions from './hooks/useActions.js'; -import useAsyncActions from './hooks/useAsyncActions.js'; -import useSignal from './hooks/useSignal.js'; -import useOperations from './hooks/useOperations.js'; +import GXProvider from "./providers/index.js"; +import { AsyncActionStatuses } from "./helpers/types.js"; +import createSignal from "./helpers/createSignal.js"; +import createStore from "./helpers/createStore.js"; +import createAsyncAction from "./helpers/createAsyncAction.js"; +import useAction from "./hooks/useAction.js"; +import useActions from "./hooks/useActions.js"; +import useAsyncActions from "./hooks/useAsyncActions.js"; +import useAllSignals from "./hooks/useAllSignals.js"; +import useSignal from "./hooks/useSignal.js"; +import useOperations from "./hooks/useOperations.js"; export default GXProvider; -export { createSignal, createStore, createAsyncAction, useAction, useActions, useAsyncActions, useSignal, useOperations, AsyncActionStatuses }; +export { createSignal, createStore, createAsyncAction, useAction, useActions, useAsyncActions, useAllSignals, useSignal, useOperations, AsyncActionStatuses, }; diff --git a/dist/index.js b/dist/index.js index 0833468..0d820f8 100644 --- a/dist/index.js +++ b/dist/index.js @@ -3,7 +3,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); -exports.AsyncActionStatuses = exports.useOperations = exports.useSignal = exports.useAsyncActions = exports.useActions = exports.useAction = exports.createAsyncAction = exports.createStore = exports.createSignal = void 0; +exports.AsyncActionStatuses = exports.useOperations = exports.useSignal = exports.useAllSignals = exports.useAsyncActions = exports.useActions = exports.useAction = exports.createAsyncAction = exports.createStore = exports.createSignal = void 0; // Provider const index_js_1 = __importDefault(require("./providers/index.js")); // Constants @@ -23,6 +23,8 @@ const useActions_js_1 = __importDefault(require("./hooks/useActions.js")); exports.useActions = useActions_js_1.default; const useAsyncActions_js_1 = __importDefault(require("./hooks/useAsyncActions.js")); exports.useAsyncActions = useAsyncActions_js_1.default; +const useAllSignals_js_1 = __importDefault(require("./hooks/useAllSignals.js")); +exports.useAllSignals = useAllSignals_js_1.default; const useSignal_js_1 = __importDefault(require("./hooks/useSignal.js")); exports.useSignal = useSignal_js_1.default; const useOperations_js_1 = __importDefault(require("./hooks/useOperations.js")); diff --git a/dist/index.js.map b/dist/index.js.map index 07bef4d..8ccf781 100644 --- a/dist/index.js.map +++ b/dist/index.js.map @@ -1 +1 @@ -{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;AAAA,WAAW;AACX,oEAA6C;AAE7C,YAAY;AACZ,iDAAwD;AAyBtD,oGAzBO,8BAAmB,OAyBP;AAvBrB,oBAAoB;AACpB,gFAAoD;AAclD,uBAdK,yBAAY,CAcL;AAbd,8EAAkD;AAchD,sBAdK,wBAAW,CAcL;AAbb,0FAA8D;AAc5D,4BAdK,8BAAiB,CAcL;AAZnB,QAAQ;AACR,wEAA4C;AAY1C,oBAZK,sBAAS,CAYL;AAXX,0EAA8C;AAY5C,qBAZK,uBAAU,CAYL;AAXZ,oFAAwD;AAYtD,0BAZK,4BAAe,CAYL;AAXjB,wEAA4C;AAY1C,oBAZK,sBAAS,CAYL;AAXX,gFAAoD;AAYlD,wBAZK,0BAAa,CAYL;AAVf,kBAAe,kBAAU,CAAA;AAczB,uGAAuG"} \ No newline at end of file +{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;AAAA,WAAW;AACX,oEAA8C;AAE9C,YAAY;AACZ,iDAAyD;AA2BvD,oGA3BO,8BAAmB,OA2BP;AAzBrB,oBAAoB;AACpB,gFAAqD;AAenD,uBAfK,yBAAY,CAeL;AAdd,8EAAmD;AAejD,sBAfK,wBAAW,CAeL;AAdb,0FAA+D;AAe7D,4BAfK,8BAAiB,CAeL;AAbnB,QAAQ;AACR,wEAA6C;AAa3C,oBAbK,sBAAS,CAaL;AAZX,0EAA+C;AAa7C,qBAbK,uBAAU,CAaL;AAZZ,oFAAyD;AAavD,0BAbK,4BAAe,CAaL;AAZjB,gFAAqD;AAanD,wBAbK,0BAAa,CAaL;AAZf,wEAA6C;AAa3C,oBAbK,sBAAS,CAaL;AAZX,gFAAqD;AAanD,wBAbK,0BAAa,CAaL;AAXf,kBAAe,kBAAU,CAAC;AAe1B,uGAAuG"} \ No newline at end of file diff --git a/dist/interfaces/builder.d.ts b/dist/interfaces/builder.d.ts index 27e9cfe..5a136aa 100644 --- a/dist/interfaces/builder.d.ts +++ b/dist/interfaces/builder.d.ts @@ -1,10 +1,23 @@ -import { type CreateAsyncActionReturnType } from '../helpers/types.js'; -import type IBuilderCase from './builderCase.js'; +import { type CreateAsyncActionReturnType } from "../helpers/types.js"; +import type IBuilderCase from "./builderCase.js"; export default interface IBuilder { use: (asyncAction: CreateAsyncActionReturnType) => IBuilderCase; } +/** + * @class Builder + * @implements IBuilder + * @description + * Builder class to initialize a new builder case instance and return it in order to + * chain the builder case methods, or onPending, onFulfilled, onRejected methods to define + * the async action steps. + */ export declare class Builder implements IBuilder { private readonly _builderCase; constructor(); + /** + * This method takes an async action object and assign it to the builder case instance + * @param asyncAction An async action object + * @returns IBuilderCase + */ use(asyncAction: CreateAsyncActionReturnType): IBuilderCase; } diff --git a/dist/interfaces/builder.js b/dist/interfaces/builder.js index e7f71c7..8a65c26 100644 --- a/dist/interfaces/builder.js +++ b/dist/interfaces/builder.js @@ -2,10 +2,23 @@ Object.defineProperty(exports, "__esModule", { value: true }); exports.Builder = void 0; const builderCase_js_1 = require("./builderCase.js"); +/** + * @class Builder + * @implements IBuilder + * @description + * Builder class to initialize a new builder case instance and return it in order to + * chain the builder case methods, or onPending, onFulfilled, onRejected methods to define + * the async action steps. + */ class Builder { constructor() { this._builderCase = new builderCase_js_1.BuilderCase(); } + /** + * This method takes an async action object and assign it to the builder case instance + * @param asyncAction An async action object + * @returns IBuilderCase + */ use(asyncAction) { this._builderCase.asyncAction = asyncAction; return this._builderCase; diff --git a/dist/interfaces/builder.js.map b/dist/interfaces/builder.js.map index d422a91..2b98a55 100644 --- a/dist/interfaces/builder.js.map +++ b/dist/interfaces/builder.js.map @@ -1 +1 @@ -{"version":3,"file":"builder.js","sourceRoot":"","sources":["../../src/interfaces/builder.ts"],"names":[],"mappings":";;;AAEA,qDAA8C;AAM9C,MAAa,OAAO;IAGlB;QACE,IAAI,CAAC,YAAY,GAAG,IAAI,4BAAW,EAAE,CAAA;IACvC,CAAC;IAED,GAAG,CAAE,WAAwC;QAC3C,IAAI,CAAC,YAAY,CAAC,WAAW,GAAG,WAAW,CAAA;QAE3C,OAAO,IAAI,CAAC,YAAY,CAAA;IAC1B,CAAC;CACF;AAZD,0BAYC"} \ No newline at end of file +{"version":3,"file":"builder.js","sourceRoot":"","sources":["../../src/interfaces/builder.ts"],"names":[],"mappings":";;;AAEA,qDAA+C;AAM/C;;;;;;;GAOG;AACH,MAAa,OAAO;IAGlB;QACE,IAAI,CAAC,YAAY,GAAG,IAAI,4BAAW,EAAE,CAAC;IACxC,CAAC;IAED;;;;OAIG;IACH,GAAG,CAAC,WAAwC;QAC1C,IAAI,CAAC,YAAY,CAAC,WAAW,GAAG,WAAW,CAAC;QAE5C,OAAO,IAAI,CAAC,YAAY,CAAC;IAC3B,CAAC;CACF;AAjBD,0BAiBC"} \ No newline at end of file diff --git a/dist/providers/index.js b/dist/providers/index.js index 019670a..664065e 100644 --- a/dist/providers/index.js +++ b/dist/providers/index.js @@ -4,24 +4,22 @@ var __importDefault = (this && this.__importDefault) || function (mod) { }; Object.defineProperty(exports, "__esModule", { value: true }); const jsx_runtime_1 = require("react/jsx-runtime"); -const react_1 = __importDefault(require("react")); -const react_2 = require("react"); +const react_1 = require("react"); const index_js_1 = __importDefault(require("../contexts/index.js")); const reducer_js_1 = __importDefault(require("./reducer.js")); function GXProvider({ children, store }) { // Global state that manage all signals - const [signals, dispatch] = (0, react_2.useReducer)(reducer_js_1.default, store.getSignals()); + const [signals, dispatch] = (0, react_1.useReducer)(reducer_js_1.default, store.getSignals()); // Wrap your dispatch function with useTransition - const [, startTransition] = (0, react_2.useTransition)(); + const [, startTransition] = (0, react_1.useTransition)(); // Your state management logic using useContext and useReducer const syncDispatch = (action) => { startTransition(() => { dispatch(action); }); }; - const asyncDispatch = (0, react_2.useCallback)((action) => { + const asyncDispatch = (0, react_1.useCallback)((action) => { const signalName = action.type.split("/")[0]; - console.log(action.status); const newState = signals.map(({ name, operations, actions, asyncActions, state: prevState }) => { let state = prevState; // Capture the target signal (a state and a bunch of async actions) from the array of signals. @@ -57,13 +55,11 @@ function GXProvider({ children, store }) { }); return signal.state; }, []); - // Ref - const asyncActionRef = react_1.default.useRef(asyncDispatch); // Context value const contextValue = { signals, dispatch: syncDispatch, - asyncDispatch: asyncActionRef.current, + asyncDispatch }; return ((0, jsx_runtime_1.jsx)(index_js_1.default.Provider, { value: contextValue, children: children })); } diff --git a/dist/providers/index.js.map b/dist/providers/index.js.map index a4d3b70..bbc356e 100644 --- a/dist/providers/index.js.map +++ b/dist/providers/index.js.map @@ -1 +1 @@ -{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/providers/index.tsx"],"names":[],"mappings":";;;;;;AAAA,kDAAyC;AACzC,iCAA+D;AAC/D,oEAA6C;AAE7C,8DAAqC;AAGrC,SAAwB,UAAU,CAAC,EAAE,QAAQ,EAAE,KAAK,EAAmB;IACrE,uCAAuC;IACvC,MAAM,CAAC,OAAO,EAAE,QAAQ,CAAC,GAAG,IAAA,kBAAU,EAAC,oBAAS,EAAE,KAAK,CAAC,UAAU,EAAE,CAAC,CAAC;IAEtE,iDAAiD;IACjD,MAAM,CAAC,EAAE,eAAe,CAAC,GAAG,IAAA,qBAAa,GAAE,CAAC;IAE5C,8DAA8D;IAC9D,MAAM,YAAY,GAAG,CAAC,MAAgB,EAAE,EAAE;QACxC,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AC;IAEP,gBAAgB;IAChB,MAAM,YAAY,GAAG;QACnB,OAAO;QACP,QAAQ,EAAE,YAAY;QACtB,aAAa;KACd,CAAC;IAEF,OAAO,CACL,uBAAC,kBAAS,CAAC,QAAQ,IAAC,KAAK,EAAE,YAAY,YAAG,QAAQ,GAAsB,CACzE,CAAC;AACJ,CAAC;AAtED,6BAsEC"} \ No newline at end of file diff --git a/src/hooks/useAllSignals.tsx b/src/hooks/useAllSignals.tsx new file mode 100644 index 0000000..05051fe --- /dev/null +++ b/src/hooks/useAllSignals.tsx @@ -0,0 +1,9 @@ +import { useContext } from "react" +import GXContext from "../contexts" + +export default function useAllSignals() { + // Global state that manage all signals + const { signals } = useContext(GXContext) + + return signals +} \ No newline at end of file diff --git a/src/index.ts b/src/index.ts index 0abfe1a..587e3d4 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,22 +1,23 @@ // Provider -import GXProvider from './providers/index.js' +import GXProvider from "./providers/index.js"; // Constants -import { AsyncActionStatuses } from './helpers/types.js' +import { AsyncActionStatuses } from "./helpers/types.js"; // Helpers functions -import createSignal from './helpers/createSignal.js' -import createStore from './helpers/createStore.js' -import createAsyncAction from './helpers/createAsyncAction.js' +import createSignal from "./helpers/createSignal.js"; +import createStore from "./helpers/createStore.js"; +import createAsyncAction from "./helpers/createAsyncAction.js"; // Hooks -import useAction from './hooks/useAction.js' -import useActions from './hooks/useActions.js' -import useAsyncActions from './hooks/useAsyncActions.js' -import useSignal from './hooks/useSignal.js' -import useOperations from './hooks/useOperations.js' +import useAction from "./hooks/useAction.js"; +import useActions from "./hooks/useActions.js"; +import useAsyncActions from "./hooks/useAsyncActions.js"; +import useAllSignals from "./hooks/useAllSignals.js"; +import useSignal from "./hooks/useSignal.js"; +import useOperations from "./hooks/useOperations.js"; -export default GXProvider +export default GXProvider; export { createSignal, @@ -25,9 +26,10 @@ export { useAction, useActions, useAsyncActions, + useAllSignals, useSignal, useOperations, - AsyncActionStatuses -} + AsyncActionStatuses, +}; // "build": "tsc && npx babel dist --out-dir cjs --extensions '.js' --source-maps inline --copy-files", diff --git a/src/providers/index.tsx b/src/providers/index.tsx index fcd892f..87fecec 100644 --- a/src/providers/index.tsx +++ b/src/providers/index.tsx @@ -62,7 +62,7 @@ export default function GXProvider({ children, store }: GXProviderProps) { payload: signal.state, }); - return signal.state; + return signal.state }, []); // Context value From f0cf3e02612deef7c3401fb926925722461b5bb9 Mon Sep 17 00:00:00 2001 From: dilane3 Date: Fri, 13 Oct 2023 01:48:48 +0100 Subject: [PATCH 2/4] ADD: Adding a new hook for getting all signals --- src/hooks/useAllSignals.ts | 9 +++++++++ 1 file changed, 9 insertions(+) create mode 100644 src/hooks/useAllSignals.ts diff --git a/src/hooks/useAllSignals.ts b/src/hooks/useAllSignals.ts new file mode 100644 index 0000000..05051fe --- /dev/null +++ b/src/hooks/useAllSignals.ts @@ -0,0 +1,9 @@ +import { useContext } from "react" +import GXContext from "../contexts" + +export default function useAllSignals() { + // Global state that manage all signals + const { signals } = useContext(GXContext) + + return signals +} \ No newline at end of file From 4fd7a8cb085b7ba12bcf0d5ba3bd207fd921135d Mon Sep 17 00:00:00 2001 From: dilane3 Date: Fri, 13 Oct 2023 01:50:01 +0100 Subject: [PATCH 3/4] Fix: Fix problem with strict mode in react for twice rendering while using async actions --- dist/helpers/types.d.ts | 10 ++++-- dist/helpers/types.js | 6 ++-- dist/helpers/types.js.map | 2 +- dist/hooks/types.d.ts | 2 +- dist/hooks/useAllSignals.js.map | 2 +- dist/hooks/useAsyncActions.d.ts | 2 +- dist/hooks/useAsyncActions.js | 23 ++++++++++++-- dist/hooks/useAsyncActions.js.map | 2 +- dist/index.d.ts | 3 +- dist/index.js.map | 2 +- dist/interfaces/builder.d.ts | 2 -- dist/interfaces/builder.js | 9 +++--- dist/interfaces/builder.js.map | 2 +- dist/providers/index.js.map | 2 +- dist/providers/reducer.js | 1 + dist/providers/reducer.js.map | 2 +- src/helpers/types.ts | 51 ++++++++++++++++++------------- src/hooks/types.ts | 2 +- src/hooks/useAllSignals.tsx | 9 ------ src/hooks/useAsyncActions.ts | 37 ++++++++++++++++++---- src/index.ts | 4 +++ src/interfaces/builder.ts | 13 +++----- src/providers/reducer.ts | 1 + 23 files changed, 117 insertions(+), 72 deletions(-) delete mode 100644 src/hooks/useAllSignals.tsx diff --git a/dist/helpers/types.d.ts b/dist/helpers/types.d.ts index 550e46e..c343f91 100644 --- a/dist/helpers/types.d.ts +++ b/dist/helpers/types.d.ts @@ -1,6 +1,6 @@ -import { type GXSignalType } from '../contexts/types.js'; -import { type Builder } from '../interfaces/builder.js'; -import type IBuilderCase from '../interfaces/builderCase.js'; +import { type GXSignalType } from "../contexts/types.js"; +import { type Builder } from "../interfaces/builder.js"; +import type IBuilderCase from "../interfaces/builderCase.js"; /** * Type of the create signal option function */ @@ -36,6 +36,10 @@ export interface CreateAsyncActionReturnType { rejected: AsyncActionStatusesType; handler: CreateAsyncActionProp; } +export type AsyncActionReturn = Promise<{ + data: T; + status: AsyncActionStatusesType; +}>; export declare const AsyncActionStatuses: { readonly PENDING: "PENDING"; readonly FULFILLED: "FULFILLED"; diff --git a/dist/helpers/types.js b/dist/helpers/types.js index 868b071..5cb4c54 100644 --- a/dist/helpers/types.js +++ b/dist/helpers/types.js @@ -2,8 +2,8 @@ Object.defineProperty(exports, "__esModule", { value: true }); exports.AsyncActionStatuses = void 0; exports.AsyncActionStatuses = { - PENDING: 'PENDING', - FULFILLED: 'FULFILLED', - REJECTED: 'REJECTED' + PENDING: "PENDING", + FULFILLED: "FULFILLED", + REJECTED: "REJECTED", }; //# sourceMappingURL=types.js.map \ No newline at end of file diff --git a/dist/helpers/types.js.map b/dist/helpers/types.js.map index 45d4f6c..2c56d44 100644 --- a/dist/helpers/types.js.map +++ b/dist/helpers/types.js.map @@ -1 +1 @@ -{"version":3,"file":"types.js","sourceRoot":"","sources":["../../src/helpers/types.ts"],"names":[],"mappings":";;;AAwDa,QAAA,mBAAmB,GAAG;IACjC,OAAO,EAAE,SAAS;IAClB,SAAS,EAAE,WAAW;IACtB,QAAQ,EAAE,UAAU;CACZ,CAAA"} \ No newline at end of file +{"version":3,"file":"types.js","sourceRoot":"","sources":["../../src/helpers/types.ts"],"names":[],"mappings":";;;AA+Da,QAAA,mBAAmB,GAAG;IACjC,OAAO,EAAE,SAAS;IAClB,SAAS,EAAE,WAAW;IACtB,QAAQ,EAAE,UAAU;CACZ,CAAC"} \ No newline at end of file diff --git a/dist/hooks/types.d.ts b/dist/hooks/types.d.ts index fef325d..426e038 100644 --- a/dist/hooks/types.d.ts +++ b/dist/hooks/types.d.ts @@ -2,6 +2,6 @@ import { type AsyncActionStatusesType } from '../helpers/types'; export type Actions = Record void>; export type AsyncActions = Record Promise<{ data: T; - status: Omit; + status: AsyncActionStatusesType; }>>; export type Operations

= Record P>; diff --git a/dist/hooks/useAllSignals.js.map b/dist/hooks/useAllSignals.js.map index 296cc17..0a80829 100644 --- a/dist/hooks/useAllSignals.js.map +++ b/dist/hooks/useAllSignals.js.map @@ -1 +1 @@ -{"version":3,"file":"useAllSignals.js","sourceRoot":"","sources":["../../src/hooks/useAllSignals.tsx"],"names":[],"mappings":";;;;;AAAA,iCAAkC;AAClC,2DAAmC;AAEnC,SAAwB,aAAa;IACnC,uCAAuC;IACvC,MAAM,EAAE,OAAO,EAAE,GAAG,IAAA,kBAAU,EAAC,kBAAS,CAAC,CAAA;IAEzC,OAAO,OAAO,CAAA;AAChB,CAAC;AALD,gCAKC"} \ No newline at end of file +{"version":3,"file":"useAllSignals.js","sourceRoot":"","sources":["../../src/hooks/useAllSignals.ts"],"names":[],"mappings":";;;;;AAAA,iCAAkC;AAClC,2DAAmC;AAEnC,SAAwB,aAAa;IACnC,uCAAuC;IACvC,MAAM,EAAE,OAAO,EAAE,GAAG,IAAA,kBAAU,EAAC,kBAAS,CAAC,CAAA;IAEzC,OAAO,OAAO,CAAA;AAChB,CAAC;AALD,gCAKC"} \ No newline at end of file diff --git a/dist/hooks/useAsyncActions.d.ts b/dist/hooks/useAsyncActions.d.ts index af51506..eee0917 100644 --- a/dist/hooks/useAsyncActions.d.ts +++ b/dist/hooks/useAsyncActions.d.ts @@ -1,3 +1,3 @@ import { type AsyncActions } from "./types"; -declare const useAsyncActions: >(signalName: string, ...actions: string[]) => P; +declare const useAsyncActions: >(signalName: string, ...actions: string[]) => P; export default useAsyncActions; diff --git a/dist/hooks/useAsyncActions.js b/dist/hooks/useAsyncActions.js index 9f09baa..81956ed 100644 --- a/dist/hooks/useAsyncActions.js +++ b/dist/hooks/useAsyncActions.js @@ -21,8 +21,21 @@ const useAsyncActions = (signalName, ...actions) => { } // Get Global Context const { signals, asyncDispatch } = (0, react_1.useContext)(contexts_1.default); + // Refs + // Define a ref to block the execution of async action callback twice + const isAsyncActionCallbackRunning = (0, react_1.useRef)({}); // Async action callback - const asyncActionCallback = (0, react_1.useCallback)((action, payload) => __awaiter(void 0, void 0, void 0, function* () { + const asyncActionCallback = (0, react_1.useRef)((action, payload) => __awaiter(void 0, void 0, void 0, function* () { + // Prevent the execution of async action callback twice + if (isAsyncActionCallbackRunning.current[action.type]) + return new Promise((resolve) => { + resolve({ + status: types_1.AsyncActionStatuses.PENDING, + data: null, + }); + }); + // Set the ref to true + isAsyncActionCallbackRunning.current[action.type] = true; // Dispatch pending action asyncDispatch({ type: action.type, @@ -58,7 +71,11 @@ const useAsyncActions = (signalName, ...actions) => { status: types_1.AsyncActionStatuses.REJECTED, }; } - }), []); + finally { + // Set the ref to false + isAsyncActionCallbackRunning.current[action.type] = false; + } + })); // Some handlers /** * Get async actions of a signal @@ -98,7 +115,7 @@ const useAsyncActions = (signalName, ...actions) => { return [ actionName, (payload) => __awaiter(void 0, void 0, void 0, function* () { - return asyncActionCallback(action, payload); + return asyncActionCallback.current(action, payload); }), ]; }); diff --git a/dist/hooks/useAsyncActions.js.map b/dist/hooks/useAsyncActions.js.map index 2f42518..a876f35 100644 --- a/dist/hooks/useAsyncActions.js.map +++ b/dist/hooks/useAsyncActions.js.map @@ -1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AmB,CAAC,GAAG,CAAC,CAAC,MAAM,EAAE,EAAE;YAC1D,kBAAkB;YAClB,MAAM,UAAU,GAAG,MAAM,CAAC,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,CAAC;YAE7C,OAAO;gBACL,UAAU;gBACV,CAAO,OAAa,EAAE,EAAE;oBACtB,OAAO,mBAAmB,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;gBAC9C,CAAC,CAAA;aACF,CAAC;QACJ,CAAC,CAAC,CAAC;QAEH,OAAO,MAAM,CAAC,WAAW,CAAC,gBAAgB,CAAC,CAAC;IAC9C,CAAC,CAAC;IAEF,OAAO,wBAAwB,EAAE,CAAC;AACpC,CAAC,CAAC;AAEF,kBAAe,eAAe,CAAC"} \ No newline at end of file +{"version":3,"file":"useAsyncActions.js","sourceRoot":"","sources":["../../src/hooks/useAsyncActions.ts"],"names":[],"mappings":";;;;;;;;;;;;;;AAAA,iCAOe;AAEf,2DAAoC;AAEpC,4CAAuD;AAGvD,MAAM,eAAe,GAAG,CACtB,UAAkB,EAClB,GAAG,OAAiB,EACpB,EAAE;IACF,IAAI,CAAC,UAAU,IAAI,OAAO,UAAU,KAAK,QAAQ,EAAE;QACjD,MAAM,IAAI,KAAK,CACb,2DAA2D,CAC5D,CAAC;KACH;IAED,qBAAqB;IACrB,MAAM,EAAE,OAAO,EAAE,aAAa,EAAE,GAAG,IAAA,kBAAU,EAAC,kBAAS,CAAC,CAAC;IAEzD,OAAO;IACP,qEAAqE;IACrE,MAAM,4BAA4B,GAAG,IAAA,cAAM,EAA2B,EAAE,CAAC,CAAC;IAE1E,wBAAwB;IACxB,MAAM,mBAAmB,GAAG,IAAA,cAAM,EAChC,CAAO,MAA4B,EAAE,OAAa,EAAE,EAAE;QACpD,uDAAuD;QACvD,IAAI,4BAA4B,CAAC,OAAO,CAAC,MAAM,CAAC,IAAI,CAAC;YACnD,OAAO,IAAI,OAAO,CAAC,CAAC,OAAO,EAAE,EAAE;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O,mBAAmB,CAAC,OAAO,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;gBACtD,CAAC,CAAA;aACF,CAAC;QACJ,CAAC,CAAC,CAAC;QAEH,OAAO,MAAM,CAAC,WAAW,CAAC,gBAAgB,CAAC,CAAC;IAC9C,CAAC,CAAC;IAEF,OAAO,wBAAwB,EAAE,CAAC;AACpC,CAAC,CAAC;AAEF,kBAAe,eAAe,CAAC"} \ No newline at end of file diff --git a/dist/index.d.ts b/dist/index.d.ts index cac7ffa..ac2a09f 100644 --- a/dist/index.d.ts +++ b/dist/index.d.ts @@ -1,5 +1,6 @@ import GXProvider from "./providers/index.js"; import { AsyncActionStatuses } from "./helpers/types.js"; +import { type AsyncActionReturn } from "./helpers/types.js"; import createSignal from "./helpers/createSignal.js"; import createStore from "./helpers/createStore.js"; import createAsyncAction from "./helpers/createAsyncAction.js"; @@ -10,4 +11,4 @@ import useAllSignals from "./hooks/useAllSignals.js"; import useSignal from "./hooks/useSignal.js"; import useOperations from "./hooks/useOperations.js"; export default GXProvider; -export { createSignal, createStore, createAsyncAction, useAction, useActions, useAsyncActions, useAllSignals, useSignal, useOperations, AsyncActionStatuses, }; +export { createSignal, createStore, createAsyncAction, useAction, useActions, useAsyncActions, useAllSignals, useSignal, useOperations, AsyncActionStatuses, AsyncActionReturn }; diff --git a/dist/index.js.map b/dist/index.js.map index 8ccf781..594aa1a 100644 --- a/dist/index.js.map +++ b/dist/index.js.map @@ -1 +1 @@ -{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;AAAA,WAAW;AACX,oEAA8C;AAE9C,YAAY;AACZ,iDAAyD;AA2BvD,oGA3BO,8BAAmB,OA2BP;AAzBrB,oBAAoB;AACpB,gFAAqD;AAenD,uBAfK,yBAAY,CAeL;AAdd,8EAAmD;AAejD,sBAfK,wBAAW,CAeL;AAdb,0FAA+D;AAe7D,4BAfK,8BAAiB,CAeL;AAbnB,QAAQ;AACR,wEAA6C;AAa3C,oBAbK,sBAAS,CAaL;AAZX,0EAA+C;AAa7C,qBAbK,uBAAU,CAaL;AAZZ,oFAAyD;AAavD,0BAbK,4BAAe,CAaL;AAZjB,gFAAqD;AAanD,wBAbK,0BAAa,CAaL;AAZf,wEAA6C;AAa3C,oBAbK,sBAAS,CAaL;AAZX,gFAAqD;AAanD,wBAbK,0BAAa,CAaL;AAXf,kBAAe,kBAAU,CAAC;AAe1B,uGAAuG"} \ No newline at end of file +{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;AAAA,WAAW;AACX,oEAA8C;AAE9C,YAAY;AACZ,iDAAyD;AA8BvD,oGA9BO,8BAAmB,OA8BP;AAzBrB,oBAAoB;AACpB,gFAAqD;AAenD,uBAfK,yBAAY,CAeL;AAdd,8EAAmD;AAejD,sBAfK,wBAAW,CAeL;AAdb,0FAA+D;AAe7D,4BAfK,8BAAiB,CAeL;AAbnB,QAAQ;AACR,wEAA6C;AAa3C,oBAbK,sBAAS,CAaL;AAZX,0EAA+C;AAa7C,qBAbK,uBAAU,CAaL;AAZZ,oFAAyD;AAavD,0BAbK,4BAAe,CAaL;AAZjB,gFAAqD;AAanD,wBAbK,0BAAa,CAaL;AAZf,wEAA6C;AAa3C,oBAbK,sBAAS,CAaL;AAZX,gFAAqD;AAanD,wBAbK,0BAAa,CAaL;AAXf,kBAAe,kBAAU,CAAC;AAgB1B,uGAAuG"} \ No newline at end of file diff --git a/dist/interfaces/builder.d.ts b/dist/interfaces/builder.d.ts index 5a136aa..5406f1b 100644 --- a/dist/interfaces/builder.d.ts +++ b/dist/interfaces/builder.d.ts @@ -12,8 +12,6 @@ export default interface IBuilder { * the async action steps. */ export declare class Builder implements IBuilder { - private readonly _builderCase; - constructor(); /** * This method takes an async action object and assign it to the builder case instance * @param asyncAction An async action object diff --git a/dist/interfaces/builder.js b/dist/interfaces/builder.js index 8a65c26..413b69c 100644 --- a/dist/interfaces/builder.js +++ b/dist/interfaces/builder.js @@ -11,17 +11,16 @@ const builderCase_js_1 = require("./builderCase.js"); * the async action steps. */ class Builder { - constructor() { - this._builderCase = new builderCase_js_1.BuilderCase(); - } /** * This method takes an async action object and assign it to the builder case instance * @param asyncAction An async action object * @returns IBuilderCase */ use(asyncAction) { - this._builderCase.asyncAction = asyncAction; - return this._builderCase; + const builderCase = new builderCase_js_1.BuilderCase(); + builderCase.asyncAction = asyncAction; + builderCase.cases = []; + return builderCase; } } exports.Builder = Builder; diff --git a/dist/interfaces/builder.js.map b/dist/interfaces/builder.js.map index 2b98a55..bd55edf 100644 --- a/dist/interfaces/builder.js.map +++ b/dist/interfaces/builder.js.map @@ -1 +1 @@ -{"version":3,"file":"builder.js","sourceRoot":"","sources":["../../src/interfaces/builder.ts"],"names":[],"mappings":";;;AAEA,qDAA+C;AAM/C;;;;;;;GAOG;AACH,MAAa,OAAO;IAGlB;QACE,IAAI,CAAC,YAAY,GAAG,IAAI,4BAAW,EAAE,CAAC;IACxC,CAAC;IAED;;;;OAIG;IACH,GAAG,CAAC,WAAwC;QAC1C,IAAI,CAAC,YAAY,CAAC,WAAW,GAAG,WAAW,CAAC;QAE5C,OAAO,IAAI,CAAC,YAAY,CAAC;IAC3B,CAAC;CACF;AAjBD,0BAiBC"} \ No newline at end of file +{"version":3,"file":"builder.js","sourceRoot":"","sources":["../../src/interfaces/builder.ts"],"names":[],"mappings":";;;AAEA,qDAA+C;AAM/C;;;;;;;GAOG;AACH,MAAa,OAAO;IAClB;;;;OAIG;IACH,GAAG,CAAC,WAAwC;QAC1C,MAAM,WAAW,GAAG,IAAI,4BAAW,EAAQ,CAAC;QAE5C,WAAW,CAAC,WAAW,GAAG,WAAW,CAAC;QACtC,WAAW,CAAC,KAAK,GAAG,EAAE,CAAC;QAEvB,OAAO,WAAW,CAAC;IACrB,CAAC;CACF;AAdD,0BAcC"} \ No newline at end of file diff --git a/dist/providers/index.js.map b/dist/providers/index.js.map index bbc356e..f3b4842 100644 --- a/dist/providers/index.js.map +++ b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AC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,MAAM,KAAK,MAAM,CAAC,MAAM,CAAC;iCACvC,OAAO,CAAC,KAAK,EAAE,MAAM,CAAC,OAAO,CAAC,CAAC;4BAClC,MAAM;yBACP;qBACF;iBACF;aACF;YAED,OAAO;gBACL,IAAI;gBACJ,UAAU;gBACV,KAAK;gBACL,OAAO;gBACP,YAAY;aACb,CAAC;QACJ,CAAC,CACF,CAAC;QAEF,0CAA0C;QAC1C,MAAM,MAAM,GAAG,QAAQ,CAAC,IAAI,CAAC,CAAC,MAAM,EAAE,EAAE,CAAC,MAAM,CAAC,IAAI,KAAK,UAAU,CAAC,CAAC;QAErE,QAAQ,CAAC;YACP,IAAI,EAAE,MAAM,CAAC,IAAI;YACjB,OAAO,EAAE,MAAM,CAAC,OAAO;YACvB,MAAM,EAAE,MAAM,CAAC,MAAM;YACrB,OAAO,EAAE,MAAM,CAAC,KAAK;SACtB,CAAC,CAAC;QAEH,OAAO,MAAM,CAAC,KAAK,CAAC;IACtB,CAAC,EAAE,EAAE,CAAC,CAAC;IAEP,gBAAgB;IAChB,MAAM,YAAY,GAAG;QACnB,OAAO;QACP,QAAQ,EAAE,YAAY;QACtB,aAAa;KACd,CAAC;IAEF,OAAO,CACL,uBAAC,kBAAS,CAAC,QAAQ,IAAC,KAAK,EAAE,YAAY,YAAG,QAAQ,GAAsB,CACzE,CAAC;AACJ,CAAC;AAtED,6BAsEC"} \ No newline at end of file +{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/providers/index.tsx"],"names":[],"mappings":";;;;;;AACA,iCAA+D;AAC/D,oEAA6C;AAE7C,8DAAqC;AAGrC,SAAwB,UAAU,CAAC,EAAE,QAAQ,EAAE,KAAK,EAAmB;IACrE,uCAAuC;IACvC,MAAM,CAAC,OAAO,EAAE,QAAQ,CAAC,GAAG,IAAA,kBAAU,EAAC,oBAAS,EAAE,KAAK,CAAC,UAAU,EAAE,CAAC,CAAC;IAEtE,iDAAiD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,CAAC,MAAM,EAAE,EAAE,CAAC,MAAM,CAAC,IAAI,KAAK,UAAU,CAAC,CAAC;QAErE,QAAQ,CAAC;YACP,IAAI,EAAE,MAAM,CAAC,IAAI;YACjB,OAAO,EAAE,MAAM,CAAC,OAAO;YACvB,MAAM,EAAE,MAAM,CAAC,MAAM;YACrB,OAAO,EAAE,MAAM,CAAC,KAAK;SACtB,CAAC,CAAC;QAEH,OAAO,MAAM,CAAC,KAAK,CAAA;IACrB,CAAC,EAAE,EAAE,CAAC,CAAC;IAEP,gBAAgB;IAChB,MAAM,YAAY,GAAG;QACnB,OAAO;QACP,QAAQ,EAAE,YAAY;QACtB,aAAa;KACd,CAAC;IAEF,OAAO,CACL,uBAAC,kBAAS,CAAC,QAAQ,IAAC,KAAK,EAAE,YAAY,YAAG,QAAQ,GAAsB,CACzE,CAAC;AACJ,CAAC;AAtED,6BAsEC"} \ No newline at end of file diff --git a/dist/providers/reducer.js b/dist/providers/reducer.js index 2954e2b..94535ae 100644 --- a/dist/providers/reducer.js +++ b/dist/providers/reducer.js @@ -19,6 +19,7 @@ const gxReducer = (signals, action) => { } } else { + console.log("youpiiiiiiiiii"); state = action.payload; } } diff --git a/dist/providers/reducer.js.map b/dist/providers/reducer.js.map index ed5747e..f66c003 100644 --- a/dist/providers/reducer.js.map +++ b/dist/providers/reducer.js.map @@ -1 +1 @@ -{"version":3,"file":"reducer.js","sourceRoot":"","sources":["../../src/providers/reducer.ts"],"names":[],"mappings":";;AAGA,MAAM,SAAS,GAAG,CAChB,OAAuB,EACvB,MAAgB,EACA,EAAE;IAClB,MAAM,UAAU,GAAG,MAAM,CAAC,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,CAAA;IAE5C,4DAA4D;IAC5D,qDAAqD;IACrD,OAAO,OAAO,CAAC,GAAG,CAChB,CAAC,EAAE,IAAI,EAAE,UAAU,EAAE,OAAO,EAAE,YAAY,EAAE,KAAK,EAAE,SAAS,EAAE,EAAE,EAAE;QAChE,IAAI,KAAK,GAAG,SAAS,CAAA;QAErB,wFAAwF;QACxF,mEAAmE;QACnE,8CAA8C;QAC9C,IAAI,IAAI,KAAK,UAAU,EAAE;YACvB,IAAI,CAAC,MAAM,CAAC,OAAO,EAAE;gBACnB,KAAK,MAAM,EAAE,IAAI,EAAE,OAAO,EAAE,IAAI,OAAO,EAAE;oBACvC,IAAI,IAAI,KAAK,MAAM,CAAC,IAAI,EAAE;wBACxB,KAAK,GAAG,OAAO,CAAC,SAAS,EAAE,MAAM,CAAC,OAAO,CAAC,CAAA;wBAC1C,MAAK;qBACN;iBACF;aACF;iBAAM;gBACL,KAAK,GAAG,MAAM,CAAC,OAAO,CAAA;aACvB;SACF;QAED,OAAO;YACL,IAAI;YACJ,UAAU;YACV,KAAK;YACL,OAAO;YACP,YAAY;SACb,CAAA;IACH,CAAC,CACF,CAAA;AACH,CAAC,CAAA;AAED,kBAAe,SAAS,CAAA"} \ No newline at end of file +{"version":3,"file":"reducer.js","sourceRoot":"","sources":["../../src/providers/reducer.ts"],"names":[],"mappings":";;AAGA,MAAM,SAAS,GAAG,CAChB,OAAuB,EACvB,MAAgB,EACA,EAAE;IAClB,MAAM,UAAU,GAAG,MAAM,CAAC,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,CAAA;IAE5C,4DAA4D;IAC5D,qDAAqD;IACrD,OAAO,OAAO,CAAC,GAAG,CAChB,CAAC,EAAE,IAAI,EAAE,UAAU,EAAE,OAAO,EAAE,YAAY,EAAE,KAAK,EAAE,SAAS,EAAE,EAAE,EAAE;QAChE,IAAI,KAAK,GAAG,SAAS,CAAA;QAErB,wFAAwF;QACxF,mEAAmE;QACnE,8CAA8C;QAC9C,IAAI,IAAI,KAAK,UAAU,EAAE;YACvB,IAAI,CAAC,MAAM,CAAC,OAAO,EAAE;gBACnB,KAAK,MAAM,EAAE,IAAI,EAAE,OAAO,EAAE,IAAI,OAAO,EAAE;oBACvC,IAAI,IAAI,KAAK,MAAM,CAAC,IAAI,EAAE;wBACxB,KAAK,GAAG,OAAO,CAAC,SAAS,EAAE,MAAM,CAAC,OAAO,CAAC,CAAA;wBAC1C,MAAK;qBACN;iBACF;aACF;iBAAM;gBACL,OAAO,CAAC,GAAG,CAAC,gBAAgB,CAAC,CAAA;gBAC7B,KAAK,GAAG,MAAM,CAAC,OAAO,CAAA;aACvB;SACF;QAED,OAAO;YACL,IAAI;YACJ,UAAU;YACV,KAAK;YACL,OAAO;YACP,YAAY;SACb,CAAA;IACH,CAAC,CACF,CAAA;AACH,CAAC,CAAA;AAED,kBAAe,SAAS,CAAA"} \ No newline at end of file diff --git a/src/helpers/types.ts b/src/helpers/types.ts index 433cb00..0a505be 100644 --- a/src/helpers/types.ts +++ b/src/helpers/types.ts @@ -1,25 +1,25 @@ -import { type GXSignalType } from '../contexts/types.js' -import { type Builder } from '../interfaces/builder.js' -import type IBuilderCase from '../interfaces/builderCase.js' +import { type GXSignalType } from "../contexts/types.js"; +import { type Builder } from "../interfaces/builder.js"; +import type IBuilderCase from "../interfaces/builderCase.js"; /** * Type of the create signal option function */ export interface CreateSignalOptionType { // Name of the signal - name: string + name: string; // State of the signal - state: T + state: T; // Actions of the signal - actions?: Action + actions?: Action; // Operations of the signal - operations?: Operation + operations?: Operation; // Async actions of the signal - asyncActions?: AsyncAction + asyncActions?: AsyncAction; } /** @@ -27,38 +27,45 @@ export interface CreateSignalOptionType { */ export interface CreateStoreType { // Function that return the list of signals - getSignals: () => GXSignalType[] + getSignals: () => GXSignalType[]; } /** * Type of Action */ -export type Action = Record T> +export type Action = Record T>; /** * Type of Operation */ -export type Operation = Record any> +export type Operation = Record any>; /** * Type of Async Action */ -export type AsyncAction = (builder: Builder) => Record> +export type AsyncAction = ( + builder: Builder +) => Record>; -export type CreateAsyncActionProp = (payload?: any) => Promise +export type CreateAsyncActionProp = (payload?: any) => Promise; export interface CreateAsyncActionReturnType { - pending: AsyncActionStatusesType - fulfilled: AsyncActionStatusesType - rejected: AsyncActionStatusesType - handler: CreateAsyncActionProp + pending: AsyncActionStatusesType; + fulfilled: AsyncActionStatusesType; + rejected: AsyncActionStatusesType; + handler: CreateAsyncActionProp; } +export type AsyncActionReturn = Promise<{ + data: T; + status: AsyncActionStatusesType; +}>; + export const AsyncActionStatuses = { - PENDING: 'PENDING', - FULFILLED: 'FULFILLED', - REJECTED: 'REJECTED' -} as const + PENDING: "PENDING", + FULFILLED: "FULFILLED", + REJECTED: "REJECTED", +} as const; export type AsyncActionStatusesType = - (typeof AsyncActionStatuses)[keyof typeof AsyncActionStatuses] + (typeof AsyncActionStatuses)[keyof typeof AsyncActionStatuses]; diff --git a/src/hooks/types.ts b/src/hooks/types.ts index bb37ebf..cd6e0ff 100644 --- a/src/hooks/types.ts +++ b/src/hooks/types.ts @@ -4,7 +4,7 @@ export type Actions = Record void> export type AsyncActions = Record Promise<{ data: T - status: Omit + status: AsyncActionStatusesType }>> export type Operations

= Record P> diff --git a/src/hooks/useAllSignals.tsx b/src/hooks/useAllSignals.tsx deleted file mode 100644 index 05051fe..0000000 --- a/src/hooks/useAllSignals.tsx +++ /dev/null @@ -1,9 +0,0 @@ -import { useContext } from "react" -import GXContext from "../contexts" - -export default function useAllSignals() { - // Global state that manage all signals - const { signals } = useContext(GXContext) - - return signals -} \ No newline at end of file diff --git a/src/hooks/useAsyncActions.ts b/src/hooks/useAsyncActions.ts index d2bc3af..29a4812 100644 --- a/src/hooks/useAsyncActions.ts +++ b/src/hooks/useAsyncActions.ts @@ -1,11 +1,18 @@ -import { useCallback, useContext, useEffect, useMemo, useState } from "react"; +import { + useCallback, + useContext, + useEffect, + useMemo, + useRef, + useState, +} from "react"; import { type AsyncActions } from "./types"; import GXContext from "../contexts"; import { type GXAsyncActionType } from "../contexts/types"; import { AsyncActionStatuses } from "../helpers/types"; import { BuilderCase } from "../interfaces/builderCase"; -const useAsyncActions = >( +const useAsyncActions = >( signalName: string, ...actions: string[] ) => { @@ -18,9 +25,25 @@ const useAsyncActions = >( // Get Global Context const { signals, asyncDispatch } = useContext(GXContext); + // Refs + // Define a ref to block the execution of async action callback twice + const isAsyncActionCallbackRunning = useRef<{[key: string]: Boolean}>({}); + // Async action callback - const asyncActionCallback = useCallback( + const asyncActionCallback = useRef( async (action: GXAsyncActionType, payload?: any) => { + // Prevent the execution of async action callback twice + if (isAsyncActionCallbackRunning.current[action.type]) + return new Promise((resolve) => { + resolve({ + status: AsyncActionStatuses.PENDING, + data: null, + }); + }); + + // Set the ref to true + isAsyncActionCallbackRunning.current[action.type] = true; + // Dispatch pending action asyncDispatch({ type: action.type, @@ -60,9 +83,11 @@ const useAsyncActions = >( error, status: AsyncActionStatuses.REJECTED, }; + } finally { + // Set the ref to false + isAsyncActionCallbackRunning.current[action.type] = false; } - }, - [] + } ); // Some handlers @@ -111,7 +136,7 @@ const useAsyncActions = >( return [ actionName, async (payload?: any) => { - return asyncActionCallback(action, payload); + return asyncActionCallback.current(action, payload); }, ]; }); diff --git a/src/index.ts b/src/index.ts index 587e3d4..794248a 100644 --- a/src/index.ts +++ b/src/index.ts @@ -4,6 +4,9 @@ import GXProvider from "./providers/index.js"; // Constants import { AsyncActionStatuses } from "./helpers/types.js"; +// Types +import { type AsyncActionReturn } from "./helpers/types.js"; + // Helpers functions import createSignal from "./helpers/createSignal.js"; import createStore from "./helpers/createStore.js"; @@ -30,6 +33,7 @@ export { useSignal, useOperations, AsyncActionStatuses, + AsyncActionReturn }; // "build": "tsc && npx babel dist --out-dir cjs --extensions '.js' --source-maps inline --copy-files", diff --git a/src/interfaces/builder.ts b/src/interfaces/builder.ts index c944881..dd2cd56 100644 --- a/src/interfaces/builder.ts +++ b/src/interfaces/builder.ts @@ -15,20 +15,17 @@ export default interface IBuilder { * the async action steps. */ export class Builder implements IBuilder { - private readonly _builderCase: BuilderCase; - - constructor() { - this._builderCase = new BuilderCase(); - } - /** * This method takes an async action object and assign it to the builder case instance * @param asyncAction An async action object * @returns IBuilderCase */ use(asyncAction: CreateAsyncActionReturnType): IBuilderCase { - this._builderCase.asyncAction = asyncAction; + const builderCase = new BuilderCase(); + + builderCase.asyncAction = asyncAction; + builderCase.cases = []; - return this._builderCase; + return builderCase; } } diff --git a/src/providers/reducer.ts b/src/providers/reducer.ts index 2e074a9..06763a0 100644 --- a/src/providers/reducer.ts +++ b/src/providers/reducer.ts @@ -25,6 +25,7 @@ const gxReducer = ( } } } else { + console.log("youpiiiiiiiiii") state = action.payload } } From bc936552e9bb81e093dde786cf21c7033642cc7b Mon Sep 17 00:00:00 2001 From: dilane3 Date: Fri, 13 Oct 2023 02:11:34 +0100 Subject: [PATCH 4/4] remove logs --- src/providers/reducer.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/src/providers/reducer.ts b/src/providers/reducer.ts index 06763a0..2e074a9 100644 --- a/src/providers/reducer.ts +++ b/src/providers/reducer.ts @@ -25,7 +25,6 @@ const gxReducer = ( } } } else { - console.log("youpiiiiiiiiii") state = action.payload } }