= {
+ [key: string]: (state: T, payload?: any) => any;
+};
diff --git a/dist/hooks/types.d.ts b/dist/hooks/types.d.ts
index 5ea0015..bbaef3b 100644
--- a/dist/hooks/types.d.ts
+++ b/dist/hooks/types.d.ts
@@ -1,3 +1,6 @@
export type Actions = {
- [key: string]: (payload: any) => void;
+ [key: string]: (payload?: any) => void;
+};
+export type Operations = {
+ [key: string]: (payload?: any) => P;
};
diff --git a/dist/hooks/useAction.d.ts b/dist/hooks/useAction.d.ts
index b7661c7..13187f8 100644
--- a/dist/hooks/useAction.d.ts
+++ b/dist/hooks/useAction.d.ts
@@ -1,2 +1,3 @@
-declare const useAction: (signalName: string, action: string) => (payload: any) => void;
+import { Actions } from "./types.js";
+declare const useAction: (signalName: string, action: string) => any;
export default useAction;
diff --git a/dist/hooks/useAction.js b/dist/hooks/useAction.js
index 3eaef77..8ef4ac6 100644
--- a/dist/hooks/useAction.js
+++ b/dist/hooks/useAction.js
@@ -3,13 +3,13 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
-const useActions_1 = __importDefault(require("./useActions"));
+const useActions_js_1 = __importDefault(require("./useActions.js"));
const useAction = (signalName, action) => {
if (!signalName || typeof signalName !== "string")
throw new Error("Provide a signalName as a first argument of useAction");
if (!action || typeof action !== "string")
throw new Error("Provide an action as second argument of useAction");
- const actions = (0, useActions_1.default)(signalName, action);
+ const actions = (0, useActions_js_1.default)(signalName, action);
return Object.values(actions)[0];
};
exports.default = useAction;
diff --git a/dist/hooks/useAction.js.map b/dist/hooks/useAction.js.map
index 043e7e7..7955ac0 100644
--- a/dist/hooks/useAction.js.map
+++ b/dist/hooks/useAction.js.map
@@ -1 +1 @@
-{"version":3,"file":"useAction.js","sourceRoot":"","sources":["../../src/hooks/useAction.ts"],"names":[],"mappings":";;;;;AAAA,8DAAsC;AAEtC,MAAM,SAAS,GAAG,CAAC,UAAkB,EAAE,MAAc,EAAE,EAAE;IACvD,IAAI,CAAC,UAAU,IAAI,OAAO,UAAU,KAAK,QAAQ;QAC/C,MAAM,IAAI,KAAK,CAAC,uDAAuD,CAAC,CAAC;IAE3E,IAAI,CAAC,MAAM,IAAI,OAAO,MAAM,KAAK,QAAQ;QACvC,MAAM,IAAI,KAAK,CAAC,mDAAmD,CAAC,CAAC;IAEvE,MAAM,OAAO,GAAG,IAAA,oBAAU,EAAC,UAAU,EAAE,MAAM,CAAC,CAAC;IAE/C,OAAO,MAAM,CAAC,MAAM,CAAC,OAAO,CAAC,CAAC,CAAC,CAAC,CAAC;AACnC,CAAC,CAAC;AAEF,kBAAe,SAAS,CAAC"}
\ No newline at end of file
+{"version":3,"file":"useAction.js","sourceRoot":"","sources":["../../src/hooks/useAction.ts"],"names":[],"mappings":";;;;;AACA,oEAAyC;AAEzC,MAAM,SAAS,GAAG,CAAc,UAAkB,EAAE,MAAc,EAAE,EAAE;IACpE,IAAI,CAAC,UAAU,IAAI,OAAO,UAAU,KAAK,QAAQ;QAC/C,MAAM,IAAI,KAAK,CAAC,uDAAuD,CAAC,CAAC;IAE3E,IAAI,CAAC,MAAM,IAAI,OAAO,MAAM,KAAK,QAAQ;QACvC,MAAM,IAAI,KAAK,CAAC,mDAAmD,CAAC,CAAC;IAEvE,MAAM,OAAO,GAAG,IAAA,uBAAU,EAAI,UAAU,EAAE,MAAM,CAAC,CAAC;IAElD,OAAO,MAAM,CAAC,MAAM,CAAC,OAAO,CAAC,CAAC,CAAC,CAAC,CAAC;AACnC,CAAC,CAAC;AAEF,kBAAe,SAAS,CAAC"}
\ No newline at end of file
diff --git a/dist/hooks/useActions.d.ts b/dist/hooks/useActions.d.ts
index 07ef067..3ebd9fa 100644
--- a/dist/hooks/useActions.d.ts
+++ b/dist/hooks/useActions.d.ts
@@ -1,3 +1,3 @@
-import { Actions } from "./types";
-declare const useActions: (signalName: string, ...actions: string[]) => Actions;
+import { Actions } from "./types.js";
+declare const useActions: (signalName: string, ...actions: string[]) => T;
export default useActions;
diff --git a/dist/hooks/useActions.js b/dist/hooks/useActions.js
index 70762cc..4b3c6eb 100644
--- a/dist/hooks/useActions.js
+++ b/dist/hooks/useActions.js
@@ -4,12 +4,12 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
};
Object.defineProperty(exports, "__esModule", { value: true });
const react_1 = require("react");
-const contexts_1 = __importDefault(require("../contexts"));
+const index_js_1 = __importDefault(require("../contexts/index.js"));
const useActions = (signalName, ...actions) => {
if (!signalName || typeof signalName !== "string")
throw new Error("Provide a signalName as first argument of useActions");
// Get Global Context
- const { signals, dispatch } = (0, react_1.useContext)(contexts_1.default);
+ const { signals, dispatch } = (0, react_1.useContext)(index_js_1.default);
// Some handlers
/**
* Get actions of a signal
diff --git a/dist/hooks/useActions.js.map b/dist/hooks/useActions.js.map
index 0fad069..8d5261f 100644
--- a/dist/hooks/useActions.js.map
+++ b/dist/hooks/useActions.js.map
@@ -1 +1 @@
-{"version":3,"file":"useActions.js","sourceRoot":"","sources":["../../src/hooks/useActions.ts"],"names":[],"mappings":";;;;;AAAA,iCAAmC;AACnC,2DAAoC;AAIpC,MAAM,UAAU,GAAG,CAAC,UAAkB,EAAE,GAAG,OAAiB,EAAE,EAAE;IAC9D,IAAI,CAAC,UAAU,IAAI,OAAO,UAAU,KAAK,QAAQ;QAC/C,MAAM,IAAI,KAAK,CAAC,sDAAsD,CAAC,CAAC;IAE1E,qBAAqB;IACrB,MAAM,EAAE,OAAO,EAAE,QAAQ,EAAE,GAAG,IAAA,kBAAU,EAAC,kBAAS,CAAC,CAAC;IAEpD,gBAAgB;IAEhB;;;;OAIG;IACH,MAAM,gBAAgB,GAAG,CAAC,UAAkB,EAAE,EAAE;QAC9C,MAAM,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC,MAAM,EAAE,EAAE,CAAC,MAAM,CAAC,IAAI,KAAK,UAAU,CAAC,CAAC;QAEpE,IAAI,MAAM,EAAE;YACV,IAAI,CAAC,OAAO,IAAI,OAA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M,mBAAmB,GAAG,GAAM,EAAE;QAClC,cAAc;QACd,MAAM,mBAAmB,GAAG,gBAAgB,CAAC,UAAU,CAAC,CAAC;QAEzD,oBAAoB;QACpB,MAAM,gBAAgB,GAAG,EAAS,CAAC;QAEnC,KAAK,MAAM,MAAM,IAAI,mBAAmB,EAAE;YACxC,kBAAkB;YAClB,MAAM,UAAU,GAAG,MAAM,CAAC,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,CAAC;YAE7C,gBAAgB,CAAC,UAAU,CAAC,GAAG,CAAC,OAAa,EAAE,EAAE;gBAC/C,QAAQ,CAAC;oBACP,IAAI,EAAE,MAAM,CAAC,IAAI;oBACjB,OAAO;iBACR,CAAC,CAAC;YACL,CAAC,CAAC;SACH;QAED,OAAO,gBAAgB,CAAC;IAC1B,CAAC,CAAC;IAEF,OAAO,mBAAmB,EAAE,CAAC;AAC/B,CAAC,CAAC;AAEF,kBAAe,UAAU,CAAC"}
\ No newline at end of file
diff --git a/dist/hooks/useOperations.d.ts b/dist/hooks/useOperations.d.ts
new file mode 100644
index 0000000..e7361e3
--- /dev/null
+++ b/dist/hooks/useOperations.d.ts
@@ -0,0 +1,3 @@
+import { Operations } from "./types.js";
+declare const useOperations: >(signalName: string) => T;
+export default useOperations;
diff --git a/dist/hooks/useOperations.js b/dist/hooks/useOperations.js
new file mode 100644
index 0000000..e4418b5
--- /dev/null
+++ b/dist/hooks/useOperations.js
@@ -0,0 +1,49 @@
+"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 index_js_1 = __importDefault(require("../contexts/index.js"));
+const useOperations = (signalName) => {
+ // Get Global Context
+ const { signals } = (0, react_1.useContext)(index_js_1.default);
+ if (!signalName || typeof signalName !== "string")
+ throw new Error("Provide a signalName as a first argument of useOperations");
+ const handleFormatOperations = () => {
+ const signal = signals.find((signal) => signal.name === signalName);
+ if (!signal)
+ throw new Error(`Signal ${signalName} not found`);
+ // Get actions
+ const nonFormattedOperations = signal.operations;
+ // Formatted actions
+ const formattedOperations = {};
+ for (const operation of nonFormattedOperations) {
+ // Get action name
+ const operationName = operation.type.split("/")[1];
+ formattedOperations[operationName] = (payload) => {
+ return operation.handler(signal.state, payload);
+ };
+ }
+ // return formattedOperations;
+ return formattedOperations;
+ };
+ return handleFormatOperations();
+};
+// Définir un type générique pour représenter une fonction
+// type FunctionType any> = T;
+// Définir un type générique pour représenter un objet contenant des fonctions
+// type FunctionObject = {
+// [K in keyof T]: T[K] extends Function ? FunctionType : never;
+// };
+// type FunctionObject = {
+// [K in keyof T]: T[K] extends (payload: infer Arg) => infer R
+// ? (payload: Arg) => R
+// : (payload?: any) => any;
+// };
+// // Utilisation d'une fonction auxiliaire pour extraire le type du second paramètre
+// type SecondParamType = T extends (a: any, b: infer P) => any ? P : any;
+// // Utilisation d'une fonction auxiliaire pour extraire le type de retour d'une fonction
+// type ReturnTypeFunc = T extends (...args: any[]) => infer R ? R : any;
+exports.default = useOperations;
+//# sourceMappingURL=useOperations.js.map
\ No newline at end of file
diff --git a/dist/hooks/useOperations.js.map b/dist/hooks/useOperations.js.map
new file mode 100644
index 0000000..a223b5a
--- /dev/null
+++ b/dist/hooks/useOperations.js.map
@@ -0,0 +1 @@
+{"version":3,"file":"useOperations.js","sourceRoot":"","sources":["../../src/hooks/useOperations.ts"],"names":[],"mappings":";;;;;AAAA,iCAAmC;AACnC,oEAA6C;AAG7C,MAAM,aAAa,GAAG,CAAiB,UAAkB,EAAE,EAAE;IAC3D,qBAAqB;IACrB,MAAM,EAAE,OAAO,EAAE,GAAG,IAAA,kBAAU,EAAC,kBAAS,CAAC,CAAC;IAE1C,IAAI,CAAC,UAAU,IAAI,OAAO,UAAU,KAAK,QAAQ;QAC/C,MAAM,IAAI,KAAK,CACb,2DAA2D,CAC5D,CAAC;IAEJ,MAAM,sBAAsB,GAAG,GAAM,EAAE;QACrC,MAAM,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC,MAAM,EAAE,EAAE,CAAC,MAAM,CAAC,IAAI,KAAK,UAAU,CAAC,CAAC;QAEpE,IAAI,CAAC,MAAM;YAAE,MAAM,IAAI,KAAK,CAAC,UAAU,UAAU,YAAY,CAAC,CAAC;QAE/D,cAAc;QACd,MAAM,sBAAsB,GAAG,MAAM,CAAC,UAAU,CAAC;QAEjD,oBAAoB;QACpB,MAAM,mBAAmB,GAAG,EAAS,CAAC;QAEtC,KAAK,MAAM,SAAS,IAAI,sBAAsB,EAAE;YAC9C,kBAAkB;YAClB,MAAM,aAAa,GAAG,SAAS,CAAC,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,CAAC;YAEnD,mBAAmB,CAAC,aAAa,CAAC,GAAG,CAAC,OAAa,EAAE,EAAE;gBACrD,OAAO,SAAS,CAAC,OAAO,CAAC,MAAM,CAAC,KAAK,EAAE,OAAO,CAAC,CAAC;YAClD,CAAC,CAAC;SACH;QAED,8BAA8B;QAE9B,OAAO,mBAAmB,CAAC;IAC7B,CAAC,CAAC;IAEF,OAAO,sBAAsB,EAAE,CAAC;AAClC,CAAC,CAAC;AAEF,0DAA0D;AAC1D,0DAA0D;AAE1D,8EAA8E;AAC9E,6BAA6B;AAC7B,wEAAwE;AACxE,KAAK;AAEL,6BAA6B;AAC7B,iEAAiE;AACjE,4BAA4B;AAC5B,gCAAgC;AAChC,KAAK;AAEL,qFAAqF;AACrF,6EAA6E;AAE7E,0FAA0F;AAC1F,4EAA4E;AAE5E,kBAAe,aAAa,CAAC"}
\ No newline at end of file
diff --git a/dist/hooks/useSignal.js b/dist/hooks/useSignal.js
index d40f896..0584d23 100644
--- a/dist/hooks/useSignal.js
+++ b/dist/hooks/useSignal.js
@@ -4,9 +4,9 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
};
Object.defineProperty(exports, "__esModule", { value: true });
const react_1 = require("react");
-const contexts_1 = __importDefault(require("../contexts"));
+const index_js_1 = __importDefault(require("../contexts/index.js"));
const useSignal = (signalName) => {
- const { signals } = (0, react_1.useContext)(contexts_1.default);
+ const { signals } = (0, react_1.useContext)(index_js_1.default);
const memoizedSignals = (0, react_1.useMemo)(() => signals, [signals]);
/**
* Get state of a signal base on its name
diff --git a/dist/hooks/useSignal.js.map b/dist/hooks/useSignal.js.map
index cbcf249..82b4d16 100644
--- a/dist/hooks/useSignal.js.map
+++ b/dist/hooks/useSignal.js.map
@@ -1 +1 @@
-{"version":3,"file":"useSignal.js","sourceRoot":"","sources":["../../src/hooks/useSignal.ts"],"names":[],"mappings":";;;;;AAAA,iCAA4C;AAC5C,2DAAoC;AAEpC,MAAM,SAAS,GAAG,CAAU,UAAkB,EAAE,EAAE;IAChD,MAAM,EAAE,OAAO,EAAE,GAAG,IAAA,kBAAU,EAAC,kBAAS,CAAC,CAAC;IAC1C,MAAM,eAAe,GAAG,IAAA,eAAO,EAAC,GAAG,EAAE,CAAC,OAAO,EAAE,CAAC,OAAO,CAAC,CAAC,CAAC;IAE1D;;;;OAIG;IACH,MAAM,oBAAoB,GAAG,CAAC,UAAkB,EAAK,EAAE;QACrD,MAAM,MAAM,GAAG,eAAe,CAAC,IAAI,CAAC,MAAM,CAAC,EAAE,CAAC,MAAM,CAAC,IAAI,KAAK,UAAU,CAAC,CAAC;QAE1E,IAAI,MAAM,EAAE;YACV,OAAO,MAAM,CAAC,KAAK,CAAC;SACrB;QAED,kCAAkC;QAClC,MAAM,IAAI,KAAK,CAAC,UAAU,UAAU,YAAY,CAAC,CAAC;IACpD,CAAC,CAAA;IAED,OAAO,oBAAoB,CAAC,UAAU,CAAC,CAAC;AAC1C,CAAC,CAAC;AAEF,kBAAe,SAAS,CAAC"}
\ No newline at end of file
+{"version":3,"file":"useSignal.js","sourceRoot":"","sources":["../../src/hooks/useSignal.ts"],"names":[],"mappings":";;;;;AAAA,iCAA4C;AAC5C,oEAA6C;AAE7C,MAAM,SAAS,GAAG,CAAU,UAAkB,EAAE,EAAE;IAChD,MAAM,EAAE,OAAO,EAAE,GAAG,IAAA,kBAAU,EAAC,kBAAS,CAAC,CAAC;IAC1C,MAAM,eAAe,GAAG,IAAA,eAAO,EAAC,GAAG,EAAE,CAAC,OAAO,EAAE,CAAC,OAAO,CAAC,CAAC,CAAC;IAE1D;;;;OAIG;IACH,MAAM,oBAAoB,GAAG,CAAC,UAAkB,EAAK,EAAE;QACrD,MAAM,MAAM,GAAG,eAAe,CAAC,IAAI,CAAC,MAAM,CAAC,EAAE,CAAC,MAAM,CAAC,IAAI,KAAK,UAAU,CAAC,CAAC;QAE1E,IAAI,MAAM,EAAE;YACV,OAAO,MAAM,CAAC,KAAK,CAAC;SACrB;QAED,kCAAkC;QAClC,MAAM,IAAI,KAAK,CAAC,UAAU,UAAU,YAAY,CAAC,CAAC;IACpD,CAAC,CAAA;IAED,OAAO,oBAAoB,CAAC,UAAU,CAAC,CAAC;AAC1C,CAAC,CAAC;AAEF,kBAAe,SAAS,CAAC"}
\ No newline at end of file
diff --git a/dist/index.d.ts b/dist/index.d.ts
index b115086..5adcfbc 100644
--- a/dist/index.d.ts
+++ b/dist/index.d.ts
@@ -1,8 +1,9 @@
-import createSignal from "./helpers/createSignal";
-import createStore from "./helpers/createStore";
-import GXProvider from "./providers";
-import useAction from "./hooks/useAction";
-import useActions from "./hooks/useActions";
-import useSignal from "./hooks/useSignal";
+import createSignal from "./helpers/createSignal.js";
+import createStore from "./helpers/createStore.js";
+import GXProvider from "./providers/index.js";
+import useAction from "./hooks/useAction.js";
+import useActions from "./hooks/useActions.js";
+import useSignal from "./hooks/useSignal.js";
+import useOperations from "./hooks/useOperations.js";
export default GXProvider;
-export { createSignal, createStore, useAction, useActions, useSignal };
+export { createSignal, createStore, useAction, useActions, useSignal, useOperations, };
diff --git a/dist/index.js b/dist/index.js
index 3e99ec8..8f1e20d 100644
--- a/dist/index.js
+++ b/dist/index.js
@@ -3,17 +3,20 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
-exports.useSignal = exports.useActions = exports.useAction = exports.createStore = exports.createSignal = void 0;
-const createSignal_1 = __importDefault(require("./helpers/createSignal"));
-exports.createSignal = createSignal_1.default;
-const createStore_1 = __importDefault(require("./helpers/createStore"));
-exports.createStore = createStore_1.default;
-const providers_1 = __importDefault(require("./providers"));
-const useAction_1 = __importDefault(require("./hooks/useAction"));
-exports.useAction = useAction_1.default;
-const useActions_1 = __importDefault(require("./hooks/useActions"));
-exports.useActions = useActions_1.default;
-const useSignal_1 = __importDefault(require("./hooks/useSignal"));
-exports.useSignal = useSignal_1.default;
-exports.default = providers_1.default;
+exports.useOperations = exports.useSignal = exports.useActions = exports.useAction = exports.createStore = exports.createSignal = void 0;
+const createSignal_js_1 = __importDefault(require("./helpers/createSignal.js"));
+exports.createSignal = createSignal_js_1.default;
+const createStore_js_1 = __importDefault(require("./helpers/createStore.js"));
+exports.createStore = createStore_js_1.default;
+const index_js_1 = __importDefault(require("./providers/index.js"));
+const useAction_js_1 = __importDefault(require("./hooks/useAction.js"));
+exports.useAction = useAction_js_1.default;
+const useActions_js_1 = __importDefault(require("./hooks/useActions.js"));
+exports.useActions = useActions_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"));
+exports.useOperations = useOperations_js_1.default;
+exports.default = index_js_1.default;
+// "build": "tsc && npx babel dist --out-dir cjs --extensions '.js' --source-maps inline --copy-files",
//# sourceMappingURL=index.js.map
\ No newline at end of file
diff --git a/dist/index.js.map b/dist/index.js.map
index 80d41cd..15c3f2f 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,0EAAkD;AAUzC,uBAVF,sBAAY,CAUE;AATrB,wEAAgD;AASzB,sBAThB,qBAAW,CASgB;AARlC,4DAAqC;AAErC,kEAA0C;AAMN,oBAN7B,mBAAS,CAM6B;AAL7C,oEAA4C;AAKG,qBALxC,oBAAU,CAKwC;AAJzD,kEAA0C;AAIiB,oBAJpD,mBAAS,CAIoD;AAFpE,kBAAe,mBAAU,CAAC"}
\ No newline at end of file
+{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;AAAA,gFAAqD;AAYnD,uBAZK,yBAAY,CAYL;AAXd,8EAAmD;AAYjD,sBAZK,wBAAW,CAYL;AAXb,oEAA8C;AAE9C,wEAA6C;AAU3C,oBAVK,sBAAS,CAUL;AATX,0EAA+C;AAU7C,qBAVK,uBAAU,CAUL;AATZ,wEAA6C;AAU3C,oBAVK,sBAAS,CAUL;AATX,gFAAqD;AAUnD,wBAVK,0BAAa,CAUL;AARf,kBAAe,kBAAU,CAAC;AAW1B,uGAAuG"}
\ No newline at end of file
diff --git a/dist/providers/index.d.ts b/dist/providers/index.d.ts
index 6a3aabf..18daece 100644
--- a/dist/providers/index.d.ts
+++ b/dist/providers/index.d.ts
@@ -1,3 +1,3 @@
///
-import { GXProviderProps } from "./types";
+import { GXProviderProps } from "./types.js";
export default function GXProvider({ children, store }: GXProviderProps): JSX.Element;
diff --git a/dist/providers/index.js b/dist/providers/index.js
index c3dc3d1..a6be43f 100644
--- a/dist/providers/index.js
+++ b/dist/providers/index.js
@@ -5,17 +5,17 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
Object.defineProperty(exports, "__esModule", { value: true });
const jsx_runtime_1 = require("react/jsx-runtime");
const react_1 = require("react");
-const contexts_1 = __importDefault(require("../contexts"));
-const reducer_1 = __importDefault(require("./reducer"));
+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_1.useReducer)(reducer_1.default, store.getSignals());
+ const [signals, dispatch] = (0, react_1.useReducer)(reducer_js_1.default, store.getSignals());
// Context value
const contextValue = {
signals,
dispatch,
};
- return ((0, jsx_runtime_1.jsx)(contexts_1.default.Provider, Object.assign({ value: contextValue }, { children: children })));
+ return ((0, jsx_runtime_1.jsx)(index_js_1.default.Provider, Object.assign({ value: contextValue }, { children: children })));
}
exports.default = GXProvider;
;
diff --git a/dist/providers/index.js.map b/dist/providers/index.js.map
index fbe3203..a5d901b 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,iCAAmC;AACnC,2DAAoC;AAEpC,wDAAkC;AAElC,SAAwB,UAAU,CAAC,EAAE,QAAQ,EAAE,KAAK,EAAmB;IACrE,uCAAuC;IACvC,MAAM,CAAC,OAAO,EAAE,QAAQ,CAAC,GAAG,IAAA,kBAAU,EAAC,iBAAS,EAAE,KAAK,CAAC,UAAU,EAAE,CAAC,CAAC;IAEtE,gBAAgB;IAChB,MAAM,YAAY,GAAG;QACnB,OAAO;QACP,QAAQ;KACT,CAAC;IAEF,OAAO,CACL,uBAAC,kBAAS,CAAC,QAAQ,kBAAC,KAAK,EAAE,YAAY,gBAAG,QAAQ,IAAsB,CACzE,CAAC;AACJ,CAAC;AAbD,6BAaC;AAAA,CAAC"}
\ No newline at end of file
+{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/providers/index.tsx"],"names":[],"mappings":";;;;;;AAAA,iCAAmC;AACnC,oEAA6C;AAE7C,8DAAqC;AAErC,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,gBAAgB;IAChB,MAAM,YAAY,GAAG;QACnB,OAAO;QACP,QAAQ;KACT,CAAC;IAEF,OAAO,CACL,uBAAC,kBAAS,CAAC,QAAQ,kBAAC,KAAK,EAAE,YAAY,gBAAG,QAAQ,IAAsB,CACzE,CAAC;AACJ,CAAC;AAbD,6BAaC;AAAA,CAAC"}
\ No newline at end of file
diff --git a/dist/providers/reducer.d.ts b/dist/providers/reducer.d.ts
index 6999312..cc8ee35 100644
--- a/dist/providers/reducer.d.ts
+++ b/dist/providers/reducer.d.ts
@@ -1,4 +1,4 @@
-import { GXSignalType } from "../contexts/types";
-import { GXAction } from "./types";
+import { GXSignalType } from "../contexts/types.js";
+import { GXAction } from "./types.js";
declare const gxReducer: (signals: GXSignalType[], action: GXAction) => GXSignalType[];
export default gxReducer;
diff --git a/dist/providers/types.d.ts b/dist/providers/types.d.ts
index 0212864..dd63d14 100644
--- a/dist/providers/types.d.ts
+++ b/dist/providers/types.d.ts
@@ -1,5 +1,5 @@
///
-import { CreateStoreType } from '../helpers/types';
+import { CreateStoreType } from '../helpers/types.js';
export type GXProviderProps = {
children: React.ReactElement;
store: CreateStoreType;
diff --git a/package.json b/package.json
index d80a687..26dc140 100644
--- a/package.json
+++ b/package.json
@@ -1,9 +1,9 @@
{
"name": "@dilane3/gx",
- "version": "1.2.5",
+ "version": "1.3.0",
"private": false,
"license": "MIT",
- "main": "cjs/index.js",
+ "main": "dist/index.js",
"type": "commonjs",
"author": {
"name": "dilane3",
@@ -36,7 +36,7 @@
"react": "^18.0.0"
},
"scripts": {
- "build": "tsc && npx babel dist --out-dir cjs --extensions '.js' --source-maps inline --copy-files",
+ "build": "tsc",
"test": "react-scripts test",
"deploy": "npm publish --access public"
},
diff --git a/src/contexts/index.ts b/src/contexts/index.ts
index 6c19f6b..e3299c4 100644
--- a/src/contexts/index.ts
+++ b/src/contexts/index.ts
@@ -1,9 +1,9 @@
import { createContext } from "react";
-import { GXContextType } from "./types";
+import { GXContextType } from "./types.js";
const GXContext = createContext({
signals: [],
- dispatch: () => {}
+ dispatch: () => {},
});
-export default GXContext
\ No newline at end of file
+export default GXContext;
diff --git a/src/contexts/types.ts b/src/contexts/types.ts
index eb00618..e4dccbb 100644
--- a/src/contexts/types.ts
+++ b/src/contexts/types.ts
@@ -1,4 +1,4 @@
-import { GXAction } from "../providers/types";
+import { GXAction } from "../providers/types.js";
export type GXSignalType = {
// Name of the signal
@@ -8,7 +8,10 @@ export type GXSignalType = {
state: T,
// Actions of the signal
- actions: GXActionType[]
+ actions: GXActionType[],
+
+ // Operation of the signal
+ operations?: GXOperationType[]
}
export type GXActionType = {
@@ -19,6 +22,14 @@ export type GXActionType = {
handler: (state: T, payload: P) => T
}
+export type GXOperationType = {
+ // Represent the type of the operation
+ type: string,
+
+ // The handle function
+ handler: (state: T, payload: P) => Q
+}
+
export type DispatchedActionType = {
// The type of the action
type: string,
diff --git a/src/helpers/createSignal.ts b/src/helpers/createSignal.ts
index 5e0f7b3..74faa4d 100644
--- a/src/helpers/createSignal.ts
+++ b/src/helpers/createSignal.ts
@@ -1,32 +1,44 @@
-import { CreateSignalOptionType } from './types';
-import { GXActionType } from '../contexts/types';
+import { CreateSignalOptionType } from "./types.js";
+import { GXActionType, GXOperationType } from "../contexts/types.js";
/**
* Create a signal with a state and actions for managing this state
- * @param options
- * @returns
+ * @param options
+ * @returns
*/
const createSignal = (options: CreateSignalOptionType) => {
const actions: GXActionType[] = [];
+ const operations: GXOperationType[] = [];
// Convert the actions object to an array
- const actionsTable = Object.entries(options.actions)
+ const actionsTable = Object.entries(options.actions);
for (let action of actionsTable) {
actions.push({
type: `${options.name}/${action[0]}`,
- handler: action[1]
- })
+ handler: action[1],
+ });
+ }
+
+ // Convert the operations object to an array
+ const operationsTable = Object.entries(options.operations || {});
+
+ for (let operation of operationsTable) {
+ operations.push({
+ type: `${options.name}/${operation[0]}`,
+ handler: operation[1],
+ });
}
// Create a signal
const signal = {
name: options.name,
state: options.state,
- actions
- }
+ actions,
+ operations,
+ };
return signal;
-}
+};
-export default createSignal;
\ No newline at end of file
+export default createSignal;
diff --git a/src/helpers/createStore.ts b/src/helpers/createStore.ts
index 6708d76..1978c8f 100644
--- a/src/helpers/createStore.ts
+++ b/src/helpers/createStore.ts
@@ -1,5 +1,5 @@
-import { GXSignalType } from "../contexts/types";
-import { CreateStoreType } from "./types";
+import { GXSignalType } from "../contexts/types.js";
+import { CreateStoreType } from "./types.js";
const createStore = (signals: GXSignalType[]): CreateStoreType => {
return {
diff --git a/src/helpers/types.ts b/src/helpers/types.ts
index 7bd8c4f..4475df0 100644
--- a/src/helpers/types.ts
+++ b/src/helpers/types.ts
@@ -1,9 +1,10 @@
-import { GXSignalType } from "../contexts/types";
+import { GXSignalType } from "../contexts/types.js";
export type CreateSignalOptionType = {
name: string;
state: T;
actions: Action;
+ operations?: Operation;
}
export type CreateStoreType = {
@@ -12,4 +13,8 @@ export type CreateStoreType = {
export type Action = {
[key: string]: (state: T, payload: any) => T
+}
+
+export type Operation = {
+ [key: string]: (state: T, payload?: any) => any
}
\ No newline at end of file
diff --git a/src/hooks/types.ts b/src/hooks/types.ts
index 1b2a68b..54e955a 100644
--- a/src/hooks/types.ts
+++ b/src/hooks/types.ts
@@ -1,3 +1,7 @@
export type Actions = {
- [key: string]: (payload: any) => void
+ [key: string]: (payload?: any) => void
+}
+
+export type Operations = {
+ [key: string]: (payload?: any) => P
}
\ No newline at end of file
diff --git a/src/hooks/useAction.ts b/src/hooks/useAction.ts
index 6200459..d4545ef 100644
--- a/src/hooks/useAction.ts
+++ b/src/hooks/useAction.ts
@@ -1,13 +1,14 @@
-import useActions from "./useActions";
+import { Actions } from "./types.js";
+import useActions from "./useActions.js";
-const useAction = (signalName: string, action: string) => {
+const useAction = (signalName: string, action: string) => {
if (!signalName || typeof signalName !== "string")
throw new Error("Provide a signalName as a first argument of useAction");
if (!action || typeof action !== "string")
throw new Error("Provide an action as second argument of useAction");
- const actions = useActions(signalName, action);
+ const actions = useActions(signalName, action);
return Object.values(actions)[0];
};
diff --git a/src/hooks/useActions.ts b/src/hooks/useActions.ts
index 4fbc323..1498c88 100644
--- a/src/hooks/useActions.ts
+++ b/src/hooks/useActions.ts
@@ -1,9 +1,9 @@
import { useContext } from "react";
-import GXContext from "../contexts";
-import { GXActionType } from "../contexts/types";
-import { Actions } from "./types";
+import GXContext from "../contexts/index.js";
+import { GXActionType } from "../contexts/types.js";
+import { Actions } from "./types.js";
-const useActions = (signalName: string, ...actions: string[]) => {
+const useActions = (signalName: string, ...actions: string[]) => {
if (!signalName || typeof signalName !== "string")
throw new Error("Provide a signalName as first argument of useActions");
@@ -40,18 +40,18 @@ const useActions = (signalName: string, ...actions: string[]) => {
} else throw new Error(`Signal ${signalName} not found`);
};
- const handleFormatActions = () => {
+ const handleFormatActions = (): T => {
// Get actions
const nonFormattedActions = handleGetActions(signalName);
// Formatted actions
- const formattedActions: Actions = {};
+ const formattedActions = {} as any;
for (const action of nonFormattedActions) {
// Get action name
const actionName = action.type.split("/")[1];
- formattedActions[actionName] = (payload: any) => {
+ formattedActions[actionName] = (payload?: any) => {
dispatch({
type: action.type,
payload,
diff --git a/src/hooks/useOperations.ts b/src/hooks/useOperations.ts
new file mode 100644
index 0000000..d1729d7
--- /dev/null
+++ b/src/hooks/useOperations.ts
@@ -0,0 +1,62 @@
+import { useContext } from "react";
+import GXContext from "../contexts/index.js";
+import { Operations } from "./types.js";
+
+const useOperations = (signalName: string) => {
+ // Get Global Context
+ const { signals } = useContext(GXContext);
+
+ if (!signalName || typeof signalName !== "string")
+ throw new Error(
+ "Provide a signalName as a first argument of useOperations"
+ );
+
+ const handleFormatOperations = (): T => {
+ const signal = signals.find((signal) => signal.name === signalName);
+
+ if (!signal) throw new Error(`Signal ${signalName} not found`);
+
+ // Get actions
+ const nonFormattedOperations = signal.operations;
+
+ // Formatted actions
+ const formattedOperations = {} as any;
+
+ for (const operation of nonFormattedOperations) {
+ // Get action name
+ const operationName = operation.type.split("/")[1];
+
+ formattedOperations[operationName] = (payload?: any) => {
+ return operation.handler(signal.state, payload);
+ };
+ }
+
+ // return formattedOperations;
+
+ return formattedOperations;
+ };
+
+ return handleFormatOperations();
+};
+
+// Définir un type générique pour représenter une fonction
+// type FunctionType any> = T;
+
+// Définir un type générique pour représenter un objet contenant des fonctions
+// type FunctionObject = {
+// [K in keyof T]: T[K] extends Function ? FunctionType : never;
+// };
+
+// type FunctionObject = {
+// [K in keyof T]: T[K] extends (payload: infer Arg) => infer R
+// ? (payload: Arg) => R
+// : (payload?: any) => any;
+// };
+
+// // Utilisation d'une fonction auxiliaire pour extraire le type du second paramètre
+// type SecondParamType = T extends (a: any, b: infer P) => any ? P : any;
+
+// // Utilisation d'une fonction auxiliaire pour extraire le type de retour d'une fonction
+// type ReturnTypeFunc = T extends (...args: any[]) => infer R ? R : any;
+
+export default useOperations;
diff --git a/src/hooks/useSignal.ts b/src/hooks/useSignal.ts
index 72d72f9..1190652 100644
--- a/src/hooks/useSignal.ts
+++ b/src/hooks/useSignal.ts
@@ -1,5 +1,5 @@
import { useContext, useMemo } from "react";
-import GXContext from "../contexts";
+import GXContext from "../contexts/index.js";
const useSignal = (signalName: string) => {
const { signals } = useContext(GXContext);
diff --git a/src/index.ts b/src/index.ts
index 721e7f8..e0ce541 100644
--- a/src/index.ts
+++ b/src/index.ts
@@ -1,11 +1,21 @@
-import createSignal from "./helpers/createSignal";
-import createStore from "./helpers/createStore";
-import GXProvider from "./providers";
+import createSignal from "./helpers/createSignal.js";
+import createStore from "./helpers/createStore.js";
+import GXProvider from "./providers/index.js";
-import useAction from "./hooks/useAction";
-import useActions from "./hooks/useActions";
-import useSignal from "./hooks/useSignal";
+import useAction from "./hooks/useAction.js";
+import useActions from "./hooks/useActions.js";
+import useSignal from "./hooks/useSignal.js";
+import useOperations from "./hooks/useOperations.js";
export default GXProvider;
-export { createSignal, createStore, useAction, useActions, useSignal };
+export {
+ createSignal,
+ createStore,
+ useAction,
+ useActions,
+ useSignal,
+ useOperations,
+};
+
+// "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 56a58af..c679c6b 100644
--- a/src/providers/index.tsx
+++ b/src/providers/index.tsx
@@ -1,7 +1,7 @@
import { useReducer } from "react";
-import GXContext from "../contexts";
-import { GXProviderProps } from "./types";
-import gxReducer from "./reducer";
+import GXContext from "../contexts/index.js";
+import { GXProviderProps } from "./types.js";
+import gxReducer from "./reducer.js";
export default function GXProvider({ children, store }: GXProviderProps) {
// Global state that manage all signals
diff --git a/src/providers/reducer.ts b/src/providers/reducer.ts
index 5b4cdb9..b694dac 100644
--- a/src/providers/reducer.ts
+++ b/src/providers/reducer.ts
@@ -1,5 +1,5 @@
-import { GXSignalType } from "../contexts/types";
-import { GXAction } from "./types";
+import { GXSignalType } from "../contexts/types.js";
+import { GXAction } from "./types.js";
const gxReducer = (signals: GXSignalType[], action: GXAction) => {
// Prev signals
diff --git a/src/providers/types.ts b/src/providers/types.ts
index 5a394b8..8830fac 100644
--- a/src/providers/types.ts
+++ b/src/providers/types.ts
@@ -1,4 +1,4 @@
-import { CreateStoreType } from '../helpers/types';
+import { CreateStoreType } from '../helpers/types.js';
export type GXProviderProps = {
children: React.ReactElement,