@dr.pogodin/react-global-state 0.13.0 → 0.14.0

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.
@@ -66,7 +66,7 @@ var _useAsyncCollection = _interopRequireDefault(require("./useAsyncCollection")
66
66
  var _useAsyncData = _interopRequireWildcard(require("./useAsyncData"));
67
67
  var _useGlobalState = _interopRequireDefault(require("./useGlobalState"));
68
68
  function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function (e) { return e ? t : r; })(e); }
69
- function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != typeof e && "function" != typeof e) return { default: e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && Object.prototype.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n.default = e, t && t.set(e, n), n; }
69
+ function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != typeof e && "function" != typeof e) return { default: e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && {}.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n.default = e, t && t.set(e, n), n; }
70
70
  const api = {
71
71
  getGlobalState: _GlobalStateProvider.getGlobalState,
72
72
  getSsrContext: _GlobalStateProvider.getSsrContext,
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","names":["_GlobalState","_interopRequireDefault","require","_GlobalStateProvider","_interopRequireWildcard","_SsrContext","_useAsyncCollection","_useAsyncData","_useGlobalState","_getRequireWildcardCache","e","WeakMap","r","t","__esModule","default","has","get","n","__proto__","a","Object","defineProperty","getOwnPropertyDescriptor","u","prototype","hasOwnProperty","call","i","set","api","getGlobalState","getSsrContext","GlobalState","GlobalStateProvider","newAsyncDataEnvelope","SsrContext","useAsyncCollection","useAsyncData","useGlobalState","withGlobalStateType"],"sources":["../../src/index.ts"],"sourcesContent":["import GlobalState from './GlobalState';\n\nimport GlobalStateProvider, {\n getGlobalState,\n getSsrContext,\n} from './GlobalStateProvider';\n\nimport SsrContext from './SsrContext';\n\nimport useAsyncCollection, {\n type UseAsyncCollectionI,\n} from './useAsyncCollection';\n\nimport useAsyncData, {\n type UseAsyncDataI,\n newAsyncDataEnvelope,\n} from './useAsyncData';\n\nimport useGlobalState, { type UseGlobalStateI } from './useGlobalState';\n\nexport type { AsyncCollectionLoaderT } from './useAsyncCollection';\n\nexport type {\n AsyncDataEnvelopeT,\n AsyncDataLoaderT,\n UseAsyncDataOptionsT,\n UseAsyncDataResT,\n} from './useAsyncData';\n\nexport type { SetterT, UseGlobalStateResT } from './useGlobalState';\n\nexport type { ForceT, ValueOrInitializerT } from './utils';\n\nexport {\n getGlobalState,\n getSsrContext,\n GlobalState,\n GlobalStateProvider,\n newAsyncDataEnvelope,\n SsrContext,\n useAsyncCollection,\n useAsyncData,\n useGlobalState,\n};\n\ninterface API<\n StateT,\n SsrContextT extends SsrContext<StateT> = SsrContext<StateT>,\n> {\n getGlobalState: typeof getGlobalState<StateT, SsrContextT>;\n getSsrContext: typeof getSsrContext<SsrContextT>,\n GlobalState: typeof GlobalState<StateT, SsrContextT>,\n GlobalStateProvider: typeof GlobalStateProvider<StateT, SsrContextT>,\n newAsyncDataEnvelope: typeof newAsyncDataEnvelope,\n SsrContext: typeof SsrContext<StateT>,\n useAsyncCollection: UseAsyncCollectionI<StateT>,\n useAsyncData: UseAsyncDataI<StateT>,\n useGlobalState: UseGlobalStateI<StateT>,\n}\n\nconst api = {\n getGlobalState,\n getSsrContext,\n GlobalState,\n GlobalStateProvider,\n newAsyncDataEnvelope,\n SsrContext,\n useAsyncCollection,\n useAsyncData,\n useGlobalState,\n};\n\nexport function withGlobalStateType<\n StateT,\n SsrContextT extends SsrContext<StateT> = SsrContext<StateT>,\n>() {\n return api as API<StateT, SsrContextT>;\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,IAAAA,YAAA,GAAAC,sBAAA,CAAAC,OAAA;AAEA,IAAAC,oBAAA,GAAAC,uBAAA,CAAAF,OAAA;AAKA,IAAAG,WAAA,GAAAJ,sBAAA,CAAAC,OAAA;AAEA,IAAAI,mBAAA,GAAAL,sBAAA,CAAAC,OAAA;AAIA,IAAAK,aAAA,GAAAH,uBAAA,CAAAF,OAAA;AAKA,IAAAM,eAAA,GAAAP,sBAAA,CAAAC,OAAA;AAAwE,SAAAO,yBAAAC,CAAA,6BAAAC,OAAA,mBAAAC,CAAA,OAAAD,OAAA,IAAAE,CAAA,OAAAF,OAAA,YAAAF,wBAAA,YAAAA,CAAAC,CAAA,WAAAA,CAAA,GAAAG,CAAA,GAAAD,CAAA,KAAAF,CAAA;AAAA,SAAAN,wBAAAM,CAAA,EAAAE,CAAA,SAAAA,CAAA,IAAAF,CAAA,IAAAA,CAAA,CAAAI,UAAA,SAAAJ,CAAA,eAAAA,CAAA,uBAAAA,CAAA,yBAAAA,CAAA,WAAAK,OAAA,EAAAL,CAAA,QAAAG,CAAA,GAAAJ,wBAAA,CAAAG,CAAA,OAAAC,CAAA,IAAAA,CAAA,CAAAG,GAAA,CAAAN,CAAA,UAAAG,CAAA,CAAAI,GAAA,CAAAP,CAAA,OAAAQ,CAAA,KAAAC,SAAA,UAAAC,CAAA,GAAAC,MAAA,CAAAC,cAAA,IAAAD,MAAA,CAAAE,wBAAA,WAAAC,CAAA,IAAAd,CAAA,oBAAAc,CAAA,IAAAH,MAAA,CAAAI,SAAA,CAAAC,cAAA,CAAAC,IAAA,CAAAjB,CAAA,EAAAc,CAAA,SAAAI,CAAA,GAAAR,CAAA,GAAAC,MAAA,CAAAE,wBAAA,CAAAb,CAAA,EAAAc,CAAA,UAAAI,CAAA,KAAAA,CAAA,CAAAX,GAAA,IAAAW,CAAA,CAAAC,GAAA,IAAAR,MAAA,CAAAC,cAAA,CAAAJ,CAAA,EAAAM,CAAA,EAAAI,CAAA,IAAAV,CAAA,CAAAM,CAAA,IAAAd,CAAA,CAAAc,CAAA,YAAAN,CAAA,CAAAH,OAAA,GAAAL,CAAA,EAAAG,CAAA,IAAAA,CAAA,CAAAgB,GAAA,CAAAnB,CAAA,EAAAQ,CAAA,GAAAA,CAAA;AA0CxE,MAAMY,GAAG,GAAG;EACVC,cAAc,EAAdA,mCAAc;EACdC,aAAa,EAAbA,kCAAa;EACbC,WAAW,EAAXA,oBAAW;EACXC,mBAAmB,EAAnBA,4BAAmB;EACnBC,oBAAoB,EAApBA,kCAAoB;EACpBC,UAAU,EAAVA,mBAAU;EACVC,kBAAkB,EAAlBA,2BAAkB;EAClBC,YAAY,EAAZA,qBAAY;EACZC,cAAc,EAAdA;AACF,CAAC;AAEM,SAASC,mBAAmBA,CAAA,EAG/B;EACF,OAAOV,GAAG;AACZ","ignoreList":[]}
1
+ {"version":3,"file":"index.js","names":["_GlobalState","_interopRequireDefault","require","_GlobalStateProvider","_interopRequireWildcard","_SsrContext","_useAsyncCollection","_useAsyncData","_useGlobalState","_getRequireWildcardCache","e","WeakMap","r","t","__esModule","default","has","get","n","__proto__","a","Object","defineProperty","getOwnPropertyDescriptor","u","hasOwnProperty","call","i","set","api","getGlobalState","getSsrContext","GlobalState","GlobalStateProvider","newAsyncDataEnvelope","SsrContext","useAsyncCollection","useAsyncData","useGlobalState","withGlobalStateType"],"sources":["../../src/index.ts"],"sourcesContent":["import GlobalState from './GlobalState';\n\nimport GlobalStateProvider, {\n getGlobalState,\n getSsrContext,\n} from './GlobalStateProvider';\n\nimport SsrContext from './SsrContext';\n\nimport useAsyncCollection, {\n type UseAsyncCollectionI,\n} from './useAsyncCollection';\n\nimport useAsyncData, {\n type UseAsyncDataI,\n newAsyncDataEnvelope,\n} from './useAsyncData';\n\nimport useGlobalState, { type UseGlobalStateI } from './useGlobalState';\n\nexport type { AsyncCollectionLoaderT } from './useAsyncCollection';\n\nexport type {\n AsyncDataEnvelopeT,\n AsyncDataLoaderT,\n UseAsyncDataOptionsT,\n UseAsyncDataResT,\n} from './useAsyncData';\n\nexport type { SetterT, UseGlobalStateResT } from './useGlobalState';\n\nexport type { ForceT, ValueOrInitializerT } from './utils';\n\nexport {\n getGlobalState,\n getSsrContext,\n GlobalState,\n GlobalStateProvider,\n newAsyncDataEnvelope,\n SsrContext,\n useAsyncCollection,\n useAsyncData,\n useGlobalState,\n};\n\ninterface API<\n StateT,\n SsrContextT extends SsrContext<StateT> = SsrContext<StateT>,\n> {\n getGlobalState: typeof getGlobalState<StateT, SsrContextT>;\n getSsrContext: typeof getSsrContext<SsrContextT>,\n GlobalState: typeof GlobalState<StateT, SsrContextT>,\n GlobalStateProvider: typeof GlobalStateProvider<StateT, SsrContextT>,\n newAsyncDataEnvelope: typeof newAsyncDataEnvelope,\n SsrContext: typeof SsrContext<StateT>,\n useAsyncCollection: UseAsyncCollectionI<StateT>,\n useAsyncData: UseAsyncDataI<StateT>,\n useGlobalState: UseGlobalStateI<StateT>,\n}\n\nconst api = {\n getGlobalState,\n getSsrContext,\n GlobalState,\n GlobalStateProvider,\n newAsyncDataEnvelope,\n SsrContext,\n useAsyncCollection,\n useAsyncData,\n useGlobalState,\n};\n\nexport function withGlobalStateType<\n StateT,\n SsrContextT extends SsrContext<StateT> = SsrContext<StateT>,\n>() {\n return api as API<StateT, SsrContextT>;\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,IAAAA,YAAA,GAAAC,sBAAA,CAAAC,OAAA;AAEA,IAAAC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,IAAAA,CAAA,CAAAe,GAAA,CAAAlB,CAAA,EAAAQ,CAAA,GAAAA,CAAA;AA0CxE,MAAMW,GAAG,GAAG;EACVC,cAAc,EAAdA,mCAAc;EACdC,aAAa,EAAbA,kCAAa;EACbC,WAAW,EAAXA,oBAAW;EACXC,mBAAmB,EAAnBA,4BAAmB;EACnBC,oBAAoB,EAApBA,kCAAoB;EACpBC,UAAU,EAAVA,mBAAU;EACVC,kBAAkB,EAAlBA,2BAAkB;EAClBC,YAAY,EAAZA,qBAAY;EACZC,cAAc,EAAdA;AACF,CAAC;AAEM,SAASC,mBAAmBA,CAAA,EAG/B;EACF,OAAOV,GAAG;AACZ","ignoreList":[]}
@@ -71,42 +71,47 @@ var _utils = require("./utils");
71
71
  function useGlobalState(path, initialValue) {
72
72
  const globalState = (0, _GlobalStateProvider.getGlobalState)();
73
73
  const ref = (0, _react.useRef)();
74
- const rc = ref.current || {
75
- emitter: new _jsUtils.Emitter(),
76
- globalState,
77
- path,
78
- setter: value => {
79
- const newState = (0, _lodash.isFunction)(value) ? value(rc.state) : value;
80
- if (process.env.NODE_ENV !== 'production' && (0, _utils.isDebugMode)()) {
81
- /* eslint-disable no-console */
82
- console.groupCollapsed(`ReactGlobalState - useGlobalState setter triggered for path ${rc.path || ''}`);
83
- console.log('New value:', (0, _lodash.cloneDeep)(newState));
84
- console.groupEnd();
85
- /* eslint-enable no-console */
86
- }
87
- rc.globalState.set(rc.path, newState);
74
+ let rc;
75
+ if (!ref.current) {
76
+ const emitter = new _jsUtils.Emitter();
77
+ ref.current = {
78
+ emitter,
79
+ globalState,
80
+ path,
81
+ setter: value => {
82
+ const newState = (0, _lodash.isFunction)(value) ? value(rc.globalState.get(rc.path)) : value;
83
+ if (process.env.NODE_ENV !== 'production' && (0, _utils.isDebugMode)()) {
84
+ /* eslint-disable no-console */
85
+ console.groupCollapsed(`ReactGlobalState - useGlobalState setter triggered for path ${rc.path || ''}`);
86
+ console.log('New value:', (0, _lodash.cloneDeep)(newState));
87
+ console.groupEnd();
88
+ /* eslint-enable no-console */
89
+ }
90
+ rc.globalState.set(rc.path, newState);
88
91
 
89
- // NOTE: The regular global state's update notifications, automatically
90
- // triggered by the rc.globalState.set() call above, are batched, and
91
- // scheduled to fire asynchronosuly at a later time, which is problematic
92
- // for managed text inputs - if they have their value update delayed to
93
- // future render cycles, it will result in reset of their cursor position
94
- // to the value end. Calling the rc.emitter.emit() below causes a sooner
95
- // state update for the current component, thus working around the issue.
96
- // For additional details see the original issue:
97
- // https://github.com/birdofpreyru/react-global-state/issues/22
98
- if (newState !== rc.state) rc.emitter.emit();
99
- },
100
- state: (0, _lodash.isFunction)(initialValue) ? initialValue() : initialValue,
101
- watcher: () => {
102
- const state = rc.globalState.get(rc.path);
103
- if (state !== rc.state) rc.emitter.emit();
104
- }
105
- };
106
- ref.current = rc;
92
+ // NOTE: The regular global state's update notifications, automatically
93
+ // triggered by the rc.globalState.set() call above, are batched, and
94
+ // scheduled to fire asynchronosuly at a later time, which is problematic
95
+ // for managed text inputs - if they have their value update delayed to
96
+ // future render cycles, it will result in reset of their cursor position
97
+ // to the value end. Calling the rc.emitter.emit() below causes a sooner
98
+ // state update for the current component, thus working around the issue.
99
+ // For additional details see the original issue:
100
+ // https://github.com/birdofpreyru/react-global-state/issues/22
101
+ if (newState !== rc.state) rc.emitter.emit();
102
+ },
103
+ state: (0, _lodash.isFunction)(initialValue) ? initialValue() : initialValue,
104
+ subscribe: emitter.addListener.bind(emitter),
105
+ watcher: () => {
106
+ const state = rc.globalState.get(rc.path);
107
+ if (state !== rc.state) rc.emitter.emit();
108
+ }
109
+ };
110
+ }
111
+ rc = ref.current;
107
112
  rc.globalState = globalState;
108
113
  rc.path = path;
109
- rc.state = (0, _react.useSyncExternalStore)(cb => rc.emitter.addListener(cb), () => rc.globalState.get(rc.path, {
114
+ rc.state = (0, _react.useSyncExternalStore)(rc.subscribe, () => rc.globalState.get(rc.path, {
110
115
  initialValue
111
116
  }), () => rc.globalState.get(rc.path, {
112
117
  initialValue,
@@ -1 +1 @@
1
- {"version":3,"file":"useGlobalState.js","names":["_lodash","require","_react","_jsUtils","_GlobalStateProvider","_utils","useGlobalState","path","initialValue","globalState","getGlobalState","ref","useRef","rc","current","emitter","Emitter","setter","value","newState","isFunction","state","process","env","NODE_ENV","isDebugMode","console","groupCollapsed","log","cloneDeep","groupEnd","set","emit","watcher","get","useSyncExternalStore","cb","addListener","initialState","useEffect","watch","unWatch","_default","exports","default"],"sources":["../../src/useGlobalState.ts"],"sourcesContent":["// Hook for updates of global state.\n\nimport { cloneDeep, isFunction } from 'lodash';\nimport { useEffect, useRef, useSyncExternalStore } from 'react';\n\nimport { Emitter } from '@dr.pogodin/js-utils';\n\nimport 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 isDebugMode,\n} from './utils';\n\nexport type SetterT<T> = React.Dispatch<React.SetStateAction<T>>;\n\ntype GlobalStateRef = {\n emitter: Emitter<[]>;\n globalState: GlobalState<unknown>;\n path: null | string | undefined;\n setter: SetterT<unknown>;\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<ValueAtPathT<StateT, PathT, never>>\n): UseGlobalStateResT<ValueAtPathT<StateT, PathT, void>>;\n\nfunction useGlobalState(\n path?: null | string,\n initialValue?: ValueOrInitializerT<unknown>,\n): UseGlobalStateResT<any> {\n const globalState = getGlobalState();\n\n const ref = useRef<GlobalStateRef>();\n const rc: GlobalStateRef = ref.current || {\n emitter: new Emitter(),\n globalState,\n path,\n setter: (value) => {\n const newState = isFunction(value) ? value(rc.state) : value;\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:', cloneDeep(newState));\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 watcher: () => {\n const state = rc.globalState.get(rc.path);\n if (state !== rc.state) rc.emitter.emit();\n },\n };\n ref.current = rc;\n\n rc.globalState = globalState;\n rc.path = path;\n\n rc.state = useSyncExternalStore(\n (cb) => rc.emitter.addListener(cb),\n () => rc.globalState.get<ForceT, unknown>(rc.path, { initialValue }),\n () => rc.globalState.get<ForceT, unknown>(rc.path, { initialValue, initialState: true }),\n );\n\n useEffect(() => {\n const { watcher } = ref.current!;\n globalState.watch(watcher);\n watcher();\n return () => globalState.unWatch(watcher);\n }, [globalState]);\n\n useEffect(() => {\n ref.current!.watcher();\n }, [path]);\n\n return [rc.state, rc.setter];\n}\n\nexport default useGlobalState;\n\nexport interface UseGlobalStateI<StateT> {\n (): UseGlobalStateResT<StateT>;\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":";;;;;;AAEA,IAAAA,OAAA,GAAAC,OAAA;AACA,IAAAC,MAAA,GAAAD,OAAA;AAEA,IAAAE,QAAA,GAAAF,OAAA;AAGA,IAAAG,oBAAA,GAAAH,OAAA;AAEA,IAAAI,MAAA,GAAAJ,OAAA;AAVA;;AAiCA;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;;AASA,SAASK,cAAcA,CACrBC,IAAoB,EACpBC,YAA2C,EAClB;EACzB,MAAMC,WAAW,GAAG,IAAAC,mCAAc,EAAC,CAAC;EAEpC,MAAMC,GAAG,GAAG,IAAAC,aAAM,EAAiB,CAAC;EACpC,MAAMC,EAAkB,GAAGF,GAAG,CAACG,OAAO,IAAI;IACxCC,OAAO,EAAE,IAAIC,gBAAO,CAAC,CAAC;IACtBP,WAAW;IACXF,IAAI;IACJU,MAAM,EAAGC,KAAK,IAAK;MACjB,MAAMC,QAAQ,GAAG,IAAAC,kBAAU,EAACF,KAAK,CAAC,GAAGA,KAAK,CAACL,EAAE,CAACQ,KAAK,CAAC,GAAGH,KAAK;MAC5D,IAAII,OAAO,CAACC,GAAG,CAACC,QAAQ,KAAK,YAAY,IAAI,IAAAC,kBAAW,EAAC,CAAC,EAAE;QAC1D;QACAC,OAAO,CAACC,cAAc,CACnB,+DACCd,EAAE,CAACN,IAAI,IAAI,EACZ,EACH,CAAC;QACDmB,OAAO,CAACE,GAAG,CAAC,YAAY,EAAE,IAAAC,iBAAS,EAACV,QAAQ,CAAC,CAAC;QAC9CO,OAA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1BA,OAAO,CAAC,CAAC;IACT,OAAO,MAAMxB,WAAW,CAACgC,OAAO,CAACR,OAAO,CAAC;EAC3C,CAAC,EAAE,CAACxB,WAAW,CAAC,CAAC;EAEjB,IAAA8B,gBAAS,EAAC,MAAM;IACd5B,GAAG,CAACG,OAAO,CAAEmB,OAAO,CAAC,CAAC;EACxB,CAAC,EAAE,CAAC1B,IAAI,CAAC,CAAC;EAEV,OAAO,CAACM,EAAE,CAACQ,KAAK,EAAER,EAAE,CAACI,MAAM,CAAC;AAC9B;AAAC,IAAAyB,QAAA,GAAAC,OAAA,CAAAC,OAAA,GAEctC,cAAc","ignoreList":[]}
1
+ {"version":3,"file":"useGlobalState.js","names":["_lodash","require","_react","_jsUtils","_GlobalStateProvider","_utils","useGlobalState","path","initialValue","globalState","getGlobalState","ref","useRef","rc","current","emitter","Emitter","setter","value","newState","isFunction","get","process","env","NODE_ENV","isDebugMode","console","groupCollapsed","log","cloneDeep","groupEnd","set","state","emit","subscribe","addListener","bind","watcher","useSyncExternalStore","initialState","useEffect","watch","unWatch","_default","exports","default"],"sources":["../../src/useGlobalState.ts"],"sourcesContent":["// Hook for updates of global state.\n\nimport { cloneDeep, isFunction } from 'lodash';\nimport { useEffect, useRef, useSyncExternalStore } from 'react';\n\nimport { Emitter } from '@dr.pogodin/js-utils';\n\nimport 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 isDebugMode,\n} from './utils';\n\nexport type SetterT<T> = React.Dispatch<React.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<ValueAtPathT<StateT, PathT, never>>\n): UseGlobalStateResT<ValueAtPathT<StateT, PathT, void>>;\n\nfunction useGlobalState(\n path?: null | string,\n initialValue?: ValueOrInitializerT<unknown>,\n): UseGlobalStateResT<any> {\n const globalState = getGlobalState();\n\n const ref = useRef<GlobalStateRef>();\n\n let rc: GlobalStateRef;\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))\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:', cloneDeep(newState));\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 const state = rc!.globalState.get(rc!.path);\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 { initialValue, initialState: true },\n ),\n );\n\n useEffect(() => {\n const { watcher } = ref.current!;\n globalState.watch(watcher);\n watcher();\n return () => globalState.unWatch(watcher);\n }, [globalState]);\n\n useEffect(() => {\n ref.current!.watcher();\n }, [path]);\n\n return [rc.state, rc.setter];\n}\n\nexport default useGlobalState;\n\nexport interface UseGlobalStateI<StateT> {\n (): UseGlobalStateResT<StateT>;\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":";;;;;;AAEA,IAAAA,OAAA,GAAAC,OAAA;AACA,IAAAC,MAAA,GAAAD,OAAA;AAEA,IAAAE,QAAA,GAAAF,OAAA;AAGA,IAAAG,oBAAA,GAAAH,OAAA;AAEA,IAAAI,MAAA,GAAAJ,OAAA;AAVA;;AAoCA;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;;AASA,SAASK,cAAcA,CACrBC,IAAoB,EACpBC,YAA2C,EAClB;EACzB,MAAMC,WAAW,GAAG,IAAAC,mCAAc,EAAC,CAAC;EAEpC,MAAMC,GAAG,GAAG,IAAAC,aAAM,EAAiB,CAAC;EAEpC,IAAIC,EAAkB;EACtB,IAAI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AAEmB,KAAK,EAAEnB,EAAE,CAAEE,OAAO,CAACkB,IAAI,CAAC,CAAC;MAC7C;IACF,CAAC;EACH;EACApB,EAAE,GAAGF,GAAG,CAACG,OAAQ;EAEjBD,EAAE,CAACJ,WAAW,GAAGA,WAAW;EAC5BI,EAAE,CAACN,IAAI,GAAGA,IAAI;EAEdM,EAAE,CAACmB,KAAK,GAAG,IAAAM,2BAAoB,EAC7BzB,EAAE,CAACqB,SAAS,EACZ,MAAMrB,EAAE,CAAEJ,WAAW,CAACY,GAAG,CAAkBR,EAAE,CAAEN,IAAI,EAAE;IAAEC;EAAa,CAAC,CAAC,EAEtE,MAAMK,EAAE,CAAEJ,WAAW,CAACY,GAAG,CACvBR,EAAE,CAAEN,IAAI,EACR;IAAEC,YAAY;IAAE+B,YAAY,EAAE;EAAK,CACrC,CACF,CAAC;EAED,IAAAC,gBAAS,EAAC,MAAM;IACd,MAAM;MAAEH;IAAQ,CAAC,GAAG1B,GAAG,CAACG,OAAQ;IAChCL,WAAW,CAACgC,KAAK,CAACJ,OAAO,CAAC;IAC1BA,OAAO,CAAC,CAAC;IACT,OAAO,MAAM5B,WAAW,CAACiC,OAAO,CAACL,OAAO,CAAC;EAC3C,CAAC,EAAE,CAAC5B,WAAW,CAAC,CAAC;EAEjB,IAAA+B,gBAAS,EAAC,MAAM;IACd7B,GAAG,CAACG,OAAO,CAAEuB,OAAO,CAAC,CAAC;EACxB,CAAC,EAAE,CAAC9B,IAAI,CAAC,CAAC;EAEV,OAAO,CAACM,EAAE,CAACmB,KAAK,EAAEnB,EAAE,CAACI,MAAM,CAAC;AAC9B;AAAC,IAAA0B,QAAA,GAAAC,OAAA,CAAAC,OAAA,GAEcvC,cAAc","ignoreList":[]}
@@ -66,42 +66,47 @@ import { isDebugMode } from "./utils";
66
66
  function useGlobalState(path, initialValue) {
67
67
  const globalState = getGlobalState();
68
68
  const ref = useRef();
69
- const rc = ref.current || {
70
- emitter: new Emitter(),
71
- globalState,
72
- path,
73
- setter: value => {
74
- const newState = isFunction(value) ? value(rc.state) : value;
75
- if (process.env.NODE_ENV !== 'production' && isDebugMode()) {
76
- /* eslint-disable no-console */
77
- console.groupCollapsed(`ReactGlobalState - useGlobalState setter triggered for path ${rc.path || ''}`);
78
- console.log('New value:', cloneDeep(newState));
79
- console.groupEnd();
80
- /* eslint-enable no-console */
81
- }
82
- rc.globalState.set(rc.path, newState);
69
+ let rc;
70
+ if (!ref.current) {
71
+ const emitter = new Emitter();
72
+ ref.current = {
73
+ emitter,
74
+ globalState,
75
+ path,
76
+ setter: value => {
77
+ const newState = isFunction(value) ? value(rc.globalState.get(rc.path)) : value;
78
+ if (process.env.NODE_ENV !== 'production' && isDebugMode()) {
79
+ /* eslint-disable no-console */
80
+ console.groupCollapsed(`ReactGlobalState - useGlobalState setter triggered for path ${rc.path || ''}`);
81
+ console.log('New value:', cloneDeep(newState));
82
+ console.groupEnd();
83
+ /* eslint-enable no-console */
84
+ }
85
+ rc.globalState.set(rc.path, newState);
83
86
 
84
- // NOTE: The regular global state's update notifications, automatically
85
- // triggered by the rc.globalState.set() call above, are batched, and
86
- // scheduled to fire asynchronosuly at a later time, which is problematic
87
- // for managed text inputs - if they have their value update delayed to
88
- // future render cycles, it will result in reset of their cursor position
89
- // to the value end. Calling the rc.emitter.emit() below causes a sooner
90
- // state update for the current component, thus working around the issue.
91
- // For additional details see the original issue:
92
- // https://github.com/birdofpreyru/react-global-state/issues/22
93
- if (newState !== rc.state) rc.emitter.emit();
94
- },
95
- state: isFunction(initialValue) ? initialValue() : initialValue,
96
- watcher: () => {
97
- const state = rc.globalState.get(rc.path);
98
- if (state !== rc.state) rc.emitter.emit();
99
- }
100
- };
101
- ref.current = rc;
87
+ // NOTE: The regular global state's update notifications, automatically
88
+ // triggered by the rc.globalState.set() call above, are batched, and
89
+ // scheduled to fire asynchronosuly at a later time, which is problematic
90
+ // for managed text inputs - if they have their value update delayed to
91
+ // future render cycles, it will result in reset of their cursor position
92
+ // to the value end. Calling the rc.emitter.emit() below causes a sooner
93
+ // state update for the current component, thus working around the issue.
94
+ // For additional details see the original issue:
95
+ // https://github.com/birdofpreyru/react-global-state/issues/22
96
+ if (newState !== rc.state) rc.emitter.emit();
97
+ },
98
+ state: isFunction(initialValue) ? initialValue() : initialValue,
99
+ subscribe: emitter.addListener.bind(emitter),
100
+ watcher: () => {
101
+ const state = rc.globalState.get(rc.path);
102
+ if (state !== rc.state) rc.emitter.emit();
103
+ }
104
+ };
105
+ }
106
+ rc = ref.current;
102
107
  rc.globalState = globalState;
103
108
  rc.path = path;
104
- rc.state = useSyncExternalStore(cb => rc.emitter.addListener(cb), () => rc.globalState.get(rc.path, {
109
+ rc.state = useSyncExternalStore(rc.subscribe, () => rc.globalState.get(rc.path, {
105
110
  initialValue
106
111
  }), () => rc.globalState.get(rc.path, {
107
112
  initialValue,
@@ -1 +1 @@
1
- {"version":3,"file":"useGlobalState.js","names":["cloneDeep","isFunction","useEffect","useRef","useSyncExternalStore","Emitter","getGlobalState","isDebugMode","useGlobalState","path","initialValue","globalState","ref","rc","current","emitter","setter","value","newState","state","process","env","NODE_ENV","console","groupCollapsed","log","groupEnd","set","emit","watcher","get","cb","addListener","initialState","watch","unWatch"],"sources":["../../src/useGlobalState.ts"],"sourcesContent":["// Hook for updates of global state.\n\nimport { cloneDeep, isFunction } from 'lodash';\nimport { useEffect, useRef, useSyncExternalStore } from 'react';\n\nimport { Emitter } from '@dr.pogodin/js-utils';\n\nimport 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 isDebugMode,\n} from './utils';\n\nexport type SetterT<T> = React.Dispatch<React.SetStateAction<T>>;\n\ntype GlobalStateRef = {\n emitter: Emitter<[]>;\n globalState: GlobalState<unknown>;\n path: null | string | undefined;\n setter: SetterT<unknown>;\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<ValueAtPathT<StateT, PathT, never>>\n): UseGlobalStateResT<ValueAtPathT<StateT, PathT, void>>;\n\nfunction useGlobalState(\n path?: null | string,\n initialValue?: ValueOrInitializerT<unknown>,\n): UseGlobalStateResT<any> {\n const globalState = getGlobalState();\n\n const ref = useRef<GlobalStateRef>();\n const rc: GlobalStateRef = ref.current || {\n emitter: new Emitter(),\n globalState,\n path,\n setter: (value) => {\n const newState = isFunction(value) ? value(rc.state) : value;\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:', cloneDeep(newState));\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 watcher: () => {\n const state = rc.globalState.get(rc.path);\n if (state !== rc.state) rc.emitter.emit();\n },\n };\n ref.current = rc;\n\n rc.globalState = globalState;\n rc.path = path;\n\n rc.state = useSyncExternalStore(\n (cb) => rc.emitter.addListener(cb),\n () => rc.globalState.get<ForceT, unknown>(rc.path, { initialValue }),\n () => rc.globalState.get<ForceT, unknown>(rc.path, { initialValue, initialState: true }),\n );\n\n useEffect(() => {\n const { watcher } = ref.current!;\n globalState.watch(watcher);\n watcher();\n return () => globalState.unWatch(watcher);\n }, [globalState]);\n\n useEffect(() => {\n ref.current!.watcher();\n }, [path]);\n\n return [rc.state, rc.setter];\n}\n\nexport default useGlobalState;\n\nexport interface UseGlobalStateI<StateT> {\n (): UseGlobalStateResT<StateT>;\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,SAAS,EAAEC,UAAU,QAAQ,QAAQ;AAC9C,SAASC,SAAS,EAAEC,MAAM,EAAEC,oBAAoB,QAAQ,OAAO;AAE/D,SAASC,OAAO,QAAQ,sBAAsB;AAG9C,SAASC,cAAc;AAEvB,SAOEC,WAAW;;AAgBb;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;;AASA,SAASC,cAAcA,CACrBC,IAAoB,EACpBC,YAA2C,EAClB;EACzB,MAAMC,WAAW,GAAGL,cAAc,CAAC,CAAC;EAEpC,MAAMM,GAAG,GAAGT,MAAM,CAAiB,CAAC;EACpC,MAAMU,EAAkB,GAAGD,GAAG,CAACE,OAAO,IAAI;IACxCC,OAAO,EAAE,IAAIV,OAAO,CAAC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OAAO,CAACiB,WAAW,CAACD,EAAE,CAAC,EAClC,MAAMlB,EAAE,CAACF,WAAW,CAACmB,GAAG,CAAkBjB,EAAE,CAACJ,IAAI,EAAE;IAAEC;EAAa,CAAC,CAAC,EACpE,MAAMG,EAAE,CAACF,WAAW,CAACmB,GAAG,CAAkBjB,EAAE,CAACJ,IAAI,EAAE;IAAEC,YAAY;IAAEuB,YAAY,EAAE;EAAK,CAAC,CACzF,CAAC;EAED/B,SAAS,CAAC,MAAM;IACd,MAAM;MAAE2B;IAAQ,CAAC,GAAGjB,GAAG,CAACE,OAAQ;IAChCH,WAAW,CAACuB,KAAK,CAACL,OAAO,CAAC;IAC1BA,OAAO,CAAC,CAAC;IACT,OAAO,MAAMlB,WAAW,CAACwB,OAAO,CAACN,OAAO,CAAC;EAC3C,CAAC,EAAE,CAAClB,WAAW,CAAC,CAAC;EAEjBT,SAAS,CAAC,MAAM;IACdU,GAAG,CAACE,OAAO,CAAEe,OAAO,CAAC,CAAC;EACxB,CAAC,EAAE,CAACpB,IAAI,CAAC,CAAC;EAEV,OAAO,CAACI,EAAE,CAACM,KAAK,EAAEN,EAAE,CAACG,MAAM,CAAC;AAC9B;AAEA,eAAeR,cAAc","ignoreList":[]}
1
+ {"version":3,"file":"useGlobalState.js","names":["cloneDeep","isFunction","useEffect","useRef","useSyncExternalStore","Emitter","getGlobalState","isDebugMode","useGlobalState","path","initialValue","globalState","ref","rc","current","emitter","setter","value","newState","get","process","env","NODE_ENV","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 { cloneDeep, isFunction } from 'lodash';\nimport { useEffect, useRef, useSyncExternalStore } from 'react';\n\nimport { Emitter } from '@dr.pogodin/js-utils';\n\nimport 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 isDebugMode,\n} from './utils';\n\nexport type SetterT<T> = React.Dispatch<React.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<ValueAtPathT<StateT, PathT, never>>\n): UseGlobalStateResT<ValueAtPathT<StateT, PathT, void>>;\n\nfunction useGlobalState(\n path?: null | string,\n initialValue?: ValueOrInitializerT<unknown>,\n): UseGlobalStateResT<any> {\n const globalState = getGlobalState();\n\n const ref = useRef<GlobalStateRef>();\n\n let rc: GlobalStateRef;\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))\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:', cloneDeep(newState));\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 const state = rc!.globalState.get(rc!.path);\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 { initialValue, initialState: true },\n ),\n );\n\n useEffect(() => {\n const { watcher } = ref.current!;\n globalState.watch(watcher);\n watcher();\n return () => globalState.unWatch(watcher);\n }, [globalState]);\n\n useEffect(() => {\n ref.current!.watcher();\n }, [path]);\n\n return [rc.state, rc.setter];\n}\n\nexport default useGlobalState;\n\nexport interface UseGlobalStateI<StateT> {\n (): UseGlobalStateResT<StateT>;\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,SAAS,EAAEC,UAAU,QAAQ,QAAQ;AAC9C,SAASC,SAAS,EAAEC,MAAM,EAAEC,oBAAoB,QAAQ,OAAO;AAE/D,SAASC,OAAO,QAAQ,sBAAsB;AAG9C,SAASC,cAAc;AAEvB,SAOEC,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;A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ACG,MAAM,CAAC;AAC9B;AAEA,eAAeR,cAAc","ignoreList":[]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dr.pogodin/react-global-state",
3
- "version": "0.13.0",
3
+ "version": "0.14.0",
4
4
  "description": "Hook-based global state for React",
5
5
  "main": "./build/common/index.js",
6
6
  "react-native": "src/index.ts",
@@ -43,30 +43,30 @@
43
43
  "node": ">=18"
44
44
  },
45
45
  "dependencies": {
46
- "@babel/runtime": "^7.24.0",
46
+ "@babel/runtime": "^7.24.1",
47
47
  "@dr.pogodin/js-utils": "^0.0.9",
48
- "@types/lodash": "^4.14.202",
48
+ "@types/lodash": "^4.17.0",
49
49
  "lodash": "^4.17.21",
50
50
  "uuid": "^9.0.1"
51
51
  },
52
52
  "devDependencies": {
53
- "@babel/cli": "^7.23.9",
54
- "@babel/core": "^7.24.0",
55
- "@babel/eslint-parser": "^7.23.10",
53
+ "@babel/cli": "^7.24.1",
54
+ "@babel/core": "^7.24.3",
55
+ "@babel/eslint-parser": "^7.24.1",
56
56
  "@babel/eslint-plugin": "^7.23.5",
57
57
  "@babel/node": "^7.23.9",
58
- "@babel/plugin-transform-runtime": "^7.24.0",
59
- "@babel/preset-env": "^7.24.0",
60
- "@babel/preset-react": "^7.23.3",
61
- "@babel/preset-typescript": "^7.23.3",
62
- "@tsconfig/recommended": "^1.0.3",
63
- "@tsd/typescript": "^5.3.3",
58
+ "@babel/plugin-transform-runtime": "^7.24.3",
59
+ "@babel/preset-env": "^7.24.3",
60
+ "@babel/preset-react": "^7.24.1",
61
+ "@babel/preset-typescript": "^7.24.1",
62
+ "@tsconfig/recommended": "^1.0.5",
63
+ "@tsd/typescript": "^5.4.3",
64
64
  "@types/jest": "^29.5.12",
65
65
  "@types/pretty": "^2.0.3",
66
- "@types/react": "^18.2.64",
67
- "@types/react-dom": "^18.2.21",
66
+ "@types/react": "^18.2.73",
67
+ "@types/react-dom": "^18.2.23",
68
68
  "@types/uuid": "^9.0.8",
69
- "typescript-eslint": "^7.1.1",
69
+ "typescript-eslint": "^7.4.0",
70
70
  "babel-jest": "^29.7.0",
71
71
  "babel-plugin-module-resolver": "^5.0.0",
72
72
  "eslint": "^8.57.0",
@@ -76,7 +76,7 @@
76
76
  "eslint-plugin-import": "^2.29.1",
77
77
  "eslint-plugin-jest": "^27.9.0",
78
78
  "eslint-plugin-jsx-a11y": "^6.8.0",
79
- "eslint-plugin-react": "^7.34.0",
79
+ "eslint-plugin-react": "^7.34.1",
80
80
  "eslint-plugin-react-hooks": "^4.6.0",
81
81
  "jest": "^29.7.0",
82
82
  "jest-environment-jsdom": "^29.7.0",
@@ -85,7 +85,7 @@
85
85
  "pretty": "^2.0.0",
86
86
  "rimraf": "^5.0.1",
87
87
  "tsd-lite": "^0.9.0",
88
- "typescript": "^5.4.2"
88
+ "typescript": "^5.4.3"
89
89
  },
90
90
  "peerDependencies": {
91
91
  "react": "^18.2.0",
@@ -20,11 +20,14 @@ import {
20
20
 
21
21
  export type SetterT<T> = React.Dispatch<React.SetStateAction<T>>;
22
22
 
23
+ type ListenerT = () => void;
24
+
23
25
  type GlobalStateRef = {
24
26
  emitter: Emitter<[]>;
25
27
  globalState: GlobalState<unknown>;
26
28
  path: null | string | undefined;
27
29
  setter: SetterT<unknown>;
30
+ subscribe: (listener: ListenerT) => () => void;
28
31
  state: unknown;
29
32
  watcher: CallbackT;
30
33
  };
@@ -110,51 +113,64 @@ function useGlobalState(
110
113
  const globalState = getGlobalState();
111
114
 
112
115
  const ref = useRef<GlobalStateRef>();
113
- const rc: GlobalStateRef = ref.current || {
114
- emitter: new Emitter(),
115
- globalState,
116
- path,
117
- setter: (value) => {
118
- const newState = isFunction(value) ? value(rc.state) : value;
119
- if (process.env.NODE_ENV !== 'production' && isDebugMode()) {
120
- /* eslint-disable no-console */
121
- console.groupCollapsed(
122
- `ReactGlobalState - useGlobalState setter triggered for path ${
123
- rc.path || ''
124
- }`,
125
- );
126
- console.log('New value:', cloneDeep(newState));
127
- console.groupEnd();
128
- /* eslint-enable no-console */
129
- }
130
- rc.globalState.set<ForceT, unknown>(rc.path, newState);
131
-
132
- // NOTE: The regular global state's update notifications, automatically
133
- // triggered by the rc.globalState.set() call above, are batched, and
134
- // scheduled to fire asynchronosuly at a later time, which is problematic
135
- // for managed text inputs - if they have their value update delayed to
136
- // future render cycles, it will result in reset of their cursor position
137
- // to the value end. Calling the rc.emitter.emit() below causes a sooner
138
- // state update for the current component, thus working around the issue.
139
- // For additional details see the original issue:
140
- // https://github.com/birdofpreyru/react-global-state/issues/22
141
- if (newState !== rc.state) rc.emitter.emit();
142
- },
143
- state: isFunction(initialValue) ? initialValue() : initialValue,
144
- watcher: () => {
145
- const state = rc.globalState.get(rc.path);
146
- if (state !== rc.state) rc.emitter.emit();
147
- },
148
- };
149
- ref.current = rc;
116
+
117
+ let rc: GlobalStateRef;
118
+ if (!ref.current) {
119
+ const emitter = new Emitter();
120
+ ref.current = {
121
+ emitter,
122
+ globalState,
123
+ path,
124
+ setter: (value) => {
125
+ const newState = isFunction(value)
126
+ ? value(rc!.globalState.get(rc!.path))
127
+ : value;
128
+
129
+ if (process.env.NODE_ENV !== 'production' && isDebugMode()) {
130
+ /* eslint-disable no-console */
131
+ console.groupCollapsed(
132
+ `ReactGlobalState - useGlobalState setter triggered for path ${
133
+ rc!.path || ''
134
+ }`,
135
+ );
136
+ console.log('New value:', cloneDeep(newState));
137
+ console.groupEnd();
138
+ /* eslint-enable no-console */
139
+ }
140
+ rc!.globalState.set<ForceT, unknown>(rc!.path, newState);
141
+
142
+ // NOTE: The regular global state's update notifications, automatically
143
+ // triggered by the rc.globalState.set() call above, are batched, and
144
+ // scheduled to fire asynchronosuly at a later time, which is problematic
145
+ // for managed text inputs - if they have their value update delayed to
146
+ // future render cycles, it will result in reset of their cursor position
147
+ // to the value end. Calling the rc.emitter.emit() below causes a sooner
148
+ // state update for the current component, thus working around the issue.
149
+ // For additional details see the original issue:
150
+ // https://github.com/birdofpreyru/react-global-state/issues/22
151
+ if (newState !== rc!.state) rc!.emitter.emit();
152
+ },
153
+ state: isFunction(initialValue) ? initialValue() : initialValue,
154
+ subscribe: emitter.addListener.bind(emitter),
155
+ watcher: () => {
156
+ const state = rc!.globalState.get(rc!.path);
157
+ if (state !== rc!.state) rc!.emitter.emit();
158
+ },
159
+ };
160
+ }
161
+ rc = ref.current!;
150
162
 
151
163
  rc.globalState = globalState;
152
164
  rc.path = path;
153
165
 
154
166
  rc.state = useSyncExternalStore(
155
- (cb) => rc.emitter.addListener(cb),
156
- () => rc.globalState.get<ForceT, unknown>(rc.path, { initialValue }),
157
- () => rc.globalState.get<ForceT, unknown>(rc.path, { initialValue, initialState: true }),
167
+ rc.subscribe,
168
+ () => rc!.globalState.get<ForceT, unknown>(rc!.path, { initialValue }),
169
+
170
+ () => rc!.globalState.get<ForceT, unknown>(
171
+ rc!.path,
172
+ { initialValue, initialState: true },
173
+ ),
158
174
  );
159
175
 
160
176
  useEffect(() => {