react-hooks-global-states 15.0.1 → 15.0.3
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/createContext.d.ts +31 -21
- package/createContext.js +1 -1
- package/createGlobalState.d.ts +11 -2
- package/index.d.ts +1 -1
- package/package.json +1 -1
package/createContext.d.ts
CHANGED
|
@@ -1,11 +1,33 @@
|
|
|
1
1
|
import React, { type PropsWithChildren, Context as ReactContext } from 'react';
|
|
2
|
+
import { GlobalStore } from './GlobalStore';
|
|
2
3
|
import type { BaseMetadata, GlobalStoreCallbacks as GlobalStoreCallbacksBase, UseHookOptions, AnyFunction, StoreTools, ReadonlyStateApi, SelectHook, ObservableFragment, StateApi, UnsubscribeCallback } from './types';
|
|
3
|
-
|
|
4
|
+
type GlobalStoreContextCallbacks<State, StateMutator, Metadata extends BaseMetadata> = {
|
|
4
5
|
/**
|
|
5
|
-
* @description
|
|
6
|
+
* @description Optional callback invoked after the context is created,
|
|
7
|
+
*/
|
|
8
|
+
onCreated?: (
|
|
9
|
+
/**
|
|
10
|
+
* @description Full context instance
|
|
11
|
+
*/
|
|
12
|
+
storeTools: ContextStoreTools<State, StateMutator extends AnyFunction ? null : StateMutator, Metadata>,
|
|
13
|
+
/**
|
|
14
|
+
* @description Underlying store instance
|
|
15
|
+
*/
|
|
16
|
+
store: GlobalStore<State, Metadata, unknown, any>) => void;
|
|
17
|
+
/**
|
|
18
|
+
* @description Called when the context provider is mounted
|
|
19
|
+
*/
|
|
20
|
+
onMounted?: (
|
|
21
|
+
/**
|
|
22
|
+
* @description Full context instance
|
|
23
|
+
*/
|
|
24
|
+
storeTools: ContextStoreTools<State, StateMutator extends AnyFunction ? null : StateMutator, Metadata>,
|
|
25
|
+
/**
|
|
26
|
+
* @description Underlying store instance
|
|
6
27
|
*/
|
|
7
|
-
|
|
28
|
+
store: GlobalStore<State, Metadata, unknown, any>) => void | UnsubscribeCallback;
|
|
8
29
|
};
|
|
30
|
+
export type GlobalStoreCallbacks<State, StateMutator, Metadata extends BaseMetadata> = GlobalStoreCallbacksBase<State, StateMutator, Metadata> & GlobalStoreContextCallbacks<State, StateMutator, Metadata>;
|
|
9
31
|
/**
|
|
10
32
|
* @description Resulting type of the action collection configuration
|
|
11
33
|
*/
|
|
@@ -84,15 +106,7 @@ export type ContextProviderExtensions<State, StateMutator, Metadata extends Base
|
|
|
84
106
|
* @description Optional initial state or initializer function, useful for testing, storybooks, etc.
|
|
85
107
|
*/
|
|
86
108
|
value?: State | ((initialValue: State) => State);
|
|
87
|
-
|
|
88
|
-
* @description Optional callback invoked after the context is created,
|
|
89
|
-
*/
|
|
90
|
-
onCreated?: (
|
|
91
|
-
/**
|
|
92
|
-
* @description Full context instance
|
|
93
|
-
*/
|
|
94
|
-
context: ContextStoreTools<State, StateMutator extends AnyFunction ? null : StateMutator, Metadata>) => void;
|
|
95
|
-
}) => {
|
|
109
|
+
} & GlobalStoreContextCallbacks<State, StateMutator extends AnyFunction ? null : StateMutator, Metadata>) => {
|
|
96
110
|
/**
|
|
97
111
|
* Provider for the context
|
|
98
112
|
*/
|
|
@@ -110,6 +124,10 @@ export type ContextProviderExtensions<State, StateMutator, Metadata extends Base
|
|
|
110
124
|
* @description Current context value
|
|
111
125
|
*/
|
|
112
126
|
current: ContextStoreTools<State, StateMutator extends AnyFunction ? null : StateMutator, Metadata>;
|
|
127
|
+
/**
|
|
128
|
+
* @description Underlying store instance
|
|
129
|
+
*/
|
|
130
|
+
instance: GlobalStore<State, Metadata, unknown, any>;
|
|
113
131
|
};
|
|
114
132
|
};
|
|
115
133
|
};
|
|
@@ -123,15 +141,7 @@ export type ContextProvider<State, StateMutator, Metadata extends BaseMetadata>
|
|
|
123
141
|
* @description Optional initial state or initializer function, useful for testing, storybooks, etc.
|
|
124
142
|
*/
|
|
125
143
|
value?: State | ((initialValue: State) => State);
|
|
126
|
-
|
|
127
|
-
* @description Optional callback invoked after the context is created,
|
|
128
|
-
*/
|
|
129
|
-
onCreated?: (
|
|
130
|
-
/**
|
|
131
|
-
* @description Full context instance
|
|
132
|
-
*/
|
|
133
|
-
context: ContextStoreTools<State, StateMutator extends AnyFunction ? null : StateMutator, Metadata>) => void;
|
|
134
|
-
}>> & ContextProviderExtensions<State, StateMutator, Metadata>;
|
|
144
|
+
} & GlobalStoreContextCallbacks<State, StateMutator extends AnyFunction ? null : StateMutator, Metadata>>> & ContextProviderExtensions<State, StateMutator, Metadata>;
|
|
135
145
|
/**
|
|
136
146
|
* @description Represents a hook that returns a readonly state.
|
|
137
147
|
*/
|
package/createContext.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
var e,t;e=this,t=(e,t,r,o,n)=>(()=>{"use strict";var a={78:t=>{t.exports=e},155:e=>{e.exports=t},506:e=>{e.exports=r},639:(e,t,r)=>{var o=function(e){return e&&e.__esModule?e:{default:e}};Object.defineProperty(t,"__esModule",{value:!0}),t.createContext=void 0;var
|
|
1
|
+
var e,t;e=this,t=(e,t,r,o,n)=>(()=>{"use strict";var a={78:t=>{t.exports=e},155:e=>{e.exports=t},506:e=>{e.exports=r},639:(e,t,r)=>{var o=function(e,t){var r={};for(var o in e)Object.prototype.hasOwnProperty.call(e,o)&&t.indexOf(o)<0&&(r[o]=e[o]);if(null!=e&&"function"==typeof Object.getOwnPropertySymbols){var n=0;for(o=Object.getOwnPropertySymbols(e);n<o.length;n++)t.indexOf(o[n])<0&&Object.prototype.propertyIsEnumerable.call(e,o[n])&&(r[o[n]]=e[o[n]])}return r},n=function(e){return e&&e.__esModule?e:{default:e}};Object.defineProperty(t,"__esModule",{value:!0}),t.createContext=void 0;var a=r(155),u=r(778),i=n(r(506)),l=n(r(773)),s=n(r(78));function c(e,t){var r,o=this,n=null!==(r=null==t?void 0:t.name)&&void 0!==r?r:(0,s.default)(),i=function(){var r=o.api();return(0,a.useDebugValue)("".concat(o.displayName,":selector")),(0,a.useDebugValue)("".concat(n,":use")),(0,a.useMemo)((function(){return r.createSelectorHook(e,t)}),[r]).apply(void 0,arguments)},l={displayName:this.displayName,createSelectorHook:c.bind(i),api:function(){(0,a.useDebugValue)("".concat(o.displayName,":selector")),(0,a.useDebugValue)("".concat(n,":api"));var r=o.api();return(0,a.useMemo)((function(){var o=r.createObservable(e,t);return Object.assign(Object.assign({},r),{getState:function(){return o.getState()},subscribe:o.subscribe.bind(o),createSelectorHook:u.createSelectorHook.bind(o),createObservable:u.createObservable.bind(o)})}),[r])}};return Object.assign(i,l),i}t.createContext=function(e){var t,r=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{},n=(0,a.createContext)(null),d=null!==(t=r.name)&&void 0!==t?t:(0,s.default)(),v=function(t){var s=function(t){var n=t.value,s=o(t,["value"]);(0,a.useDebugValue)("".concat(d,":Provider"));var c=(0,a.useMemo)((function(){var t,o,a,c,d=function(){return(0,i.default)(e)?e():e},v=(0,l.default)(n)?d():(0,i.default)(n)?n(d()):n,f=new u.GlobalStore(v,Object.assign(Object.assign({},r),{metadata:null!==(t=(0,i.default)(r.metadata)?r.metadata():r.metadata)&&void 0!==t?t:{}})),p={use:b};return Object.assign(f.storeTools,p),null===(a=null===(o=r.callbacks)||void 0===o?void 0:o.onCreated)||void 0===a||a.call(o,f.storeTools,f),null===(c=s.onCreated)||void 0===c||c.call(s,f.storeTools,f),f}),[]);return(0,a.useEffect)((function(){var e,t,o,n=[null===(t=null===(e=r.callbacks)||void 0===e?void 0:e.onMounted)||void 0===t?void 0:t.call(e,c.storeTools,c),null===(o=s.onMounted)||void 0===o?void 0:o.call(s,c.storeTools,c)].filter(Boolean);return function(){var e,t,r,o;null===(t=null===(e=c.callbacks)||void 0===e?void 0:e.onUnMount)||void 0===t||t.call(e,c),n.forEach((function(e){return null==e?void 0:e()})),null===(o=(r=c).__onUnMountContext)||void 0===o||o.call(r,c)}}),[c]),c}(t);return(0,a.createElement)(n.Provider,{value:s.use},t.children)};v.displayName="".concat(d,":Provider"),n.displayName="".concat(d);var f={makeProviderWrapper:function(e){var t={current:void 0,instance:void 0};return{wrapper:function(r){var n=r.children,u=o(r,["children"]);return(0,a.createElement)(v,Object.assign(Object.assign(Object.assign({},e),u),{onCreated:function(r,o){var n;t.current=r,t.instance=o,null===(n=null==e?void 0:e.onCreated)||void 0===n||n.call(e,r,o)}}),n)},context:t}}},b=function(){(0,a.useDebugValue)("".concat(d,":use"));var e=(0,a.useContext)(n);if(!e)throw new Error("use hook must be used within a ContextProvider");return e.apply(void 0,arguments)},p=function(){(0,a.useDebugValue)("".concat(d,":api"));var e=(0,a.useContext)(n);if(!e)throw new Error("api hook must be used within a ContextProvider");return e},m={displayName:n.displayName,createSelectorHook:c.bind(b),api:p,select:function(){return b.apply(void 0,arguments)[0]},observable:function(){(0,a.useDebugValue)("".concat(d,":observable"));for(var e=p(),t=arguments.length,r=new Array(t),o=0;o<t;o++)r[o]=arguments[o];var n=(0,a.useRef)(r);return n.current=r,(0,a.useMemo)((function(){var t,r,o;return e.createObservable((function(e){return(0,n.current[0])(e)}),{isEqualRoot:(null===(t=n.current[1])||void 0===t?void 0:t.isEqualRoot)?function(e,t){var r=n.current[1].isEqualRoot;return Boolean(r(e,t))}:void 0,isEqual:(null===(r=n.current[1])||void 0===r?void 0:r.isEqual)?function(e,t){var r=n.current[1].isEqual;return Boolean(r(e,t))}:void 0,name:null===(o=n.current[1])||void 0===o?void 0:o.name})}),[e])},actions:function(){return p().actions}};return Object.assign(v,f),Object.assign(b,m),{use:b,Provider:v,Context:n}},t.default=t.createContext},773:e=>{e.exports=o},778:e=>{e.exports=n}},u={};return function e(t){var r=u[t];if(void 0!==r)return r.exports;var o=u[t]={exports:{}};return a[t](o,o.exports,e),o.exports}(639)})(),"object"==typeof exports&&"object"==typeof module?module.exports=t(require("./uniqueId.js"),require("react"),require("json-storage-formatter/isFunction"),require("json-storage-formatter/isNil"),require("./GlobalStore.js")):"function"==typeof define&&define.amd?define(["./uniqueId.js","react","json-storage-formatter/isFunction","json-storage-formatter/isNil","./GlobalStore.js"],t):"object"==typeof exports?exports["react-hooks-global-states"]=t(require("./uniqueId.js"),require("react"),require("json-storage-formatter/isFunction"),require("json-storage-formatter/isNil"),require("./GlobalStore.js")):e["react-hooks-global-states"]=t(e["./uniqueId.js"],e.react,e["json-storage-formatter/isFunction"],e["json-storage-formatter/isNil"],e["./GlobalStore.js"]);
|
package/createGlobalState.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { ActionCollectionConfig, ActionCollectionResult, StateHook, BaseMetadata, GlobalStoreCallbacks } from './types';
|
|
1
|
+
import type { ActionCollectionConfig, ActionCollectionResult, StateHook, BaseMetadata, GlobalStoreCallbacks, StoreTools } from './types';
|
|
2
2
|
interface CreateGlobalState {
|
|
3
3
|
/**
|
|
4
4
|
* Creates a global state hook.
|
|
@@ -159,5 +159,14 @@ export declare const createGlobalState: CreateGlobalState;
|
|
|
159
159
|
* type CounterActions = InferActionsType<typeof useCounter>;
|
|
160
160
|
* ```
|
|
161
161
|
*/
|
|
162
|
-
export type InferActionsType<Hook extends StateHook<any, any, any>> = ReturnType<Hook
|
|
162
|
+
export type InferActionsType<Hook extends StateHook<any, any, any>> = ReturnType<Hook>['1'];
|
|
163
|
+
/**
|
|
164
|
+
* Infers the StoreTools type from a StateHook, useful to split actions code
|
|
165
|
+
*
|
|
166
|
+
* @example
|
|
167
|
+
* ```ts
|
|
168
|
+
* type CounterStoreTools = InferStateApi<typeof useCounter>;
|
|
169
|
+
* ```
|
|
170
|
+
*/
|
|
171
|
+
export type InferStateApi<Hook extends StateHook<any, any, any>> = Hook extends StateHook<infer State, infer PublicStateMutator, infer Metadata> ? StoreTools<State, PublicStateMutator, Metadata> : never;
|
|
163
172
|
export default createGlobalState;
|
package/index.d.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
export type { StateApi, ObservableFragment, MetadataSetter, StateChanges, StoreTools, ActionCollectionResult, GlobalStoreCallbacks, UseHookOptions, UnsubscribeCallback, SubscribeCallbackConfig, SubscribeCallback, BaseMetadata, MetadataGetter, SelectorCallback, SubscriberParameters, SubscriptionCallback, StateHook, ActionCollectionConfig, } from './types';
|
|
2
2
|
export { GlobalStore } from './GlobalStore';
|
|
3
3
|
export { GlobalStoreAbstract } from './GlobalStoreAbstract';
|
|
4
|
-
export { createGlobalState, type InferActionsType } from './createGlobalState';
|
|
4
|
+
export { createGlobalState, type InferActionsType, type InferStateApi } from './createGlobalState';
|
|
5
5
|
export { shallowCompare } from './shallowCompare';
|
|
6
6
|
export { uniqueId } from './uniqueId';
|
|
7
7
|
export { throwWrongKeyOnActionCollectionConfig } from './throwWrongKeyOnActionCollectionConfig';
|
package/package.json
CHANGED