@dr.pogodin/react-global-state 0.19.4 → 0.20.1

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,11 +1,10 @@
1
1
  // Hook for updates of global state.
2
2
 
3
- import { isFunction } from 'lodash';
4
- import { useEffect, useRef, useSyncExternalStore } from 'react';
3
+ import isFunction from 'lodash/isFunction.js';
4
+ import { useEffect, useRef, useState, useSyncExternalStore } from 'react';
5
5
  import { Emitter } from '@dr.pogodin/js-utils';
6
- import { getGlobalState } from "./GlobalStateProvider";
7
- import { cloneDeepForLog, isDebugMode } from "./utils";
8
-
6
+ import { getGlobalState } from "./GlobalStateProvider.js";
7
+ import { cloneDeepForLog, isDebugMode } from "./utils.js";
9
8
  /**
10
9
  * The primary hook for interacting with the global state, modeled after
11
10
  * the standard React's
@@ -56,13 +55,9 @@ import { cloneDeepForLog, isDebugMode } from "./utils";
56
55
  * Also, similar to the standard React's state setters, `setValue()` is
57
56
  * stable function: it does not change between component re-renders.
58
57
  */
59
-
60
58
  // "Enforced type overload"
61
-
62
59
  // "Entire state overload"
63
-
64
60
  // "State evaluation overload"
