react-hooks-global-states 15.0.4 → 15.0.5

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.
@@ -1,6 +1,7 @@
1
1
  import React, { type PropsWithChildren, Context as ReactContext } from 'react';
2
2
  import { GlobalStore } from './GlobalStore';
3
3
  import type { BaseMetadata, GlobalStoreCallbacks as GlobalStoreCallbacksBase, UseHookOptions, AnyFunction, StoreTools, ReadonlyStateApi, SelectHook, ObservableFragment, StateApi, UnsubscribeCallback } from './types';
4
+ import { AnyActions } from 'createGlobalState';
4
5
  type GlobalStoreContextCallbacks<State, StateMutator, Metadata extends BaseMetadata> = {
5
6
  /**
6
7
  * @description Optional callback invoked after the context is created,
@@ -405,7 +406,7 @@ interface CreateContext {
405
406
  args: {
406
407
  name?: string;
407
408
  metadata?: Metadata | (() => Metadata);
408
- callbacks?: GlobalStoreCallbacks<State, StateMutator, Metadata>;
409
+ callbacks?: GlobalStoreCallbacks<State, AnyActions, Metadata>;
409
410
  actions?: ActionsConfig;
410
411
  }): {
411
412
  /**
@@ -469,7 +470,7 @@ interface CreateContext {
469
470
  * - **`Context`** — The raw React `Context` object for advanced usage, such as integration with
470
471
  * external tools or non-React consumers.
471
472
  */
472
- <State, Metadata extends BaseMetadata, ActionsConfig extends ContextActionCollectionConfig<State, Metadata>, StateMutator = React.Dispatch<React.SetStateAction<State>>>(
473
+ <State, Metadata extends BaseMetadata, ActionsConfig extends ContextActionCollectionConfig<State, Metadata>>(
473
474
  /**
474
475
  * @description Initial state value or initializer function.
475
476
  */
@@ -484,7 +485,7 @@ interface CreateContext {
484
485
  args: {
485
486
  name?: string;
486
487
  metadata?: Metadata | (() => Metadata);
487
- callbacks?: GlobalStoreCallbacks<State, StateMutator, Metadata>;
488
+ callbacks?: GlobalStoreCallbacks<State, AnyActions, Metadata>;
488
489
  actions: ActionsConfig;
489
490
  }): {
490
491
  /**
@@ -1,4 +1,28 @@
1
1
  import type { ActionCollectionConfig, ActionCollectionResult, StateHook, BaseMetadata, GlobalStoreCallbacks, StoreTools } from './types';
2
+ /**
3
+ * Typescript is unable to infer the actions type correctly for the lifecycle callbacks
4
+ * so we use a generic AnyActions type here to bypass that limitation.
5
+ *
6
+ * The parameter could still be typed before using it with
7
+ * ```ts
8
+ * type StoreTools = InferStateApi<typeof <hook>>;
9
+ *
10
+ * onInit: (tools) => {
11
+ * const storeTools = tools as StoreTools;
12
+ * // ...
13
+ * }
14
+ *
15
+ * or when dealing with context:
16
+ *
17
+ * type ContextApi = InferContextApi<typeof <context>>;
18
+ *
19
+ * onInit: (tools) => {
20
+ * const storeTools = tools as ContextApi;
21
+ * // ...
22
+ * }
23
+ * ```
24
+ */
25
+ export type AnyActions = Record<string, (...args: any[]) => any>;
2
26
  interface CreateGlobalState {
3
27
  /**
4
28
  * Creates a global state hook.
@@ -89,7 +113,7 @@ interface CreateGlobalState {
89
113
  <State, Metadata extends BaseMetadata, ActionsConfig extends ActionCollectionConfig<State, Metadata> | null | {}, PublicStateMutator = keyof ActionsConfig extends never | undefined ? React.Dispatch<React.SetStateAction<State>> : ActionCollectionResult<State, Metadata, NonNullable<ActionsConfig>>>(state: State, args: {
90
114
  name?: string;
91
115
  metadata?: Metadata;
92
- callbacks?: GlobalStoreCallbacks<State, PublicStateMutator, Metadata>;
116
+ callbacks?: GlobalStoreCallbacks<State, AnyActions, Metadata>;
93
117
  actions?: ActionsConfig;
94
118
  }): StateHook<State, PublicStateMutator, Metadata>;
95
119
  /**
@@ -144,7 +168,7 @@ interface CreateGlobalState {
144
168
  <State, Metadata extends BaseMetadata, ActionsConfig extends ActionCollectionConfig<State, Metadata>, PublicStateMutator = ActionCollectionResult<State, Metadata, NonNullable<ActionsConfig>>>(state: State, args: {
145
169
  name?: string;
146
170
  metadata?: Metadata;
147
- callbacks?: GlobalStoreCallbacks<State, PublicStateMutator, Metadata>;
171
+ callbacks?: GlobalStoreCallbacks<State, AnyActions, Metadata>;
148
172
  actions: ActionsConfig;
149
173
  }): StateHook<State, PublicStateMutator, Metadata>;
150
174
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "react-hooks-global-states",
3
- "version": "15.0.4",
3
+ "version": "15.0.5",
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",
package/types.d.ts CHANGED
@@ -180,7 +180,7 @@ export type StateChanges<State> = {
180
180
  **/
181
181
  export type StoreTools<State, StateMutator = React.Dispatch<React.SetStateAction<State>>, Metadata extends BaseMetadata = BaseMetadata> = {
182
182
  /**
183
- * The actions available for the global state
183
+ * The actions available for the global state if provided
184
184
  */
185
185
  actions: StateMutator extends AnyFunction ? null : StateMutator;
186
186
  /**