react-hooks-global-states 10.2.0 → 11.0.0-beta.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/GlobalStore.d.ts +63 -32
- package/GlobalStore.js +128 -1
- package/GlobalStoreAbstract.d.ts +1 -1
- package/GlobalStoreAbstract.js +41 -1
- package/README.md +43 -24
- package/bundle.js +56 -1
- package/createContext.d.ts +200 -63
- package/createContext.js +70 -1
- package/createGlobalState.d.ts +134 -7
- package/createGlobalState.js +47 -1
- package/index.d.ts +2 -5
- package/isRecord.js +44 -1
- package/package.json +4 -17
- package/shallowCompare.d.ts +43 -4
- package/shallowCompare.js +53 -1
- package/throwWrongKeyOnActionCollectionConfig.js +16 -1
- package/types.d.ts +88 -61
- package/types.js +4 -1
- package/uniqueId.js +16 -1
- package/webpack.config.js +5 -5
- package/createCustomGlobalState.d.ts +0 -23
- package/createCustomGlobalState.js +0 -1
- package/generateStackHash.d.ts +0 -2
- package/generateStackHash.js +0 -1
- package/useStableState.d.ts +0 -14
- package/useStableState.js +0 -1
package/createGlobalState.js
CHANGED
|
@@ -1 +1,47 @@
|
|
|
1
|
-
var e;e=e
|
|
1
|
+
var e;e=e=>/******/(()=>{
|
|
2
|
+
/******/"use strict";
|
|
3
|
+
/******/var t={
|
|
4
|
+
/***/778:
|
|
5
|
+
/***/t=>{t.exports=e;
|
|
6
|
+
/***/},
|
|
7
|
+
/***/840:
|
|
8
|
+
/***/(e,t,o)=>{Object.defineProperty(t,"__esModule",{value:!0}),t.createGlobalState=void 0;var r=o(778);
|
|
9
|
+
/**
|
|
10
|
+
* Creates a global state hook
|
|
11
|
+
*/t.createGlobalState=function(){for(var e=arguments.length,t=new Array(e),o=0;o<e;o++)t[o]=arguments[o];var a=t[0],l=t[1];return new r.GlobalStore(a,l).use},t.default=t.createGlobalState}
|
|
12
|
+
/***/
|
|
13
|
+
/******/},o={};
|
|
14
|
+
/************************************************************************/
|
|
15
|
+
/******/
|
|
16
|
+
/******/
|
|
17
|
+
/******/
|
|
18
|
+
/******/
|
|
19
|
+
/******/
|
|
20
|
+
/******/
|
|
21
|
+
/******/return function e(r){
|
|
22
|
+
/******/
|
|
23
|
+
/******/var a=o[r];
|
|
24
|
+
/******/if(void 0!==a)
|
|
25
|
+
/******/return a.exports;
|
|
26
|
+
/******/
|
|
27
|
+
/******/
|
|
28
|
+
/******/var l=o[r]={
|
|
29
|
+
/******/
|
|
30
|
+
/******/
|
|
31
|
+
/******/exports:{}
|
|
32
|
+
/******/};
|
|
33
|
+
/******/
|
|
34
|
+
/******/
|
|
35
|
+
/******/
|
|
36
|
+
/******/
|
|
37
|
+
/******/
|
|
38
|
+
/******/return t[r](l,l.exports,e),l.exports;
|
|
39
|
+
/******/}
|
|
40
|
+
/******/
|
|
41
|
+
/************************************************************************/
|
|
42
|
+
/******/
|
|
43
|
+
/******/
|
|
44
|
+
/******/
|
|
45
|
+
/******/
|
|
46
|
+
/******/(840);
|
|
47
|
+
/******/})(),"object"==typeof exports&&"object"==typeof module?module.exports=e(require("./GlobalStore.js")):"function"==typeof define&&define.amd?define(["./GlobalStore.js"],e):"object"==typeof exports?exports["react-hooks-global-states"]=e(require("./GlobalStore.js")):this["react-hooks-global-states"]=e(this["./GlobalStore.js"]);
|
package/index.d.ts
CHANGED
|
@@ -1,12 +1,9 @@
|
|
|
1
|
-
export {
|
|
1
|
+
export { StateApi, ObservableFragment, MetadataSetter, StateChanges, StoreTools, ActionCollectionResult, GlobalStoreCallbacks, UseHookConfig, UnsubscribeCallback, SubscribeCallbackConfig, SubscribeCallback, BaseMetadata, MetadataGetter, CustomGlobalHookBuilderParams, SelectorCallback, SubscriberParameters, SubscriptionCallback, StateHook, ActionCollectionConfig, } from './types';
|
|
2
2
|
export { GlobalStore } from './GlobalStore';
|
|
3
3
|
export { GlobalStoreAbstract } from './GlobalStoreAbstract';
|
|
4
4
|
export { createGlobalState, type InferActionsType } from './createGlobalState';
|
|
5
|
-
export { createCustomGlobalState } from './createCustomGlobalState';
|
|
6
5
|
export { shallowCompare } from './shallowCompare';
|
|
7
6
|
export { uniqueId } from './uniqueId';
|
|
8
7
|
export { throwWrongKeyOnActionCollectionConfig } from './throwWrongKeyOnActionCollectionConfig';
|
|
9
8
|
export { isRecord } from './isRecord';
|
|
10
|
-
export {
|
|
11
|
-
export { type Context, type ContextProvider, type ContextHook, type CreateContext, type InferContextType, createContext, } from './createContext';
|
|
12
|
-
export { generateStackHash } from './generateStackHash';
|
|
9
|
+
export { type ContextApi as Context, type ContextProvider, type ContextHook, type InferContextApi, createContext, } from './createContext';
|
package/isRecord.js
CHANGED
|
@@ -1 +1,44 @@
|
|
|
1
|
-
var o;o=o
|
|
1
|
+
var o;o=o=>/******/(()=>{
|
|
2
|
+
/******/"use strict";
|
|
3
|
+
/******/var t={
|
|
4
|
+
/***/35:
|
|
5
|
+
/***/(o,t,e)=>{function r(o){return r="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(o){return typeof o}:function(o){return o&&"function"==typeof Symbol&&o.constructor===Symbol&&o!==Symbol.prototype?"symbol":typeof o},r(o)}Object.defineProperty(t,"__esModule",{value:!0}),t.isRecord=void 0;var s=e(773);t.isRecord=function(o){return!(0,s.isNil)(o)&&"object"===r(o)},t.default=t.isRecord}
|
|
6
|
+
/***/,
|
|
7
|
+
/***/773:
|
|
8
|
+
/***/t=>{t.exports=o;
|
|
9
|
+
/***/
|
|
10
|
+
/******/}},e={};
|
|
11
|
+
/************************************************************************/
|
|
12
|
+
/******/
|
|
13
|
+
/******/
|
|
14
|
+
/******/
|
|
15
|
+
/******/
|
|
16
|
+
/******/
|
|
17
|
+
/******/
|
|
18
|
+
/******/return function o(r){
|
|
19
|
+
/******/
|
|
20
|
+
/******/var s=e[r];
|
|
21
|
+
/******/if(void 0!==s)
|
|
22
|
+
/******/return s.exports;
|
|
23
|
+
/******/
|
|
24
|
+
/******/
|
|
25
|
+
/******/var i=e[r]={
|
|
26
|
+
/******/
|
|
27
|
+
/******/
|
|
28
|
+
/******/exports:{}
|
|
29
|
+
/******/};
|
|
30
|
+
/******/
|
|
31
|
+
/******/
|
|
32
|
+
/******/
|
|
33
|
+
/******/
|
|
34
|
+
/******/
|
|
35
|
+
/******/return t[r](i,i.exports,o),i.exports;
|
|
36
|
+
/******/}
|
|
37
|
+
/******/
|
|
38
|
+
/************************************************************************/
|
|
39
|
+
/******/
|
|
40
|
+
/******/
|
|
41
|
+
/******/
|
|
42
|
+
/******/
|
|
43
|
+
/******/(35);
|
|
44
|
+
/******/})(),"object"==typeof exports&&"object"==typeof module?module.exports=o(require("json-storage-formatter/isNil")):"function"==typeof define&&define.amd?define(["json-storage-formatter/isNil"],o):"object"==typeof exports?exports["react-hooks-global-states"]=o(require("json-storage-formatter/isNil")):this["react-hooks-global-states"]=o(this["json-storage-formatter/isNil"]);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "react-hooks-global-states",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "11.0.0-beta.2",
|
|
4
4
|
"description": "This is a package to easily handling global-state across your react-components using hooks.",
|
|
5
5
|
"main": "./bundle.js",
|
|
6
6
|
"types": "./index.d.ts",
|
|
@@ -26,11 +26,6 @@
|
|
|
26
26
|
"require": "./GlobalStoreAbstract.js",
|
|
27
27
|
"types": "./GlobalStoreAbstract.d.ts"
|
|
28
28
|
},
|
|
29
|
-
"./createCustomGlobalState": {
|
|
30
|
-
"import": "./createCustomGlobalState.js",
|
|
31
|
-
"require": "./createCustomGlobalState.js",
|
|
32
|
-
"types": "./createCustomGlobalState.d.ts"
|
|
33
|
-
},
|
|
34
29
|
"./createGlobalState": {
|
|
35
30
|
"import": "./createGlobalState.js",
|
|
36
31
|
"require": "./createGlobalState.js",
|
|
@@ -60,16 +55,6 @@
|
|
|
60
55
|
"import": "./uniqueId.js",
|
|
61
56
|
"require": "./uniqueId.js",
|
|
62
57
|
"types": "./uniqueId.d.ts"
|
|
63
|
-
},
|
|
64
|
-
"./useStableState": {
|
|
65
|
-
"import": "./useStableState.js",
|
|
66
|
-
"require": "./useStableState.js",
|
|
67
|
-
"types": "./useStableState.d.ts"
|
|
68
|
-
},
|
|
69
|
-
"./generateStackHash": {
|
|
70
|
-
"import": "./generateStackHash.js",
|
|
71
|
-
"require": "./generateStackHash.js",
|
|
72
|
-
"types": "./generateStackHash.d.ts"
|
|
73
58
|
}
|
|
74
59
|
},
|
|
75
60
|
"files": [
|
|
@@ -77,7 +62,8 @@
|
|
|
77
62
|
"*.d.ts"
|
|
78
63
|
],
|
|
79
64
|
"scripts": {
|
|
80
|
-
"
|
|
65
|
+
"format": "prettier --write .",
|
|
66
|
+
"test-debug": "node --inspect-brk node_modules/.bin/jest --runInBand --detectOpenHandles",
|
|
81
67
|
"test:quick": "jest --maxWorkers=4 -c --no-watchman -u",
|
|
82
68
|
"test:coverage": "jest --maxWorkers=4 -c --colors --no-watchman --verbose --coverage",
|
|
83
69
|
"build": "yarn clean && webpack --config webpack.config.js",
|
|
@@ -137,6 +123,7 @@
|
|
|
137
123
|
"jest": "^29.7.0",
|
|
138
124
|
"jest-environment-jsdom": "^30.0.4",
|
|
139
125
|
"json-storage-formatter": "^2.0.9",
|
|
126
|
+
"prettier": "^3.6.2",
|
|
140
127
|
"react": "^18.2.0",
|
|
141
128
|
"react-dom": "^18.2.0",
|
|
142
129
|
"ts-jest": "^29.1.1",
|
package/shallowCompare.d.ts
CHANGED
|
@@ -1,13 +1,52 @@
|
|
|
1
1
|
/**
|
|
2
|
-
*
|
|
2
|
+
* @description It performs a shallow comparison of the values.
|
|
3
|
+
* @param value1 - The first value to compare.
|
|
4
|
+
* @param value2 - The second value to compare.
|
|
5
|
+
* @returns True if the values are equal, false otherwise.
|
|
3
6
|
*/
|
|
4
7
|
export declare const shallowCompare: <T>(value1: T, value2: T) => boolean;
|
|
5
8
|
export declare const isArray: (value: unknown) => value is unknown[];
|
|
6
9
|
export declare const isMap: (value: unknown) => value is Map<unknown, unknown>;
|
|
7
10
|
export declare const isSet: (value: unknown) => value is Set<unknown>;
|
|
11
|
+
/**
|
|
12
|
+
* @description Determines whether a simple equality check (using `===`) is sufficient
|
|
13
|
+
* to compare the provided values. This helps decide when a deep equality check
|
|
14
|
+
* is unnecessary or inefficient.
|
|
15
|
+
*
|
|
16
|
+
* Simple equality checks are considered valid when:
|
|
17
|
+
* - The values have different types (comparison will trivially return false)
|
|
18
|
+
* - Either value is null or undefined
|
|
19
|
+
* - Both values are primitive types (string, number, boolean, symbol, bigint)
|
|
20
|
+
* - Both values are Date objects
|
|
21
|
+
* - Both values are functions
|
|
22
|
+
*
|
|
23
|
+
* @param value1 - The first value to compare.
|
|
24
|
+
* @param value2 - The second value to compare.
|
|
25
|
+
* @returns `true` if a simple `===` check is sufficient, `false` if a deep comparison may be required.
|
|
26
|
+
*
|
|
27
|
+
* @example
|
|
28
|
+
* ```ts
|
|
29
|
+
* canCheckSimpleEquality(42, 42); // true (primitive numbers)
|
|
30
|
+
* canCheckSimpleEquality({ a: 1 }, { a: 1 }); // false (objects)
|
|
31
|
+
* canCheckSimpleEquality([1, 2], [1, 2]); // false (arrays)
|
|
32
|
+
* canCheckSimpleEquality('a', 1); // true (different types, shallow check enough)
|
|
33
|
+
* ```
|
|
34
|
+
*/
|
|
8
35
|
export declare const canCheckSimpleEquality: (value1: unknown, value2: unknown) => boolean;
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
36
|
+
/**
|
|
37
|
+
* @description Performs a shallow comparison between two arrays.
|
|
38
|
+
*/
|
|
39
|
+
export declare const isEqualArray: <T>(array1: T[], array2: T[]) => array1 is T[];
|
|
40
|
+
/**
|
|
41
|
+
* @description Performs a shallow comparison between two maps.
|
|
42
|
+
*/
|
|
43
|
+
export declare const isEqualMap: <K, V>(map1: Map<K, V>, map2: Map<K, V>) => map1 is Map<K, V>;
|
|
44
|
+
/**
|
|
45
|
+
* @description Performs a shallow comparison between two sets.
|
|
46
|
+
*/
|
|
47
|
+
export declare const isEqualSet: <T>(set1: Set<T>, set2: Set<T>) => set1 is Set<T>;
|
|
48
|
+
/**
|
|
49
|
+
* @description Performs a shallow comparison between two objects.
|
|
50
|
+
*/
|
|
12
51
|
export declare const isEqualObject: <T extends Record<string, unknown>>(value1: T, value2: T) => boolean;
|
|
13
52
|
export default shallowCompare;
|
package/shallowCompare.js
CHANGED
|
@@ -1 +1,53 @@
|
|
|
1
|
-
var r,t;r=this,t=(r,t,e,n)
|
|
1
|
+
var r,t;r=this,t=(r,t,e,n)=>/******/(()=>{
|
|
2
|
+
/******/"use strict";
|
|
3
|
+
/******/var o={
|
|
4
|
+
/***/38:
|
|
5
|
+
/***/t=>{t.exports=r;
|
|
6
|
+
/***/},
|
|
7
|
+
/***/487:
|
|
8
|
+
/***/r=>{r.exports=t;
|
|
9
|
+
/***/},
|
|
10
|
+
/***/773:
|
|
11
|
+
/***/r=>{r.exports=e;
|
|
12
|
+
/***/},
|
|
13
|
+
/***/955:
|
|
14
|
+
/***/r=>{r.exports=n;
|
|
15
|
+
/***/},
|
|
16
|
+
/***/973:
|
|
17
|
+
/***/(r,t,e)=>{function n(r,t){var e="undefined"!=typeof Symbol&&r[Symbol.iterator]||r["@@iterator"];if(!e){if(Array.isArray(r)||(e=o(r))||t&&r&&"number"==typeof r.length){e&&(r=e);var n=0,i=function(){};return{s:i,n:function(){return n>=r.length?{done:!0}:{done:!1,value:r[n++]}},e:function(r){throw r},f:i}}throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}var a,s=!0,u=!1;return{s:function(){e=e.call(r)},n:function(){var r=e.next();return s=r.done,r},e:function(r){u=!0,a=r},f:function(){try{s||null==e.return||e.return()}finally{if(u)throw a}}}}function o(r,t){if(r){if("string"==typeof r)return i(r,t);var e={}.toString.call(r).slice(8,-1);return"Object"===e&&r.constructor&&(e=r.constructor.name),"Map"===e||"Set"===e?Array.from(r):"Arguments"===e||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(e)?i(r,t):void 0}}function i(r,t){(null==t||t>r.length)&&(t=r.length);for(var e=0,n=Array(t);e<t;e++)n[e]=r[e];return n}function a(r){return a="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(r){return typeof r}:function(r){return r&&"function"==typeof Symbol&&r.constructor===Symbol&&r!==Symbol.prototype?"symbol":typeof r},a(r)}Object.defineProperty(t,"__esModule",{value:!0}),t.isEqualObject=t.isEqualSet=t.isEqualMap=t.isEqualArray=t.canCheckSimpleEquality=t.isSet=t.isMap=t.isArray=t.shallowCompare=void 0;var s=e(955),u=e(38),l=e(773),f=e(487);t.shallowCompare=function(r,e){return!(r!==e)||((0,t.canCheckSimpleEquality)(r,e)?r===e:(0,t.isArray)(r)&&(0,t.isArray)(e)?(0,t.isEqualArray)(r,e):(0,t.isMap)(r)&&(0,t.isMap)(e)?(0,t.isEqualMap)(r,e):(0,t.isSet)(r)&&(0,t.isSet)(e)?(0,t.isEqualSet)(r,e):(0,f.isRecord)(r)&&(0,f.isRecord)(e)?(0,t.isEqualObject)(r,e):r===e)},t.isArray=function(r){return Array.isArray(r)},t.isMap=function(r){return r instanceof Map},t.isSet=function(r){return r instanceof Set},t.canCheckSimpleEquality=function(r,t){var e=a(r),n=a(t);return e!==n||(0,l.isNil)(r)||(0,l.isNil)(t)||(0,s.isPrimitive)(r)&&(0,s.isPrimitive)(t)||(0,u.isDate)(r)&&(0,u.isDate)(t)||"function"===e&&"function"===n},t.isEqualArray=function(r,t){if(r.length!==t.length)return!1;for(var e=0;e<r.length;e+=1)if(r[e]!==t[e])return!1;return!0},t.isEqualMap=function(r,t){if(r.size!==t.size)return!1;var e,i,a=n(r);try{for(a.s();!(e=a.n()).done;){var s=function(r){if(Array.isArray(r))return r}(i=e.value)||function(r){var t=null==r?null:"undefined"!=typeof Symbol&&r[Symbol.iterator]||r["@@iterator"];if(null!=t){var e,n,o,i,a=[],s=!0,u=!1;try{for(o=(t=t.call(r)).next;!(s=(e=o.call(t)).done)&&(a.push(e.value),2!==a.length);s=!0);}catch(r){u=!0,n=r}finally{try{if(!s&&null!=t.return&&(i=t.return(),Object(i)!==i))return}finally{if(u)throw n}}return a}}(i)||o(i,2)||function(){throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}(),u=s[0];if(s[1]!==t.get(u))return!1}}catch(r){a.e(r)}finally{a.f()}return!0},t.isEqualSet=function(r,t){if(r.size!==t.size)return!1;var e,o=n(r);try{for(o.s();!(e=o.n()).done;){var i=e.value;if(!t.has(i))return!1}}catch(r){o.e(r)}finally{o.f()}return!0},t.isEqualObject=function(r,t){var e=Object.keys(r),n=Object.keys(t);if(e.length!==n.length)return!1;for(var o=0,i=e;o<i.length;o++){var a=i[o];if(r[a]!==t[a])return!1}return!0},t.default=t.shallowCompare}
|
|
18
|
+
/***/
|
|
19
|
+
/******/},i={};
|
|
20
|
+
/************************************************************************/
|
|
21
|
+
/******/
|
|
22
|
+
/******/
|
|
23
|
+
/******/
|
|
24
|
+
/******/
|
|
25
|
+
/******/
|
|
26
|
+
/******/
|
|
27
|
+
/******/return function r(t){
|
|
28
|
+
/******/
|
|
29
|
+
/******/var e=i[t];
|
|
30
|
+
/******/if(void 0!==e)
|
|
31
|
+
/******/return e.exports;
|
|
32
|
+
/******/
|
|
33
|
+
/******/
|
|
34
|
+
/******/var n=i[t]={
|
|
35
|
+
/******/
|
|
36
|
+
/******/
|
|
37
|
+
/******/exports:{}
|
|
38
|
+
/******/};
|
|
39
|
+
/******/
|
|
40
|
+
/******/
|
|
41
|
+
/******/
|
|
42
|
+
/******/
|
|
43
|
+
/******/
|
|
44
|
+
/******/return o[t](n,n.exports,r),n.exports;
|
|
45
|
+
/******/}
|
|
46
|
+
/******/
|
|
47
|
+
/************************************************************************/
|
|
48
|
+
/******/
|
|
49
|
+
/******/
|
|
50
|
+
/******/
|
|
51
|
+
/******/
|
|
52
|
+
/******/(973);
|
|
53
|
+
/******/})(),"object"==typeof exports&&"object"==typeof module?module.exports=t(require("json-storage-formatter/isDate"),require("./isRecord.js"),require("json-storage-formatter/isNil"),require("json-storage-formatter/isPrimitive")):"function"==typeof define&&define.amd?define(["json-storage-formatter/isDate","./isRecord.js","json-storage-formatter/isNil","json-storage-formatter/isPrimitive"],t):"object"==typeof exports?exports["react-hooks-global-states"]=t(require("json-storage-formatter/isDate"),require("./isRecord.js"),require("json-storage-formatter/isNil"),require("json-storage-formatter/isPrimitive")):r["react-hooks-global-states"]=t(r["json-storage-formatter/isDate"],r["./isRecord.js"],r["json-storage-formatter/isNil"],r["json-storage-formatter/isPrimitive"]);
|
|
@@ -1 +1,16 @@
|
|
|
1
|
-
var t;t=()
|
|
1
|
+
var t;t=()=>/******/(()=>{
|
|
2
|
+
/******/"use strict";
|
|
3
|
+
/******/var t={};
|
|
4
|
+
/************************************************************************/
|
|
5
|
+
/******/
|
|
6
|
+
/******/
|
|
7
|
+
/******/
|
|
8
|
+
/******/
|
|
9
|
+
/******/
|
|
10
|
+
/******/
|
|
11
|
+
/******/
|
|
12
|
+
/******/
|
|
13
|
+
/***/return((t,e)=>{Object.defineProperty(e,"__esModule",{value:!0}),e.throwWrongKeyOnActionCollectionConfig=void 0,e.throwWrongKeyOnActionCollectionConfig=function(t){throw new Error("[WRONG CONFIGURATION!]: Every key inside the storeActionsConfig must be a higher order function that returns a function \n[".concat(t,"]: key is not a valid function, try something like this: \n{\n\n ").concat(t,": (param) => ({ setState, getState, setMetadata, getMetadata, actions }) => {\n\n setState((state) => ({ ...state, ...param }))\n\n }\n\n}\n"))},e.default=e.throwWrongKeyOnActionCollectionConfig})
|
|
14
|
+
/***/
|
|
15
|
+
/******/(0,t),t;
|
|
16
|
+
/******/})(),"object"==typeof exports&&"object"==typeof module?module.exports=t():"function"==typeof define&&define.amd?define([],t):"object"==typeof exports?exports["react-hooks-global-states"]=t():this["react-hooks-global-states"]=t();
|
package/types.d.ts
CHANGED
|
@@ -1,68 +1,75 @@
|
|
|
1
|
-
export type
|
|
1
|
+
export type AnyFunction = (...args: any[]) => any;
|
|
2
|
+
/**
|
|
3
|
+
* @description Represents the complete non-reactive API of a global state instance.
|
|
4
|
+
* This API provides full control over the state, including reading, writing, subscribing,
|
|
5
|
+
* and creating derived hooks or observable fragments.
|
|
6
|
+
*/
|
|
7
|
+
export type StateApi<State, StateDispatch, StateMutator, Metadata extends BaseMetadata> = {
|
|
2
8
|
/**
|
|
3
|
-
*
|
|
4
|
-
*
|
|
9
|
+
* Returns the metadata
|
|
10
|
+
* Metadata is additional non reactive information associated with the global state
|
|
5
11
|
*/
|
|
6
|
-
|
|
12
|
+
getMetadata: MetadataGetter<Metadata>;
|
|
7
13
|
/**
|
|
8
|
-
*
|
|
9
|
-
*
|
|
14
|
+
* Sets the metadata value
|
|
15
|
+
* The metadata value is not reactive and wont trigger re-renders
|
|
10
16
|
*/
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
17
|
+
setMetadata: MetadataSetter<Metadata>;
|
|
18
|
+
/**
|
|
19
|
+
* @description Contains the generated action functions if custom actions are defined.
|
|
20
|
+
* When actions exist, direct state mutation via `setState` is disabled and setState will be `null`.
|
|
21
|
+
* If no actions are provided, this property is `null`.
|
|
22
|
+
*/
|
|
23
|
+
actions: StateMutator extends AnyFunction ? null : StateMutator;
|
|
24
|
+
/**
|
|
25
|
+
* @description Provides direct access to the state updater when no custom actions are defined.
|
|
26
|
+
* When actions exist, direct state mutation via `setState` is disabled and this property will be `null`.
|
|
27
|
+
* If no actions are provided, this property exposes the `setState` function.
|
|
28
|
+
*/
|
|
29
|
+
setState: StateMutator extends AnyFunction ? StateDispatch : null;
|
|
14
30
|
/**
|
|
15
|
-
* @description
|
|
16
|
-
* This selectors includes:
|
|
17
|
-
* - stateRetriever: a function to get the current state or subscribe a callback to the state changes
|
|
18
|
-
* - stateMutator: a function to set the state or a collection of actions if you pass an storeActionsConfig configuration
|
|
19
|
-
* - metadataRetriever: a function to get the metadata of the global state
|
|
31
|
+
* @description Get the current state value
|
|
20
32
|
*/
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
33
|
+
getState: () => State;
|
|
34
|
+
/**
|
|
35
|
+
* @description Subscribe to the state changes
|
|
36
|
+
* You can subscribe to the whole state or to a fragment of the state by passing a selector as first parameter,
|
|
37
|
+
* this can be used in non react environments to listen to the state changes
|
|
38
|
+
*/
|
|
39
|
+
subscribe: SubscribeToState<State>;
|
|
26
40
|
/***
|
|
27
41
|
* @description Creates a new hooks that returns the result of the selector passed as a parameter
|
|
28
42
|
* Your can create selector hooks of other selectors hooks and extract as many derived states as or fragments of the state as you want
|
|
29
43
|
* The selector hook will be evaluated only if the result of the selector changes and the equality function returns false
|
|
30
44
|
* you can customize the equality function by passing the isEqualRoot and isEqual parameters
|
|
31
45
|
*/
|
|
32
|
-
createSelectorHook: <
|
|
33
|
-
name?: string;
|
|
34
|
-
}) => StateHook<Derivate, StateMutator, Metadata>;
|
|
35
|
-
createObservable: <Fragment>(this: StateHook<State, StateMutator, Metadata>, mainSelector: (state: State) => Fragment, args?: {
|
|
36
|
-
isEqual?: (current: Fragment, next: Fragment) => boolean;
|
|
37
|
-
isEqualRoot?: (current: State, next: State) => boolean;
|
|
46
|
+
createSelectorHook: <Selection>(selector: (state: State) => Selection, args?: Omit<UseHookConfig<Selection, State>, 'dependencies'> & {
|
|
38
47
|
name?: string;
|
|
39
|
-
}) =>
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
48
|
+
}) => StateHook<Selection, StateDispatch, StateMutator, Metadata>;
|
|
49
|
+
/**
|
|
50
|
+
* @description Creates a function that allows you to subscribe to a fragment of the state
|
|
51
|
+
* The observable selection will notify the subscribers only if the fragment changes and the equality function returns false
|
|
52
|
+
* you can customize the equality function by passing the isEqualRoot and isEqual parameters
|
|
53
|
+
*/
|
|
54
|
+
createObservable: <Selection>(this: StateApi<State, StateDispatch, StateMutator, Metadata>, selector: (state: State) => Selection, args?: {
|
|
55
|
+
isEqual?: (current: Selection, next: Selection) => boolean;
|
|
46
56
|
isEqualRoot?: (current: State, next: State) => boolean;
|
|
57
|
+
/**
|
|
58
|
+
* @description Name of the observable fragment for debugging purposes
|
|
59
|
+
*/
|
|
47
60
|
name?: string;
|
|
48
|
-
}) => ObservableFragment<
|
|
49
|
-
removeSubscriptions: () => void;
|
|
50
|
-
_name: string | undefined;
|
|
61
|
+
}) => ObservableFragment<Selection, StateDispatch, StateMutator, Metadata>;
|
|
51
62
|
};
|
|
52
|
-
|
|
63
|
+
/**
|
|
64
|
+
* @description Function that allows you to subscribe to a fragment of the state
|
|
65
|
+
*/
|
|
66
|
+
export type ObservableFragment<State, StateDispatch, StateMutator, Metadata extends BaseMetadata> = SubscribeToState<State> & StateApi<State, StateDispatch, StateMutator, Metadata>;
|
|
67
|
+
export interface StateHook<State, StateDispatch, StateMutator, Metadata extends BaseMetadata> extends StateApi<State, StateDispatch, StateMutator, Metadata> {
|
|
53
68
|
(): Readonly<[state: State, stateMutator: StateMutator, metadata: Metadata]>;
|
|
54
|
-
<Derivate>(selector: (state: State) => Derivate, dependencies?: unknown[]): Readonly<[
|
|
55
|
-
|
|
56
|
-
stateMutator: StateMutator,
|
|
57
|
-
metadata: Metadata
|
|
58
|
-
]>;
|
|
59
|
-
<Derivate>(selector: (state: State) => Derivate, config?: UseHookConfig<Derivate, State>): Readonly<[
|
|
60
|
-
state: Derivate,
|
|
61
|
-
stateMutator: StateMutator,
|
|
62
|
-
metadata: Metadata
|
|
63
|
-
]>;
|
|
69
|
+
<Derivate>(selector: (state: State) => Derivate, dependencies?: unknown[]): Readonly<[state: Derivate, stateMutator: StateMutator, metadata: Metadata]>;
|
|
70
|
+
<Derivate>(selector: (state: State) => Derivate, config?: UseHookConfig<Derivate, State>): Readonly<[state: Derivate, stateMutator: StateMutator, metadata: Metadata]>;
|
|
64
71
|
}
|
|
65
|
-
export type MetadataSetter<Metadata extends BaseMetadata
|
|
72
|
+
export type MetadataSetter<Metadata extends BaseMetadata> = (setter: Metadata | ((metadata: Metadata) => Metadata)) => void;
|
|
66
73
|
export type StateChanges<State> = {
|
|
67
74
|
state: State;
|
|
68
75
|
previousState: State | undefined;
|
|
@@ -71,31 +78,51 @@ export type StateChanges<State> = {
|
|
|
71
78
|
/**
|
|
72
79
|
* API for the actions of the global states
|
|
73
80
|
**/
|
|
74
|
-
export type StoreTools<State, Metadata extends BaseMetadata
|
|
81
|
+
export type StoreTools<State, Metadata extends BaseMetadata = BaseMetadata, Actions extends undefined | unknown | Record<string, AnyFunction> = unknown> = {
|
|
75
82
|
setMetadata: MetadataSetter<Metadata>;
|
|
76
|
-
setState:
|
|
77
|
-
getState:
|
|
83
|
+
setState: React.Dispatch<React.SetStateAction<State>>;
|
|
84
|
+
getState: () => State;
|
|
85
|
+
subscribe: SubscribeToState<State>;
|
|
78
86
|
getMetadata: () => Metadata;
|
|
79
87
|
actions: Actions;
|
|
80
88
|
};
|
|
81
89
|
/**
|
|
82
90
|
* contract for the storeActionsConfig configuration
|
|
83
91
|
*/
|
|
84
|
-
export interface ActionCollectionConfig<State, Metadata extends BaseMetadata
|
|
92
|
+
export interface ActionCollectionConfig<State, Metadata extends BaseMetadata, ThisAPI = Record<string, (...parameters: any[]) => unknown>> {
|
|
85
93
|
readonly [key: string]: {
|
|
86
94
|
(this: ThisAPI, ...parameters: any[]): (this: ThisAPI, storeTools: StoreTools<State, Metadata, Record<string, (...parameters: any[]) => unknown | void>>) => unknown | void;
|
|
87
95
|
};
|
|
88
96
|
}
|
|
89
|
-
export type ActionCollectionResult<State, Metadata extends BaseMetadata
|
|
97
|
+
export type ActionCollectionResult<State, Metadata extends BaseMetadata, ActionsConfig extends ActionCollectionConfig<State, Metadata>> = {
|
|
90
98
|
[key in keyof ActionsConfig]: {
|
|
91
99
|
(...params: Parameters<ActionsConfig[key]>): ReturnType<ReturnType<ActionsConfig[key]>>;
|
|
92
100
|
};
|
|
93
101
|
};
|
|
94
|
-
|
|
102
|
+
/**
|
|
103
|
+
* Callbacks for the global store lifecycle events
|
|
104
|
+
*/
|
|
105
|
+
export type GlobalStoreCallbacks<State, Metadata extends BaseMetadata> = {
|
|
106
|
+
/**
|
|
107
|
+
* @description Called when the store is initialized
|
|
108
|
+
*/
|
|
95
109
|
onInit?: (args: StoreTools<State, Metadata>) => void;
|
|
110
|
+
/**
|
|
111
|
+
* @description Called when the state has changed
|
|
112
|
+
*/
|
|
96
113
|
onStateChanged?: (args: StoreTools<State, Metadata> & StateChanges<State>) => void;
|
|
114
|
+
/**
|
|
115
|
+
* @description Called when a new subscription is created
|
|
116
|
+
*/
|
|
97
117
|
onSubscribed?: (args: StoreTools<State, Metadata>, subscription: SubscriberParameters) => void;
|
|
118
|
+
/**
|
|
119
|
+
* @description Called to determine whether to prevent a state change
|
|
120
|
+
*/
|
|
98
121
|
computePreventStateChange?: (args: StoreTools<State, Metadata> & StateChanges<State>) => boolean;
|
|
122
|
+
/**
|
|
123
|
+
* @description Called when the store is unmounted, only applicable in context stores
|
|
124
|
+
*/
|
|
125
|
+
onUnMount?: (store: StateApi<State, any, any, Metadata>) => void;
|
|
99
126
|
};
|
|
100
127
|
export type UseHookConfig<State, TRoot = any> = {
|
|
101
128
|
isEqual?: (current: State, next: State) => boolean;
|
|
@@ -114,24 +141,24 @@ export type SubscribeCallbackConfig<State> = UseHookConfig<State> & {
|
|
|
114
141
|
*/
|
|
115
142
|
export type SubscribeCallback<State> = (state: State) => void;
|
|
116
143
|
/**
|
|
117
|
-
*
|
|
144
|
+
* @description Subscribe to the state changes
|
|
145
|
+
* You can subscribe to the whole state or to a fragment of the state by passing a selector as first parameter
|
|
146
|
+
* This can be used in non react environments to listen to the state changes
|
|
118
147
|
*/
|
|
119
|
-
export type
|
|
120
|
-
(): State;
|
|
148
|
+
export type SubscribeToState<State> = {
|
|
121
149
|
(subscription: SubscribeCallback<State>, config?: SubscribeCallbackConfig<State>): UnsubscribeCallback;
|
|
122
150
|
<TDerivate>(selector: SelectorCallback<State, TDerivate>, subscription: SubscribeCallback<TDerivate>, config?: SubscribeCallbackConfig<TDerivate>): UnsubscribeCallback;
|
|
123
151
|
};
|
|
124
152
|
export type BaseMetadata = Record<string, unknown>;
|
|
125
|
-
export type MetadataGetter<Metadata extends BaseMetadata
|
|
126
|
-
export type CustomGlobalHookBuilderParams<TCustomConfig extends BaseMetadata | unknown, Metadata extends BaseMetadata
|
|
153
|
+
export type MetadataGetter<Metadata extends BaseMetadata> = () => Metadata;
|
|
154
|
+
export type CustomGlobalHookBuilderParams<TCustomConfig extends BaseMetadata | unknown, Metadata extends BaseMetadata> = {
|
|
127
155
|
onInitialize?: (args: StoreTools<unknown, Metadata, unknown>, config: TCustomConfig | undefined) => void;
|
|
128
156
|
onChange?: (args: StoreTools<unknown, Metadata, unknown> & StateChanges<unknown>, config: TCustomConfig | undefined) => void;
|
|
129
157
|
};
|
|
130
158
|
export type SelectorCallback<State, TDerivate> = (state: State) => TDerivate;
|
|
131
159
|
export type SubscriberParameters = {
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
getConfig: () => UseHookConfig<unknown> | SubscribeCallbackConfig<unknown> | undefined;
|
|
160
|
+
selector: SelectorCallback<any, any> | undefined;
|
|
161
|
+
getConfig: () => UseHookConfig<any> | SubscribeCallbackConfig<any> | undefined;
|
|
135
162
|
currentState: unknown;
|
|
136
163
|
callback: SubscriptionCallback | (() => void);
|
|
137
164
|
};
|
package/types.js
CHANGED
|
@@ -1 +1,4 @@
|
|
|
1
|
-
var e;e=()
|
|
1
|
+
var e;e=()=>/******/(()=>{
|
|
2
|
+
/******/"use strict";var e={};
|
|
3
|
+
/******/return(()=>{var t=e;Object.defineProperty(t,"__esModule",{value:!0})})(),e;
|
|
4
|
+
/******/})(),"object"==typeof exports&&"object"==typeof module?module.exports=e():"function"==typeof define&&define.amd?define([],e):"object"==typeof exports?exports["react-hooks-global-states"]=e():this["react-hooks-global-states"]=e();
|
package/uniqueId.js
CHANGED
|
@@ -1 +1,16 @@
|
|
|
1
|
-
var e;e=()
|
|
1
|
+
var e;e=()=>/******/(()=>{
|
|
2
|
+
/******/"use strict";
|
|
3
|
+
/******/var e={};
|
|
4
|
+
/************************************************************************/
|
|
5
|
+
/******/
|
|
6
|
+
/******/
|
|
7
|
+
/******/
|
|
8
|
+
/******/
|
|
9
|
+
/******/
|
|
10
|
+
/******/
|
|
11
|
+
/******/
|
|
12
|
+
/******/
|
|
13
|
+
/***/return((e,t)=>{var o;Object.defineProperty(t,"__esModule",{value:!0}),t.uniqueId=void 0,t.uniqueId=(o=0,function(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:"";return o===Number.MAX_SAFE_INTEGER&&(o=0),e+Date.now().toString(36)+(o++).toString(36)}),t.default=t.uniqueId})
|
|
14
|
+
/***/
|
|
15
|
+
/******/(0,e),e;
|
|
16
|
+
/******/})(),"object"==typeof exports&&"object"==typeof module?module.exports=e():"function"==typeof define&&define.amd?define([],e):"object"==typeof exports?exports["react-hooks-global-states"]=e():this["react-hooks-global-states"]=e();
|
package/webpack.config.js
CHANGED
|
@@ -5,15 +5,12 @@ const individualEntries = {
|
|
|
5
5
|
createContext: './src/createContext.ts',
|
|
6
6
|
GlobalStore: './src/GlobalStore.ts',
|
|
7
7
|
GlobalStoreAbstract: './src/GlobalStoreAbstract.ts',
|
|
8
|
-
createCustomGlobalState: './src/createCustomGlobalState.ts',
|
|
9
8
|
createGlobalState: './src/createGlobalState.ts',
|
|
10
9
|
types: './src/types.ts',
|
|
11
10
|
isRecord: './src/isRecord.ts',
|
|
12
11
|
shallowCompare: './src/shallowCompare.ts',
|
|
13
12
|
throwWrongKeyOnActionCollectionConfig: './src/throwWrongKeyOnActionCollectionConfig.ts',
|
|
14
13
|
uniqueId: './src/uniqueId.ts',
|
|
15
|
-
useStableState: './src/useStableState.ts',
|
|
16
|
-
generateStackHash: './src/generateStackHash.ts',
|
|
17
14
|
};
|
|
18
15
|
|
|
19
16
|
const getExternalsForEntries = () => {
|
|
@@ -106,8 +103,11 @@ module.exports = {
|
|
|
106
103
|
toplevel: true,
|
|
107
104
|
properties: false,
|
|
108
105
|
},
|
|
109
|
-
|
|
110
|
-
comments
|
|
106
|
+
output: {
|
|
107
|
+
// Keep only JSDoc-style comments (/** ... */)
|
|
108
|
+
comments: (_node, comment) => {
|
|
109
|
+
return comment.type === 'comment2' && comment.value.startsWith('*');
|
|
110
|
+
},
|
|
111
111
|
},
|
|
112
112
|
},
|
|
113
113
|
}),
|
|
@@ -1,23 +0,0 @@
|
|
|
1
|
-
import type { ActionCollectionConfig, StateSetter, ActionCollectionResult, StateHook, CustomGlobalHookBuilderParams, BaseMetadata, GlobalStoreCallbacks } from './types';
|
|
2
|
-
export interface CustomCreateGlobalState<TCustomConfig extends BaseMetadata | unknown, InheritMetadata extends BaseMetadata | unknown = BaseMetadata> {
|
|
3
|
-
<State>(state: State): StateHook<State, StateSetter<State>, BaseMetadata>;
|
|
4
|
-
<State, Metadata extends BaseMetadata | unknown, ActionsConfig extends ActionCollectionConfig<State, InheritMetadata & Metadata> | null | {}, PublicStateMutator = keyof ActionsConfig extends never | undefined ? StateSetter<State> : ActionCollectionResult<State, InheritMetadata & Metadata, NonNullable<ActionsConfig>>>(state: State, args: {
|
|
5
|
-
name?: string;
|
|
6
|
-
metadata?: Metadata;
|
|
7
|
-
callbacks?: GlobalStoreCallbacks<State, InheritMetadata & Metadata>;
|
|
8
|
-
actions?: ActionsConfig;
|
|
9
|
-
config?: TCustomConfig;
|
|
10
|
-
}): StateHook<State, PublicStateMutator, InheritMetadata & Metadata>;
|
|
11
|
-
<State, Metadata extends BaseMetadata | unknown, ActionsConfig extends Readonly<ActionCollectionConfig<State, InheritMetadata & Metadata>>>(state: State, args: {
|
|
12
|
-
name?: string;
|
|
13
|
-
metadata?: Metadata;
|
|
14
|
-
callbacks?: GlobalStoreCallbacks<State, InheritMetadata & Metadata>;
|
|
15
|
-
actions: ActionsConfig;
|
|
16
|
-
config?: TCustomConfig;
|
|
17
|
-
}): StateHook<State, ActionCollectionResult<State, InheritMetadata & Metadata, ActionsConfig>, InheritMetadata & Metadata>;
|
|
18
|
-
}
|
|
19
|
-
/**
|
|
20
|
-
* @description Simple custom global state hook builder
|
|
21
|
-
*/
|
|
22
|
-
export declare const createCustomGlobalState: <TCustomConfig extends BaseMetadata | unknown, InheritMetadata extends BaseMetadata | unknown = BaseMetadata>({ onInitialize, onChange, }: CustomGlobalHookBuilderParams<TCustomConfig, InheritMetadata>) => CustomCreateGlobalState<TCustomConfig, InheritMetadata>;
|
|
23
|
-
export default createCustomGlobalState;
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
var e;e=e=>(()=>{"use strict";var t={154:(e,t,o)=>{Object.defineProperty(t,"__esModule",{value:!0}),t.createCustomGlobalState=void 0;var a=o(670);t.createCustomGlobalState=function(e){var t=e.onInitialize,o=e.onChange;return function(e){var l=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{},n=l.callbacks,r=function(e,t){var o={};for(var a in e)Object.prototype.hasOwnProperty.call(e,a)&&t.indexOf(a)<0&&(o[a]=e[a]);if(null!=e&&"function"==typeof Object.getOwnPropertySymbols){var l=0;for(a=Object.getOwnPropertySymbols(e);l<a.length;l++)t.indexOf(a[l])<0&&Object.prototype.propertyIsEnumerable.call(e,a[l])&&(o[a[l]]=e[a[l]])}return o}(l,["callbacks"]);return(0,a.createGlobalState)(e,Object.assign(Object.assign({},r),{callbacks:Object.assign(Object.assign({},null!=n?n:{}),{onInit:function(e){var o;null==t||t(e,null==r?void 0:r.config),null===(o=null==n?void 0:n.onInit)||void 0===o||o.call(n,e)},onStateChanged:function(e){var t;null==o||o(e,null==r?void 0:r.config),null===(t=null==n?void 0:n.onStateChanged)||void 0===t||t.call(n,e)}})}))}},t.default=t.createCustomGlobalState},670:t=>{t.exports=e}},o={};return function e(a){var l=o[a];if(void 0!==l)return l.exports;var n=o[a]={exports:{}};return t[a](n,n.exports,e),n.exports}(154)})(),"object"==typeof exports&&"object"==typeof module?module.exports=e(require("./createGlobalState.js")):"function"==typeof define&&define.amd?define(["./createGlobalState.js"],e):"object"==typeof exports?exports["react-hooks-global-states"]=e(require("./createGlobalState.js")):this["react-hooks-global-states"]=e(this["./createGlobalState.js"]);
|
package/generateStackHash.d.ts
DELETED
package/generateStackHash.js
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
var e;e=()=>(()=>{"use strict";var e={};return((e,t)=>{Object.defineProperty(t,"__esModule",{value:!0}),t.generateStackHash=void 0,t.generateStackHash=function(e){if(!e.trim())return"unknown";e=e.replace(/\s/g,"");for(var t=2166136261,o=0;o<e.length;o++)t^=e.charCodeAt(o),t*=16777619,t&=4294967295;return function(e){for(var t="";e>0;)t="0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"[e%62]+t,e=Math.floor(e/62);return t||"0"}(t>>>0)},t.default=t.generateStackHash})(0,e),e})(),"object"==typeof exports&&"object"==typeof module?module.exports=e():"function"==typeof define&&define.amd?define([],e):"object"==typeof exports?exports["react-hooks-global-states"]=e():this["react-hooks-global-states"]=e();
|