65
-
66
61
  function useGlobalState(path,
67
62
  // TODO: Revise it later!
68
63
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
@@ -71,74 +66,74 @@ initialValue
71
66
  // TODO: Revise it later!
72
67
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
73
68
  ) {
69
+ var _ref$current;
74
70
  const globalState = getGlobalState();
75
- const ref = useRef(undefined);
76
-
77
- // TODO: Revise how this `rc` variable is used, perhaps we can simplify stuff
78
- // here.
79
- let rc = ref.current;
80
- if (!ref.current) {
71
+ const ref = useRef(null);
72
+ const [stable] = useState(() => {
81
73
  const emitter = new Emitter();
82
- ref.current = {
83
- emitter,
84
- globalState,
85
- path,
86
- setter: value => {
87
- const newState = isFunction(value) ? value(rc.globalState.get(rc.path)) : value;
88
- if (process.env.NODE_ENV !== 'production' && isDebugMode()) {
89
- var _path, _path2;
90
- /* eslint-disable no-console */
91
- console.groupCollapsed(`ReactGlobalState - useGlobalState setter triggered for path ${(_path = rc.path) !== null && _path !== void 0 ? _path : ''}`);
92
- console.log('New value:', cloneDeepForLog(newState, (_path2 = rc.path) !== null && _path2 !== void 0 ? _path2 : ''));
93
- console.groupEnd();
94
- /* eslint-enable no-console */
95
- }
96
- rc.globalState.set(rc.path, newState);
97
-
98
- // NOTE: The regular global state's update notifications, automatically
99
- // triggered by the rc.globalState.set() call above, are batched, and
100
- // scheduled to fire asynchronosuly at a later time, which is problematic
101
- // for managed text inputs - if they have their value update delayed to
102
- // future render cycles, it will result in reset of their cursor position
103
- // to the value end. Calling the rc.emitter.emit() below causes a sooner
104
- // state update for the current component, thus working around the issue.
105
- // For additional details see the original issue:
106
- // https://github.com/birdofpreyru/react-global-state/issues/22
107
- if (newState !== rc.state) rc.emitter.emit();
108
- },
109
- state: isFunction(initialValue) ? initialValue() : initialValue,
110
- subscribe: emitter.addListener.bind(emitter),
111
- watcher: () => {
112
- // TODO: Revise it later.
113
- // eslint-disable-next-line @typescript-eslint/no-confusing-void-expression
114
- const state = rc.globalState.get(rc.path);
115
- if (state !== rc.state) rc.emitter.emit();
74
+ const setter = value => {
75
+ const rc = ref.current;
76
+ if (!rc) throw Error('Internal error');
77
+ const newState = isFunction(value) ? value(rc.globalState.get(rc.path)) : value;
78
+ if (process.env.NODE_ENV !== 'production' && isDebugMode()) {
79
+ var _rc$path, _rc$path2;
80
+ /* eslint-disable no-console */
81
+ console.groupCollapsed(`ReactGlobalState - useGlobalState setter triggered for path ${(_rc$path = rc.path) !== null && _rc$path !== void 0 ? _rc$path : ''}`);
82
+ console.log('New value:', cloneDeepForLog(newState, (_rc$path2 = rc.path) !== null && _rc$path2 !== void 0 ? _rc$path2 : ''));
83
+ console.groupEnd();
84
+ /* eslint-enable no-console */
116
85
  }
86
+ rc.globalState.set(rc.path, newState);
87
+
88
+ // NOTE: The regular global state's update notifications, automatically
89
+ // triggered by the rc.globalState.set() call above, are batched, and
90
+ // scheduled to fire asynchronosuly at a later time, which is problematic
91
+ // for managed text inputs - if they have their value update delayed to
92
+ // future render cycles, it will result in reset of their cursor position
93
+ // to the value end. Calling the rc.emitter.emit() below causes a sooner
94
+ // state update for the current component, thus working around the issue.
95
+ // For additional details see the original issue:
96
+ // https://github.com/birdofpreyru/react-global-state/issues/22
97
+ if (newState !== rc.prevValue) emitter.emit();
98
+ };
99
+ const subscribe = emitter.addListener.bind(emitter);
100
+ return {
101
+ emitter,
102
+ setter,
103
+ subscribe
117
104
  };
118
- }
119
- rc = ref.current;
120
- rc.globalState = globalState;
121
- rc.path = path;
122
- rc.state = useSyncExternalStore(rc.subscribe, () => rc.globalState.get(rc.path, {
105
+ });
106
+ const value = useSyncExternalStore(stable.subscribe, () => globalState.get(path, {
123
107
  initialValue
124
- }), () => rc.globalState.get(rc.path, {
108
+ }), () => globalState.get(path, {
125
109
  initialState: true,
126
110
  initialValue
127
111
  }));
112
+ (_ref$current = ref.current) !== null && _ref$current !== void 0 ? _ref$current : ref.current = {
113
+ globalState,
114
+ path,
115
+ prevValue: value
116
+ };
128
117
  useEffect(() => {
129
- const {
130
- watcher
131
- } = ref.current;
118
+ ref.current = {
119
+ globalState,
120
+ path,
121
+ prevValue: ref.current.prevValue
122
+ };
123
+ const watcher = () => {
124
+ const nextValue = globalState.get(path);
125
+ if (ref.current.prevValue !== nextValue) {
126
+ ref.current.prevValue = nextValue;
127
+ stable.emitter.emit();
128
+ }
129
+ };
132
130
  globalState.watch(watcher);
133
131
  watcher();
134
132
  return () => {
135
133
  globalState.unWatch(watcher);
136
134
  };
137
- }, [globalState]);
138
- useEffect(() => {
139
- ref.current.watcher();
140
- }, [path]);
141
- return [rc.state, rc.setter];
135
+ }, [globalState, stable.emitter, path]);
136
+ return [value, stable.setter];
142
137
  }
143
138
  export default useGlobalState;
144
139
 
@@ -1 +1 @@
1
- {"version":3,"file":"useGlobalState.js","names":["isFunction","useEffect","useRef","useSyncExternalStore","Emitter","getGlobalState","cloneDeepForLog","isDebugMode","useGlobalState","path","initialValue","globalState","ref","undefined","rc","current","emitter","setter","value","newState","get","process","env","NODE_ENV","_path","_path2","console","groupCollapsed","log","groupEnd","set","state","emit","subscribe","addListener","bind","watcher","initialState","watch","unWatch"],"sources":["../../src/useGlobalState.ts"],"sourcesContent":["// Hook for updates of global state.\n\nimport { isFunction } from 'lodash';\n\nimport {\n type Dispatch,\n type SetStateAction,\n useEffect,\n useRef,\n useSyncExternalStore,\n} from 'react';\n\nimport { Emitter } from '@dr.pogodin/js-utils';\n\nimport type GlobalState from './GlobalState';\nimport { getGlobalState } from './GlobalStateProvider';\n\nimport {\n type CallbackT,\n type ForceT,\n type LockT,\n type TypeLock,\n type ValueAtPathT,\n type ValueOrInitializerT,\n cloneDeepForLog,\n isDebugMode,\n} from './utils';\n\nexport type SetterT<T> = Dispatch<SetStateAction<T>>;\n\ntype ListenerT = () => void;\n\ntype GlobalStateRef = {\n emitter: Emitter<[]>;\n globalState: GlobalState<unknown>;\n path: null | string | undefined;\n setter: SetterT<unknown>;\n subscribe: (listener: ListenerT) => () => void;\n state: unknown;\n watcher: CallbackT;\n};\n\nexport type UseGlobalStateResT<T> = [T, SetterT<T>];\n\n/**\n * The primary hook for interacting with the global state, modeled after\n * the standard React's\n * [useState](https://reactjs.org/docs/hooks-reference.html#usestate).\n * It subscribes a component to a given `path` of global state, and provides\n * a function to update it. Each time the value at `path` changes, the hook\n * triggers re-render of its host component.\n *\n * **Note:**\n * - For performance, the library does not copy objects written to / read from\n * global state paths. You MUST NOT manually mutate returned state values,\n * or change objects already written into the global state, without explicitly\n * clonning them first yourself.\n * - State update notifications are asynchronous. When your code does multiple\n * global state updates in the same React rendering cycle, all state update\n * notifications are queued and dispatched together, after the current\n * rendering cycle. In other words, in any given rendering cycle the global\n * state values are \"fixed\", and all changes becomes visible at once in the\n * next triggered rendering pass.\n *\n * @param path Dot-delimitered state path. It can be undefined to\n * subscribe for entire state.\n *\n * Under-the-hood state values are read and written using `lodash`\n * [_.get()](https://lodash.com/docs/4.17.15#get) and\n * [_.set()](https://lodash.com/docs/4.17.15#set) methods, thus it is safe\n * to access state paths which have not been created before.\n * @param initialValue Initial value to set at the `path`, or its\n * factory:\n * - If a function is given, it will act similar to\n * [the lazy initial state of the standard React's useState()](https://reactjs.org/docs/hooks-reference.html#lazy-initial-state):\n * only if the value at `path` is `undefined`, the function will be executed,\n * and the value it returns will be written to the `path`.\n * - Otherwise, the given value itself will be written to the `path`,\n * if the current value at `path` is `undefined`.\n * @return It returs an array with two elements: `[value, setValue]`:\n *\n * - The `value` is the current value at given `path`.\n *\n * - The `setValue()` is setter function to write a new value to the `path`.\n *\n * Similar to the standard React's `useState()`, it supports\n * [functional value updates](https://reactjs.org/docs/hooks-reference.html#functional-updates):\n * if `setValue()` is called with a function as argument, that function will\n * be called and its return value will be written to `path`. Otherwise,\n * the argument of `setValue()` itself is written to `path`.\n *\n * Also, similar to the standard React's state setters, `setValue()` is\n * stable function: it does not change between component re-renders.\n */\n\n// \"Enforced type overload\"\nfunction useGlobalState<\n Forced extends ForceT | LockT = LockT,\n ValueT = void,\n>(\n path: null | string | undefined,\n initialValue?: ValueOrInitializerT<TypeLock<Forced, never, ValueT>>,\n): UseGlobalStateResT<TypeLock<Forced, void, ValueT>>;\n\n// \"Entire state overload\"\nfunction useGlobalState<StateT>(): UseGlobalStateResT<StateT>;\n\n// \"State evaluation overload\"\nfunction useGlobalState<\n StateT,\n PathT extends null | string | undefined,\n>(\n path: PathT,\n initialValue: ValueOrInitializerT<\n Exclude<ValueAtPathT<StateT, PathT, never>, undefined>>\n): UseGlobalStateResT<Exclude<ValueAtPathT<StateT, PathT, void>, undefined>>;\n\nfunction useGlobalState<\n StateT,\n PathT extends null | string | undefined,\n>(\n path: PathT,\n initialValue?: ValueOrInitializerT<ValueAtPathT<StateT, PathT, never>>\n): UseGlobalStateResT<ValueAtPathT<StateT, PathT, void>>;\n\nfunction useGlobalState(\n path?: null | string,\n // TODO: Revise it later!\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n initialValue?: ValueOrInitializerT<any>,\n\n // TODO: Revise it later!\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n): UseGlobalStateResT<any> {\n const globalState = getGlobalState();\n\n const ref = useRef<GlobalStateRef>(undefined);\n\n // TODO: Revise how this `rc` variable is used, perhaps we can simplify stuff\n // here.\n let rc: GlobalStateRef | undefined = ref.current;\n if (!ref.current) {\n const emitter = new Emitter();\n ref.current = {\n emitter,\n globalState,\n path,\n setter: (value) => {\n const newState = isFunction(value)\n ? value(rc!.globalState.get(rc!.path)) as unknown\n : value;\n\n if (process.env.NODE_ENV !== 'production' && isDebugMode()) {\n /* eslint-disable no-console */\n console.groupCollapsed(\n `ReactGlobalState - useGlobalState setter triggered for path ${\n rc!.path ?? ''\n }`,\n );\n console.log('New value:', cloneDeepForLog(newState, rc!.path ?? ''));\n console.groupEnd();\n /* eslint-enable no-console */\n }\n rc!.globalState.set<ForceT, unknown>(rc!.path, newState);\n\n // NOTE: The regular global state's update notifications, automatically\n // triggered by the rc.globalState.set() call above, are batched, and\n // scheduled to fire asynchronosuly at a later time, which is problematic\n // for managed text inputs - if they have their value update delayed to\n // future render cycles, it will result in reset of their cursor position\n // to the value end. Calling the rc.emitter.emit() below causes a sooner\n // state update for the current component, thus working around the issue.\n // For additional details see the original issue:\n // https://github.com/birdofpreyru/react-global-state/issues/22\n if (newState !== rc!.state) rc!.emitter.emit();\n },\n state: isFunction(initialValue) ? initialValue() : initialValue,\n subscribe: emitter.addListener.bind(emitter),\n watcher: () => {\n // TODO: Revise it later.\n // eslint-disable-next-line @typescript-eslint/no-confusing-void-expression\n const state = rc!.globalState.get(rc!.path) as unknown;\n if (state !== rc!.state) rc!.emitter.emit();\n },\n };\n }\n rc = ref.current!;\n\n rc.globalState = globalState;\n rc.path = path;\n\n rc.state = useSyncExternalStore(\n rc.subscribe,\n () => rc.globalState.get<ForceT, unknown>(rc.path, { initialValue }),\n\n () => rc.globalState.get<ForceT, unknown>(\n rc.path,\n { initialState: true, initialValue },\n ),\n );\n\n useEffect(() => {\n const { watcher } = ref.current!;\n globalState.watch(watcher);\n watcher();\n return () => {\n globalState.unWatch(watcher);\n };\n }, [globalState]);\n\n useEffect(() => {\n ref.current!.watcher();\n }, [path]);\n\n return [rc.state, rc.setter];\n}\n\nexport default useGlobalState;\n\n// TODO: Revise.\n// eslint-disable-next-line @typescript-eslint/consistent-type-definitions\nexport interface UseGlobalStateI<StateT> {\n (): UseGlobalStateResT<StateT>;\n\n <PathT extends null | string | undefined>(\n path: PathT,\n initialValue: ValueOrInitializerT<\n Exclude<ValueAtPathT<StateT, PathT, never>, undefined>>\n ): UseGlobalStateResT<Exclude<ValueAtPathT<StateT, PathT, void>, undefined>>;\n\n <PathT extends null | string | undefined>(\n path: PathT,\n initialValue?: ValueOrInitializerT<ValueAtPathT<StateT, PathT, never>>\n ): UseGlobalStateResT<ValueAtPathT<StateT, PathT, void>>;\n\n <Forced extends ForceT | LockT = LockT, ValueT = unknown>(\n path: null | string | undefined,\n initialValue?: ValueOrInitializerT<TypeLock<Forced, never, ValueT>>,\n ): UseGlobalStateResT<TypeLock<Forced, void, ValueT>>;\n}\n"],"mappings":"AAAA;;AAEA,SAASA,UAAU,QAAQ,QAAQ;AAEnC,SAGEC,SAAS,EACTC,MAAM,EACNC,oBAAoB,QACf,OAAO;AAEd,SAASC,OAAO,QAAQ,sBAAsB;AAG9C,SAASC,cAAc;AAEvB,SAOEC,eAAe,EACfC,WAAW;;AAmBb;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;AAEA;;AASA;;AAGA;;AAkBA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C,CAAC;EACxB,CAAC,EAAE,CAAC3B,IAAI,CAAC,CAAC;EAEV,OAAO,CAACK,EAAE,CAACiB,KAAK,EAAEjB,EAAE,CAACG,MAAM,CAAC;AAC9B;AAEA,eAAeT,cAAc;;AAE7B;AACA","ignoreList":[]}
1
+ {"version":3,"file":"useGlobalState.js","names":["isFunction","useEffect","useRef","useState","useSyncExternalStore","Emitter","getGlobalState","cloneDeepForLog","isDebugMode","useGlobalState","path","initialValue","_ref$current","globalState","ref","stable","emitter","setter","value","rc","current","Error","newState","get","process","env","NODE_ENV","_rc$path","_rc$path2","console","groupCollapsed","log","groupEnd","set","prevValue","emit","subscribe","addListener","bind","initialState","watcher","nextValue","watch","unWatch"],"sources":["../../src/useGlobalState.ts"],"sourcesContent":["// Hook for updates of global state.\n\nimport isFunction from 'lodash/isFunction.js';\n\nimport {\n type Dispatch,\n type SetStateAction,\n useEffect,\n useRef,\n useState,\n useSyncExternalStore,\n} from 'react';\n\nimport { Emitter } from '@dr.pogodin/js-utils';\n\nimport type GlobalState from './GlobalState';\nimport { getGlobalState } from './GlobalStateProvider';\n\nimport {\n type ForceT,\n type LockT,\n type TypeLock,\n type ValueAtPathT,\n type ValueOrInitializerT,\n cloneDeepForLog,\n isDebugMode,\n} from './utils';\n\nexport type SetterT<T> = Dispatch<SetStateAction<T>>;\n\ntype ListenerT = () => void;\n\ntype CurrentT = {\n globalState: GlobalState<unknown>;\n path: null | string | undefined;\n prevValue: unknown;\n};\n\ntype StableT = {\n emitter: Emitter<[]>;\n setter: SetterT<unknown>;\n subscribe: (listener: ListenerT) => () => void;\n};\n\nexport type UseGlobalStateResT<T> = [T, SetterT<T>];\n\n/**\n * The primary hook for interacting with the global state, modeled after\n * the standard React's\n * [useState](https://reactjs.org/docs/hooks-reference.html#usestate).\n * It subscribes a component to a given `path` of global state, and provides\n * a function to update it. Each time the value at `path` changes, the hook\n * triggers re-render of its host component.\n *\n * **Note:**\n * - For performance, the library does not copy objects written to / read from\n * global state paths. You MUST NOT manually mutate returned state values,\n * or change objects already written into the global state, without explicitly\n * clonning them first yourself.\n * - State update notifications are asynchronous. When your code does multiple\n * global state updates in the same React rendering cycle, all state update\n * notifications are queued and dispatched together, after the current\n * rendering cycle. In other words, in any given rendering cycle the global\n * state values are \"fixed\", and all changes becomes visible at once in the\n * next triggered rendering pass.\n *\n * @param path Dot-delimitered state path. It can be undefined to\n * subscribe for entire state.\n *\n * Under-the-hood state values are read and written using `lodash`\n * [_.get()](https://lodash.com/docs/4.17.15#get) and\n * [_.set()](https://lodash.com/docs/4.17.15#set) methods, thus it is safe\n * to access state paths which have not been created before.\n * @param initialValue Initial value to set at the `path`, or its\n * factory:\n * - If a function is given, it will act similar to\n * [the lazy initial state of the standard React's useState()](https://reactjs.org/docs/hooks-reference.html#lazy-initial-state):\n * only if the value at `path` is `undefined`, the function will be executed,\n * and the value it returns will be written to the `path`.\n * - Otherwise, the given value itself will be written to the `path`,\n * if the current value at `path` is `undefined`.\n * @return It returs an array with two elements: `[value, setValue]`:\n *\n * - The `value` is the current value at given `path`.\n *\n * - The `setValue()` is setter function to write a new value to the `path`.\n *\n * Similar to the standard React's `useState()`, it supports\n * [functional value updates](https://reactjs.org/docs/hooks-reference.html#functional-updates):\n * if `setValue()` is called with a function as argument, that function will\n * be called and its return value will be written to `path`. Otherwise,\n * the argument of `setValue()` itself is written to `path`.\n *\n * Also, similar to the standard React's state setters, `setValue()` is\n * stable function: it does not change between component re-renders.\n */\n\n// \"Enforced type overload\"\nfunction useGlobalState<\n Forced extends ForceT | LockT = LockT,\n ValueT = void,\n>(\n path: null | string | undefined,\n initialValue?: ValueOrInitializerT<TypeLock<Forced, never, ValueT>>,\n): UseGlobalStateResT<TypeLock<Forced, void, ValueT>>;\n\n// \"Entire state overload\"\nfunction useGlobalState<StateT>(): UseGlobalStateResT<StateT>;\n\n// \"State evaluation overload\"\nfunction useGlobalState<\n StateT,\n PathT extends null | string | undefined,\n>(\n path: PathT,\n initialValue: ValueOrInitializerT<\n Exclude<ValueAtPathT<StateT, PathT, never>, undefined>>,\n): UseGlobalStateResT<Exclude<ValueAtPathT<StateT, PathT, void>, undefined>>;\n\nfunction useGlobalState<\n StateT,\n PathT extends null | string | undefined,\n>(\n path: PathT,\n initialValue?: ValueOrInitializerT<ValueAtPathT<StateT, PathT, never>>,\n): UseGlobalStateResT<ValueAtPathT<StateT, PathT, void>>;\n\nfunction useGlobalState(\n path?: null | string,\n // TODO: Revise it later!\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n initialValue?: ValueOrInitializerT<any>,\n\n // TODO: Revise it later!\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n): UseGlobalStateResT<any> {\n const globalState = getGlobalState();\n\n const ref = useRef<CurrentT>(null);\n\n const [stable] = useState<StableT>(() => {\n const emitter = new Emitter();\n const setter = (value: unknown) => {\n const rc = ref.current;\n if (!rc) throw Error('Internal error');\n\n const newState = isFunction(value)\n ? value(rc.globalState.get(rc.path)) as unknown\n : value;\n\n if (process.env.NODE_ENV !== 'production' && isDebugMode()) {\n /* eslint-disable no-console */\n console.groupCollapsed(\n `ReactGlobalState - useGlobalState setter triggered for path ${\n rc.path ?? ''\n }`,\n );\n console.log('New value:', cloneDeepForLog(newState, rc.path ?? ''));\n console.groupEnd();\n /* eslint-enable no-console */\n }\n rc.globalState.set<ForceT, unknown>(rc.path, newState);\n\n // NOTE: The regular global state's update notifications, automatically\n // triggered by the rc.globalState.set() call above, are batched, and\n // scheduled to fire asynchronosuly at a later time, which is problematic\n // for managed text inputs - if they have their value update delayed to\n // future render cycles, it will result in reset of their cursor position\n // to the value end. Calling the rc.emitter.emit() below causes a sooner\n // state update for the current component, thus working around the issue.\n // For additional details see the original issue:\n // https://github.com/birdofpreyru/react-global-state/issues/22\n if (newState !== rc.prevValue) emitter.emit();\n };\n const subscribe = emitter.addListener.bind(emitter);\n return { emitter, setter, subscribe };\n });\n\n const value = useSyncExternalStore(\n stable.subscribe,\n () => globalState.get<ForceT, unknown>(path, { initialValue }),\n\n () => globalState.get<ForceT, unknown>(\n path,\n { initialState: true, initialValue },\n ),\n );\n\n ref.current ??= {\n globalState,\n path,\n prevValue: value,\n };\n\n useEffect(() => {\n ref.current = {\n globalState,\n path,\n prevValue: ref.current!.prevValue,\n };\n\n const watcher = () => {\n const nextValue = globalState.get<ForceT, unknown>(path);\n if (ref.current!.prevValue !== nextValue) {\n ref.current!.prevValue = nextValue;\n stable.emitter.emit();\n }\n };\n\n globalState.watch(watcher);\n watcher();\n\n return () => {\n globalState.unWatch(watcher);\n };\n }, [globalState, stable.emitter, path]);\n\n return [value, stable.setter];\n}\n\nexport default useGlobalState;\n\n// TODO: Revise.\n// eslint-disable-next-line @typescript-eslint/consistent-type-definitions\nexport interface UseGlobalStateI<StateT> {\n (): UseGlobalStateResT<StateT>;\n\n <PathT extends null | string | undefined>(\n path: PathT,\n initialValue: ValueOrInitializerT<\n Exclude<ValueAtPathT<StateT, PathT, never>, undefined>>\n ): UseGlobalStateResT<Exclude<ValueAtPathT<StateT, PathT, void>, undefined>>;\n\n <PathT extends null | string | undefined>(\n path: PathT,\n initialValue?: ValueOrInitializerT<ValueAtPathT<StateT, PathT, never>>\n ): UseGlobalStateResT<ValueAtPathT<StateT, PathT, void>>;\n\n <Forced extends ForceT | LockT = LockT, ValueT = unknown>(\n path: null | string | undefined,\n initialValue?: ValueOrInitializerT<TypeLock<Forced, never, ValueT>>,\n ): UseGlobalStateResT<TypeLock<Forced, void, ValueT>>;\n}\n"],"mappings":"AAAA;;AAEA,OAAOA,UAAU,MAAM,sBAAsB;AAE7C,SAGEC,SAAS,EACTC,MAAM,EACNC,QAAQ,EACRC,oBAAoB,QACf,OAAO;AAEd,SAASC,OAAO,QAAQ,sBAAsB;AAAC,SAGtCC,cAAc;AAAA,SAQrBC,eAAe,EACfC,WAAW;AAqBb;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;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CAAC;IAC1BA,OAAO,CAAC,CAAC;IAET,OAAO,MAAM;MACX3B,WAAW,CAAC8B,OAAO,CAACH,OAAO,CAAC;IAC9B,CAAC;EACH,CAAC,EAAE,CAAC3B,WAAW,EAAEE,MAAM,CAACC,OAAO,EAAEN,IAAI,CAAC,CAAC;EAEvC,OAAO,CAACQ,KAAK,EAAEH,MAAM,CAACE,MAAM,CAAC;AAC/B;AAEA,eAAeR,cAAc;;AAE7B;AACA","ignoreList":[]}
@@ -1,4 +1,4 @@
1
- import { cloneDeep } from 'lodash';
1
+ import cloneDeep from 'lodash/cloneDeep.js';
2
2
 
3
3
  // TODO: This (ForceT, LockT, and TypeLock) probably should be moved to JS Utils
4
4
  // lib.
@@ -1 +1 @@
1
- {"version":3,"file":"utils.js","names":["cloneDeep","isDebugMode","process","env","NODE_ENV","REACT_GLOBAL_STATE_DEBUG","cloneDeepBailKeys","Set","cloneDeepForLog","value","key","has","console","warn","start","Date","now","res","time","add","escape","x","replace","toString","hash","items","map","join"],"sources":["../../src/utils.ts"],"sourcesContent":["import { type GetFieldType, cloneDeep } from 'lodash';\n\nexport type CallbackT = () => void;\n\n// TODO: This (ForceT, LockT, and TypeLock) probably should be moved to JS Utils\n// lib.\n\nexport declare const force: unique symbol;\nexport declare const lock: unique symbol;\n\nexport type ForceT = typeof force;\nexport type LockT = typeof lock;\n\nexport type TypeLock<\n Unlocked extends ForceT | LockT,\n\n // TODO: Revise later.\n // eslint-disable-next-line @typescript-eslint/no-redundant-type-constituents\n LockedT extends never | void,\n UnlockedT,\n> = Unlocked extends ForceT ? UnlockedT : LockedT;\n\n/**\n * Given the type of state, `StateT`, and the type of state path, `PathT`,\n * it evaluates the type of value at that path of the state, if it can be\n * evaluated from the path type (it is possible when `PathT` is a string\n * literal type, and `StateT` elements along this path have appropriate\n * types); otherwise it falls back to the specified `UnknownT` type,\n * which should be set either `never` (for input arguments), or `void`\n * (for return types) - `never` and `void` in those places forbid assignments,\n * and are not auto-inferred to more permissible types.\n *\n * BEWARE: When StateT is any the construct resolves to any for any string\n * paths.\n */\nexport type ValueAtPathT<\n StateT,\n PathT extends null | string | undefined,\n\n // TODO: Revise later.\n // eslint-disable-next-line @typescript-eslint/no-redundant-type-constituents, @typescript-eslint/no-invalid-void-type\n UnknownT extends never | undefined | void,\n> = unknown extends StateT\n ? UnknownT\n : string extends PathT\n ? UnknownT\n : PathT extends null | undefined\n ? StateT\n : GetFieldType<StateT, PathT> extends undefined\n ? UnknownT : GetFieldType<StateT, PathT>;\n\nexport type ValueOrInitializerT<ValueT> = ValueT | (() => ValueT);\n\n/**\n * Returns 'true' if debug logging should be performed; 'false' otherwise.\n *\n * BEWARE: The actual safeguards for the debug logging still should read\n * if (process.env.NODE_ENV !== 'production' && isDebugMode()) {\n * // Some debug logging\n * }\n * to ensure that debug code is stripped out by Webpack in production mode.\n *\n * @returns\n * @ignore\n */\nexport function isDebugMode(): boolean {\n try {\n return process.env.NODE_ENV !== 'production'\n && !!process.env.REACT_GLOBAL_STATE_DEBUG;\n } catch {\n return false;\n }\n}\n\nconst cloneDeepBailKeys = new Set<string>();\n\n/**\n * Deep-clones given value for logging purposes, or returns the value itself\n * if the previous clone attempt, with the same key, took more than 300ms\n * (to avoid situations when large payload in the global state slows down\n * development versions of the app due to the logging overhead).\n */\nexport function cloneDeepForLog<T>(value: T, key: string = ''): T {\n if (cloneDeepBailKeys.has(key)) {\n // eslint-disable-next-line no-console\n console.warn(`The logged value won't be clonned (key \"${key}\").`);\n return value;\n }\n\n const start = Date.now();\n const res = cloneDeep(value);\n const time = Date.now() - start;\n if (time > 300) {\n // eslint-disable-next-line no-console\n console.warn(`${time}ms spent to clone the logged value (key \"${key}\").`);\n cloneDeepBailKeys.add(key);\n }\n\n return res;\n}\n\n/**\n * Converts given value to an escaped string. For now, we are good with the most\n * trivial escape logic:\n * - '%' characters are replaced by '%0' code pair;\n * - '/' characters are replaced by '%1' code pair.\n */\nexport function escape(x: number | string): string {\n return typeof x === 'string'\n ? x.replace(/%/g, '%0').replace(/\\//g, '%1')\n : x.toString();\n}\n\n/**\n * Hashes given string array. For our current needs we are fine to go with\n * the most trivial implementation, which probably should not be called \"hash\"\n * in the strict sense: we just escape each given string to not include '/'\n * characters, and then we join all those strings using '/' as the separator.\n */\nexport function hash(items: Array<number | string>): string {\n return items.map(escape).join('/');\n}\n"],"mappings":"AAAA,SAA4BA,SAAS,QAAQ,QAAQ;;AAIrD;AACA;;AAiBA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;AAmBA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,OAAO,SAASC,WAAWA,CAAA,EAAY;EACrC,IAAI;IACF,OAAOC,OAAO,CAACC,GAAG,CAACC,QAAQ,KAAK,YAAY,IACvC,CAAC,CAACF,OAAO,CAACC,GAAG,CAACE,wBAAwB;EAC7C,CAAC,CAAC,MAAM;IACN,OAAO,KAAK;EACd;AACF;AAEA,MAAMC,iBAAiB,GAAG,IAAIC,GAAG,CAAS,CAAC;;AAE3C;AACA;AACA;AACA;AACA;AACA;AACA,OAAO,SAASC,eAAeA,CAAIC,KAAQ,EAAEC,GAAW,GAAG,EAAE,EAAK;EAChE,IAAIJ,iBAAiB,CAACK,GAAG,CAACD,GAAG,CAAC,EAAE;IAC9B;IACAE,OAAO,CAACC,IAAI,CAAC,2CAA2CH,GAAG,KAAK,CAAC;IACjE,OAAOD,KAAK;EACd;EAEA,MAAMK,KAAK,GAAGC,IAAI,CAACC,GAAG,CAAC,CAAC;EACxB,MAAMC,GAAG,GAAGjB,SAAS,CAACS,KAAK,CAAC;EAC5B,MAAMS,IAAI,GAAGH,IAAI,CAACC,GAAG,CAAC,CAAC,GAAGF,KAAK;EAC/B,IAAII,IAAI,GAAG,GAAG,EAAE;IACd;IACAN,OAAO,CAACC,IAAI,CAAC,GAAGK,IAAI,4CAA4CR,GAAG,KAAK,CAAC;IACzEJ,iBAAiB,CAACa,GAAG,CAACT,GAAG,CAAC;EAC5B;EAEA,OAAOO,GAAG;AACZ;;AAEA;AACA;AACA;AACA;AACA;AACA;AACA,OAAO,SAASG,MAAMA,CAACC,CAAkB,EAAU;EACjD,OAAO,OAAOA,CAAC,KAAK,QAAQ,GACxBA,CAAC,CAACC,OAAO,CAAC,IAAI,EAAE,IAAI,CAAC,CAACA,OAAO,CAAC,KAAK,EAAE,IAAI,CAAC,GAC1CD,CAAC,CAACE,QAAQ,CAAC,CAAC;AAClB;;AAEA;AACA;AACA;AACA;AACA;AACA;AACA,OAAO,SAASC,IAAIA,CAACC,KAA6B,EAAU;EAC1D,OAAOA,KAAK,CAACC,GAAG,CAACN,MAAM,CAAC,CAACO,IAAI,CAAC,GAAG,CAAC;AACpC","ignoreList":[]}
1
+ {"version":3,"file":"utils.js","names":["cloneDeep","isDebugMode","process","env","NODE_ENV","REACT_GLOBAL_STATE_DEBUG","cloneDeepBailKeys","Set","cloneDeepForLog","value","key","has","console","warn","start","Date","now","res","time","add","escape","x","replace","toString","hash","items","map","join"],"sources":["../../src/utils.ts"],"sourcesContent":["import type { GetFieldType } from 'lodash';\nimport cloneDeep from 'lodash/cloneDeep.js';\n\nexport type CallbackT = () => void;\n\n// TODO: This (ForceT, LockT, and TypeLock) probably should be moved to JS Utils\n// lib.\n\nexport declare const force: unique symbol;\nexport declare const lock: unique symbol;\n\nexport type ForceT = typeof force;\nexport type LockT = typeof lock;\n\nexport type TypeLock<\n Unlocked extends ForceT | LockT,\n\n // TODO: Revise later.\n // eslint-disable-next-line @typescript-eslint/no-redundant-type-constituents\n LockedT extends never | void,\n UnlockedT,\n> = Unlocked extends ForceT ? UnlockedT : LockedT;\n\n/**\n * Given the type of state, `StateT`, and the type of state path, `PathT`,\n * it evaluates the type of value at that path of the state, if it can be\n * evaluated from the path type (it is possible when `PathT` is a string\n * literal type, and `StateT` elements along this path have appropriate\n * types); otherwise it falls back to the specified `UnknownT` type,\n * which should be set either `never` (for input arguments), or `void`\n * (for return types) - `never` and `void` in those places forbid assignments,\n * and are not auto-inferred to more permissible types.\n *\n * BEWARE: When StateT is any the construct resolves to any for any string\n * paths.\n */\nexport type ValueAtPathT<\n StateT,\n PathT extends null | string | undefined,\n\n // TODO: Revise later.\n // eslint-disable-next-line @typescript-eslint/no-redundant-type-constituents, @typescript-eslint/no-invalid-void-type\n UnknownT extends never | undefined | void,\n> = unknown extends StateT\n ? UnknownT\n : string extends PathT\n ? UnknownT\n : PathT extends null | undefined\n ? StateT\n : GetFieldType<StateT, PathT> extends undefined\n ? UnknownT : GetFieldType<StateT, PathT>;\n\nexport type ValueOrInitializerT<ValueT> = ValueT | (() => ValueT);\n\n/**\n * Returns 'true' if debug logging should be performed; 'false' otherwise.\n *\n * BEWARE: The actual safeguards for the debug logging still should read\n * if (process.env.NODE_ENV !== 'production' && isDebugMode()) {\n * // Some debug logging\n * }\n * to ensure that debug code is stripped out by Webpack in production mode.\n *\n * @returns\n * @ignore\n */\nexport function isDebugMode(): boolean {\n try {\n return process.env.NODE_ENV !== 'production'\n && !!process.env.REACT_GLOBAL_STATE_DEBUG;\n } catch {\n return false;\n }\n}\n\nconst cloneDeepBailKeys = new Set<string>();\n\n/**\n * Deep-clones given value for logging purposes, or returns the value itself\n * if the previous clone attempt, with the same key, took more than 300ms\n * (to avoid situations when large payload in the global state slows down\n * development versions of the app due to the logging overhead).\n */\nexport function cloneDeepForLog<T>(value: T, key: string = ''): T {\n if (cloneDeepBailKeys.has(key)) {\n // eslint-disable-next-line no-console\n console.warn(`The logged value won't be clonned (key \"${key}\").`);\n return value;\n }\n\n const start = Date.now();\n const res = cloneDeep(value);\n const time = Date.now() - start;\n if (time > 300) {\n // eslint-disable-next-line no-console\n console.warn(`${time}ms spent to clone the logged value (key \"${key}\").`);\n cloneDeepBailKeys.add(key);\n }\n\n return res;\n}\n\n/**\n * Converts given value to an escaped string. For now, we are good with the most\n * trivial escape logic:\n * - '%' characters are replaced by '%0' code pair;\n * - '/' characters are replaced by '%1' code pair.\n */\nexport function escape(x: number | string): string {\n return typeof x === 'string'\n ? x.replace(/%/g, '%0').replace(/\\//g, '%1')\n : x.toString();\n}\n\n/**\n * Hashes given string array. For our current needs we are fine to go with\n * the most trivial implementation, which probably should not be called \"hash\"\n * in the strict sense: we just escape each given string to not include '/'\n * characters, and then we join all those strings using '/' as the separator.\n */\nexport function hash(items: Array<number | string>): string {\n return items.map(escape).join('/');\n}\n"],"mappings":"AACA,OAAOA,SAAS,MAAM,qBAAqB;;AAI3C;AACA;;AAiBA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;AAmBA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,OAAO,SAASC,WAAWA,CAAA,EAAY;EACrC,IAAI;IACF,OAAOC,OAAO,CAACC,GAAG,CAACC,QAAQ,KAAK,YAAY,IACvC,CAAC,CAACF,OAAO,CAACC,GAAG,CAACE,wBAAwB;EAC7C,CAAC,CAAC,MAAM;IACN,OAAO,KAAK;EACd;AACF;AAEA,MAAMC,iBAAiB,GAAG,IAAIC,GAAG,CAAS,CAAC;;AAE3C;AACA;AACA;AACA;AACA;AACA;AACA,OAAO,SAASC,eAAeA,CAAIC,KAAQ,EAAEC,GAAW,GAAG,EAAE,EAAK;EAChE,IAAIJ,iBAAiB,CAACK,GAAG,CAACD,GAAG,CAAC,EAAE;IAC9B;IACAE,OAAO,CAACC,IAAI,CAAC,2CAA2CH,GAAG,KAAK,CAAC;IACjE,OAAOD,KAAK;EACd;EAEA,MAAMK,KAAK,GAAGC,IAAI,CAACC,GAAG,CAAC,CAAC;EACxB,MAAMC,GAAG,GAAGjB,SAAS,CAACS,KAAK,CAAC;EAC5B,MAAMS,IAAI,GAAGH,IAAI,CAACC,GAAG,CAAC,CAAC,GAAGF,KAAK;EAC/B,IAAII,IAAI,GAAG,GAAG,EAAE;IACd;IACAN,OAAO,CAACC,IAAI,CAAC,GAAGK,IAAI,4CAA4CR,GAAG,KAAK,CAAC;IACzEJ,iBAAiB,CAACa,GAAG,CAACT,GAAG,CAAC;EAC5B;EAEA,OAAOO,GAAG;AACZ;;AAEA;AACA;AACA;AACA;AACA;AACA;AACA,OAAO,SAASG,MAAMA,CAACC,CAAkB,EAAU;EACjD,OAAO,OAAOA,CAAC,KAAK,QAAQ,GACxBA,CAAC,CAACC,OAAO,CAAC,IAAI,EAAE,IAAI,CAAC,CAACA,OAAO,CAAC,KAAK,EAAE,IAAI,CAAC,GAC1CD,CAAC,CAACE,QAAQ,CAAC,CAAC;AAClB;;AAEA;AACA;AACA;AACA;AACA;AACA;AACA,OAAO,SAASC,IAAIA,CAACC,KAA6B,EAAU;EAC1D,OAAOA,KAAK,CAACC,GAAG,CAACN,MAAM,CAAC,CAACO,IAAI,CAAC,GAAG,CAAC;AACpC","ignoreList":[]}
@@ -1,4 +1,4 @@
1
- import { type GetFieldType } from 'lodash';
1
+ import type { GetFieldType } from 'lodash';
2
2
  export type CallbackT = () => void;
3
3
  export declare const force: unique symbol;
4
4
  export declare const lock: unique symbol;
package/package.json CHANGED
@@ -1,11 +1,12 @@
1
1
  {
2
2
  "name": "@dr.pogodin/react-global-state",
3
- "version": "0.19.4",
3
+ "version": "0.20.1",
4
4
  "description": "Hook-based global state for React",
5
5
  "main": "./build/code/index.js",
6
6
  "types": "./build/types/index.d.ts",
7
7
  "exports": {
8
8
  "require": "./build/cjs/index.js",
9
+ "types": "./build/types/index.d.ts",
9
10
  "default": "./build/code/index.js"
10
11
  },
11
12
  "scripts": {
@@ -14,7 +15,7 @@
14
15
  "build:code": "rimraf build/code && babel src -x .ts,.tsx --out-dir build/code --source-maps --config-file ./babel.module.config.js",
15
16
  "build:types": "rimraf build/types && tsc --project tsconfig.types.json",
16
17
  "jest": "npm run jest:types && npm run jest:logic",
17
- "jest:logic": "jest --config jest/config.json -w 1",
18
+ "jest:logic": "jest --config jest/config.json -w 1 --no-cache",
18
19
  "jest:types": "tstyche",
19
20
  "lint": "eslint",
20
21
  "test": "npm run lint && npm run typecheck && npm run jest",
@@ -37,42 +38,43 @@
37
38
  },
38
39
  "homepage": "https://dr.pogodin.studio/docs/react-global-state/index.html",
39
40
  "engines": {
40
- "node": ">=18"
41
+ "node": ">=20"
41
42
  },
42
43
  "dependencies": {
43
- "@babel/runtime": "^7.28.2",
44
- "@dr.pogodin/js-utils": "^0.1.3",
44
+ "@babel/runtime": "^7.28.4",
45
+ "@dr.pogodin/js-utils": "^0.1.4",
45
46
  "@types/lodash": "^4.17.20",
46
47
  "lodash": "^4.17.21",
47
- "uuid": "^11.1.0"
48
+ "uuid": "^13.0.0"
48
49
  },
49
50
  "devDependencies": {
50
- "@babel/cli": "^7.28.0",
51
- "@babel/core": "^7.28.0",
51
+ "@babel/cli": "^7.28.3",
52
+ "@babel/core": "^7.28.5",
52
53
  "@babel/node": "^7.28.0",
53
- "@babel/plugin-transform-runtime": "^7.28.0",
54
- "@babel/preset-env": "^7.28.0",
55
- "@babel/preset-react": "^7.27.1",
56
- "@babel/preset-typescript": "^7.27.1",
57
- "@dr.pogodin/eslint-configs": "^0.0.11",
54
+ "@babel/plugin-transform-runtime": "^7.28.5",
55
+ "@babel/preset-env": "^7.28.5",
56
+ "@babel/preset-react": "^7.28.5",
57
+ "@babel/preset-typescript": "^7.28.5",
58
+ "@dr.pogodin/eslint-configs": "^0.1.1",
58
59
  "@testing-library/dom": "^10.4.1",
59
60
  "@testing-library/react": "^16.3.0",
60
61
  "@tsconfig/recommended": "^1.0.10",
61
62
  "@types/jest": "^30.0.0",
62
63
  "@types/pretty": "^2.0.3",
63
- "@types/react": "^19.1.9",
64
- "@types/react-dom": "^19.1.7",
65
- "babel-jest": "^30.0.5",
64
+ "@types/react": "^19.2.2",
65
+ "@types/react-dom": "^19.2.2",
66
+ "babel-jest": "^30.2.0",
67
+ "babel-plugin-add-import-extension": "^1.6.0",
66
68
  "babel-plugin-module-resolver": "^5.0.2",
67
- "jest": "^30.0.5",
68
- "jest-environment-jsdom": "^30.0.5",
69
+ "jest": "^30.2.0",
70
+ "jest-environment-jsdom": "^30.2.0",
69
71
  "mockdate": "^3.0.5",
70
72
  "rimraf": "^6.0.1",
71
- "tstyche": "^4.3.0",
72
- "typescript": "^5.9.2"
73
+ "tstyche": "^5.0.0",
74
+ "typescript": "^5.9.3"
73
75
  },
74
76
  "peerDependencies": {
75
- "react": "^19.1.1",
76
- "react-dom": "^19.1.1"
77
+ "react": "^19.2.0",
78
+ "react-dom": "^19.2.0"
77
79
  }
78
80
  }
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "$schema": "https://tstyche.org/schemas/config.json",
3
+ "checkDeclarationFiles": true,
3
4
  "checkSuppressedErrors": true,
4
- "checkSourceFiles": false,
5
5
  "testFileMatch": ["__tests__/**/*.{ts,tsx}"]
6
6
  }