react-hooks-global-states 15.0.16 → 15.0.18
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/README.md +4 -0
- package/createContext.js +1 -1
- package/package.json +4 -4
- package/types.d.ts +24 -13
- package/uniqueId.d.ts +2 -2
package/README.md
CHANGED
|
@@ -905,6 +905,9 @@ const DataContext = createContext([], {
|
|
|
905
905
|
console.log('Provider unmounting!');
|
|
906
906
|
};
|
|
907
907
|
},
|
|
908
|
+
onRender: (store) => {
|
|
909
|
+
console.log('Provider rendered!');
|
|
910
|
+
},
|
|
908
911
|
},
|
|
909
912
|
});
|
|
910
913
|
|
|
@@ -917,6 +920,7 @@ function App() {
|
|
|
917
920
|
console.log('Mounted!');
|
|
918
921
|
return () => console.log('Cleanup!');
|
|
919
922
|
}}
|
|
923
|
+
onRender={(store) => console.log('Executes on every provider render!')}
|
|
920
924
|
>
|
|
921
925
|
<Content />
|
|
922
926
|
</DataContext.Provider>
|
package/createContext.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
var e,t;e=this,t=(e,t,
|
|
1
|
+
var e,t;e=this,t=(e,t,o,r)=>(()=>{"use strict";var a={12:t=>{t.exports=e},78:e=>{e.exports=t},506:e=>{e.exports=o},695:function(e,t,o){var r=this&&this.__rest||function(e,t){var o={};for(var r in e)Object.prototype.hasOwnProperty.call(e,r)&&t.indexOf(r)<0&&(o[r]=e[r]);if(null!=e&&"function"==typeof Object.getOwnPropertySymbols){var a=0;for(r=Object.getOwnPropertySymbols(e);a<r.length;a++)t.indexOf(r[a])<0&&Object.prototype.propertyIsEnumerable.call(e,r[a])&&(o[r[a]]=e[r[a]])}return o},a=this&&this.__importDefault||function(e){return e&&e.__esModule?e:{default:e}};Object.defineProperty(t,"__esModule",{value:!0}),t.createContext=void 0;const n=o(12),s=o(778),l=a(o(506)),u=a(o(78));function i(e,t){var o;const r=null!==(o=null==t?void 0:t.name)&&void 0!==o?o:(0,u.default)("sh:"),a=(...o)=>{const a=this.api();return(0,n.useDebugValue)(`${this.displayName}:selector`),(0,n.useDebugValue)(`${r}:use`),(0,n.useMemo)((()=>a.createSelectorHook(e,t)),[a])(...o)},l={displayName:this.displayName,createSelectorHook:i.bind(a),api:()=>{(0,n.useDebugValue)(`${this.displayName}:selector`),(0,n.useDebugValue)(`${r}:api`);const o=this.api();return(0,n.useMemo)((()=>{const r=o.createObservable(e,t);return Object.assign(Object.assign({},o),{getState:()=>r.getState(),subscribe:r.subscribe.bind(r),createSelectorHook:s.createSelectorHook.bind(r),createObservable:s.createObservable.bind(r)})}),[o])}};return Object.assign(a,l),a}t.createContext=(e,t={})=>{var o;const a=(0,n.createContext)(null),c=null!==(o=t.name)&&void 0!==o?o:(0,u.default)("ctx:"),d=()=>(0,l.default)(e)?e():e,v=e=>Object.prototype.hasOwnProperty.call(e,"value"),b=e=>{const o=(e=>{var o;(0,n.useDebugValue)(`${c}:Provider`);const[r]=(0,n.useState)((()=>{var o,r,a,n;const u=v(e)?(0,l.default)(e.value)?e.value(d()):e.value:d(),i=new s.GlobalStore(u,Object.assign(Object.assign({},t),{metadata:null!==(o=(0,l.default)(t.metadata)?t.metadata():t.metadata)&&void 0!==o?o:{}})),c={use:f};return Object.assign(i.storeTools,c),null===(a=null===(r=t.callbacks)||void 0===r?void 0:r.onCreated)||void 0===a||a.call(r,i.storeTools,i),null===(n=e.onCreated)||void 0===n||n.call(e,i.storeTools,i),i}));return(0,n.useLayoutEffect)((()=>{v(e)&&!(0,l.default)(e.value)&&r.setState(e.value)}),[r,e.value]),(0,n.useEffect)((()=>{var o,a,n;const s=[null===(a=null===(o=t.callbacks)||void 0===o?void 0:o.onMounted)||void 0===a?void 0:a.call(o,r.storeTools,r),null===(n=e.onMounted)||void 0===n?void 0:n.call(e,r.storeTools,r)].filter(Boolean);return()=>{var e,t,o,a;null===(t=null===(e=r.callbacks)||void 0===e?void 0:e.onUnMount)||void 0===t||t.call(e,r),s.forEach((e=>null==e?void 0:e())),null===(a=(o=r).__onUnMountContext)||void 0===a||a.call(o,r)}}),[r]),null===(o=e.onRender)||void 0===o||o.call(e,r.storeTools,r),r})(e);return(0,n.createElement)(a.Provider,{value:o.use},e.children)};b.displayName=`${c}:Provider`,a.displayName=`${c}`;const p={makeProviderWrapper:e=>{const t={current:void 0,instance:void 0};return{wrapper:o=>{var{children:a}=o,s=r(o,["children"]);return(0,n.createElement)(b,Object.assign(Object.assign(Object.assign({},e),s),{onCreated:(o,r)=>{var a;t.current=o,t.instance=r,null===(a=null==e?void 0:e.onCreated)||void 0===a||a.call(e,o,r)}}),a)},context:t}}},f=(...e)=>{(0,n.useDebugValue)(`${c}:use`);const t=(0,n.useContext)(a);if(!t)throw new Error("use hook must be used within a ContextProvider");return t(...e)},m=()=>{(0,n.useDebugValue)(`${c}:api`);const e=(0,n.useContext)(a);if(!e)throw new Error("api hook must be used within a ContextProvider");return e},j={displayName:a.displayName,createSelectorHook:i.bind(f),api:m,select:(...e)=>f(...e)[0],observable:(...e)=>{(0,n.useDebugValue)(`${c}:observable`);const t=m(),o=(0,n.useRef)(e);return o.current=e,(0,n.useMemo)((()=>{var e,r,a;return t.createObservable((e=>(0,o.current[0])(e)),{isEqualRoot:(null===(e=o.current[1])||void 0===e?void 0:e.isEqualRoot)?(e,t)=>{const r=o.current[1].isEqualRoot;return Boolean(r(e,t))}:void 0,isEqual:(null===(r=o.current[1])||void 0===r?void 0:r.isEqual)?(e,t)=>{const r=o.current[1].isEqual;return Boolean(r(e,t))}:void 0,name:null===(a=o.current[1])||void 0===a?void 0:a.name})}),[t])},actions:()=>m().actions};return Object.assign(b,p),Object.assign(f,j),{use:f,Provider:b,Context:a}},t.default=t.createContext},778:e=>{e.exports=r}},n={};return function e(t){var o=n[t];if(void 0!==o)return o.exports;var r=n[t]={exports:{}};return a[t].call(r.exports,r,r.exports,e),r.exports}(695)})(),"object"==typeof exports&&"object"==typeof module?module.exports=t(require("react"),require("./uniqueId.js"),require("json-storage-formatter/isFunction"),require("./GlobalStore.js")):"function"==typeof define&&define.amd?define(["react","./uniqueId.js","json-storage-formatter/isFunction","./GlobalStore.js"],t):"object"==typeof exports?exports["react-hooks-global-states"]=t(require("react"),require("./uniqueId.js"),require("json-storage-formatter/isFunction"),require("./GlobalStore.js")):e["react-hooks-global-states"]=t(e.React,e["./uniqueId.js"],e["json-storage-formatter/isFunction"],e["./GlobalStore.js"]);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "react-hooks-global-states",
|
|
3
|
-
"version": "15.0.
|
|
3
|
+
"version": "15.0.18",
|
|
4
4
|
"description": "A package to easily handle global state across your React components using hooks.",
|
|
5
5
|
"main": "./bundle.js",
|
|
6
6
|
"types": "./index.d.ts",
|
|
@@ -68,13 +68,13 @@
|
|
|
68
68
|
],
|
|
69
69
|
"scripts": {
|
|
70
70
|
"format": "prettier --write . --log-level silent && yarn lint:fix",
|
|
71
|
-
"test": "yarn build && yarn jest",
|
|
71
|
+
"test": "yarn build && yarn ts-check:tests && yarn jest",
|
|
72
72
|
"test:logs": "sh -c 'yarn build && yarn jest \"$@\" 2>&1 | grep -i \"console\" -A 2' --",
|
|
73
73
|
"test:debug:no-watch": "node --inspect-brk node_modules/.bin/jest --no-watchman --runInBand",
|
|
74
74
|
"test:debug": "node --inspect-brk node_modules/.bin/jest --watch --runInBand",
|
|
75
|
-
"test:quick": "yarn test --no-coverage --maxWorkers=4 -c --no-watchman -u",
|
|
75
|
+
"test:quick": "yarn ts-check:tests && yarn test --no-coverage --maxWorkers=4 -c --no-watchman -u",
|
|
76
76
|
"test:coverage": "yarn test --maxWorkers=4 -c --colors --no-watchman --verbose --coverage",
|
|
77
|
-
"build": "yarn ts-check && yarn clean && webpack --config webpack.config.js
|
|
77
|
+
"build": "yarn ts-check && yarn clean && webpack --config webpack.config.js",
|
|
78
78
|
"prepare": "yarn format && npm run build && yarn test:quick",
|
|
79
79
|
"version": "npm run format && yarn build && yarn jest && git add -A",
|
|
80
80
|
"postversion": "git push && git push --tags",
|
package/types.d.ts
CHANGED
|
@@ -410,6 +410,18 @@ export type GlobalStoreContextCallbacks<State, StateMutator, Metadata extends Ba
|
|
|
410
410
|
* @description Underlying store instance
|
|
411
411
|
*/
|
|
412
412
|
store: GlobalStore<State, Metadata, unknown, Any>) => void | UnsubscribeCallback;
|
|
413
|
+
/**
|
|
414
|
+
* @description Called every time the context provider is rendered
|
|
415
|
+
*/
|
|
416
|
+
onRender?: (
|
|
417
|
+
/**
|
|
418
|
+
* @description Full context instance
|
|
419
|
+
*/
|
|
420
|
+
storeTools: ContextStoreTools<State, StateMutator extends AnyFunction ? null : StateMutator, Metadata>,
|
|
421
|
+
/**
|
|
422
|
+
* @description Underlying store instance
|
|
423
|
+
*/
|
|
424
|
+
store: GlobalStore<State, Metadata, unknown, Any>) => void;
|
|
413
425
|
};
|
|
414
426
|
export type ContextStoreCallbacks<State, StateMutator, Metadata extends BaseMetadata> = GlobalStoreCallbacks<State, StateMutator, Metadata> & GlobalStoreContextCallbacks<State, StateMutator, Metadata>;
|
|
415
427
|
/**
|
|
@@ -1139,12 +1151,11 @@ export type InferActionsType<Hook extends StateHook<Any, Any, Any>> = ReturnType
|
|
|
1139
1151
|
* ```
|
|
1140
1152
|
*/
|
|
1141
1153
|
export type InferStateApi<Hook extends StateHook<Any, Any, Any>> = Hook extends StateHook<infer State, infer PublicStateMutator, infer Metadata> ? StoreTools<State, PublicStateMutator, Metadata> : never;
|
|
1142
|
-
export declare const __uniqueIdBrand: unique symbol;
|
|
1143
1154
|
/**
|
|
1144
1155
|
* Branded unique identifier
|
|
1145
1156
|
*/
|
|
1146
1157
|
export type BrandedId<T extends string | undefined> = `${T extends string ? T : ''}${string}` & {
|
|
1147
|
-
|
|
1158
|
+
__brand: T;
|
|
1148
1159
|
};
|
|
1149
1160
|
export interface UniqueId {
|
|
1150
1161
|
/**
|
|
@@ -1152,10 +1163,10 @@ export interface UniqueId {
|
|
|
1152
1163
|
*
|
|
1153
1164
|
* @example
|
|
1154
1165
|
* uniqueId(); // "k9j3n5x8q2"
|
|
1155
|
-
* type Id1 = `${string}` & {
|
|
1166
|
+
* type Id1 = `${string}` & { __brand: undefined };
|
|
1156
1167
|
*
|
|
1157
1168
|
* uniqueId('user:'); // "user:k9j3n5x8q2"
|
|
1158
|
-
* type Id2 = `user:${string}` & {
|
|
1169
|
+
* type Id2 = `user:${string}` & { __brand: 'user:' };
|
|
1159
1170
|
*/
|
|
1160
1171
|
<T extends string | undefined>(prefix?: T): BrandedId<T>;
|
|
1161
1172
|
/**
|
|
@@ -1164,36 +1175,36 @@ export interface UniqueId {
|
|
|
1164
1175
|
* @example
|
|
1165
1176
|
* const makeOrderId = uniqueId.for('order:');
|
|
1166
1177
|
* const id = makeOrderId(); // "order:k9j3n5x8q2"
|
|
1167
|
-
* type OrderId = `order:${string}` & {
|
|
1178
|
+
* type OrderId = `order:${string}` & { __brand: 'order:' };
|
|
1168
1179
|
*/
|
|
1169
1180
|
for<T extends string>(prefix: T): {
|
|
1170
1181
|
(): `${T}${string}` & {
|
|
1171
|
-
|
|
1182
|
+
__brand: T;
|
|
1172
1183
|
};
|
|
1173
1184
|
/**
|
|
1174
1185
|
* Checks if the given value matches the branded ID for this prefix.
|
|
1175
1186
|
*/
|
|
1176
1187
|
is(value: unknown): value is `${T}${string}` & {
|
|
1177
|
-
|
|
1188
|
+
__brand: T;
|
|
1178
1189
|
};
|
|
1179
1190
|
/**
|
|
1180
1191
|
* Asserts that the value matches this branded ID, throws otherwise.
|
|
1181
1192
|
*/
|
|
1182
1193
|
assert(value: unknown): asserts value is `${T}${string}` & {
|
|
1183
|
-
|
|
1194
|
+
__brand: T;
|
|
1184
1195
|
};
|
|
1185
1196
|
/**
|
|
1186
1197
|
* Returns a strictly branded generator using a custom symbol brand.
|
|
1187
1198
|
*/
|
|
1188
1199
|
strict<Brand extends symbol>(): {
|
|
1189
1200
|
(): `${T}${string}` & {
|
|
1190
|
-
|
|
1201
|
+
__brand: Brand;
|
|
1191
1202
|
};
|
|
1192
1203
|
is(value: unknown): value is `${T}${string}` & {
|
|
1193
|
-
|
|
1204
|
+
__brand: Brand;
|
|
1194
1205
|
};
|
|
1195
1206
|
assert(value: unknown): asserts value is `${T}${string}` & {
|
|
1196
|
-
|
|
1207
|
+
__brand: Brand;
|
|
1197
1208
|
};
|
|
1198
1209
|
};
|
|
1199
1210
|
};
|
|
@@ -1201,13 +1212,13 @@ export interface UniqueId {
|
|
|
1201
1212
|
* Creates a reusable unique ID generator without a prefix.
|
|
1202
1213
|
*/
|
|
1203
1214
|
of<T extends string>(): () => string & {
|
|
1204
|
-
|
|
1215
|
+
__brand: T;
|
|
1205
1216
|
};
|
|
1206
1217
|
/**
|
|
1207
1218
|
* Creates a strictly branded unique ID generator without a prefix.
|
|
1208
1219
|
*/
|
|
1209
1220
|
strict<Brand extends symbol>(): () => string & {
|
|
1210
|
-
|
|
1221
|
+
__brand: Brand;
|
|
1211
1222
|
};
|
|
1212
1223
|
}
|
|
1213
1224
|
/**
|
package/uniqueId.d.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import type { UniqueId, BrandedId
|
|
1
|
+
import type { UniqueId, BrandedId } from './types';
|
|
2
2
|
/**
|
|
3
3
|
* Generates a unique identifier string, optionally prefixed.
|
|
4
4
|
*/
|
|
5
5
|
export declare const uniqueId: UniqueId;
|
|
6
6
|
export default uniqueId;
|
|
7
|
-
export type { UniqueId, BrandedId
|
|
7
|
+
export type { UniqueId, BrandedId };
|