@webkrafters/react-observable-context 2.0.0 → 2.1.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.
package/README.md CHANGED
@@ -16,7 +16,7 @@ The React-Observable-Context package exports **3** modules namely: the **createC
16
16
 
17
17
  * **createContext** is a zero-parameter funtion returning a store-bearing context. Pass the context to the React::useContext() parameter to obtain the context's `store`.
18
18
 
19
- * **useContext** is analogous to React::useContext hook but returns the context store and takes a second parameter named ***watchedKeys***. The `watchedKeys` parameter is a list of state object property names to watch. Change in any of the referenced properties automatically triggers a render of the component calling this hook.
19
+ * **useContext** is analogous to React::useContext hook but returns the context store and takes a second parameter named ***watchedKeys***. The `watchedKeys` parameter is a list of state object property paths to watch. A change in any of the referenced properties automatically triggers a render of the component calling this hook.
20
20
 
21
21
  * **Provider** can immediately be used as-is anywhere the React-Observable-Context is required. It accepts **3** props and the customary Provider `children` prop. Supply the context to its `context` prop; the initial state to the customary Provider `value` prop; and the optional `prehooks` prop <i>(discussed in the prehooks section below)</i>.
22
22
 
@@ -30,7 +30,7 @@ The context's `store` exposes **4** methods for interacting with the context's i
30
30
 
31
31
  * **resetState**: VoidFunction // resets the state to the Provider initial `value` prop.
32
32
 
33
- * **setState**: (changes: PartialState\<State\>) => void
33
+ * **setState**: (changes: PartialState\<State\>) => void // sets only new/changed top level properties.
34
34
 
35
35
  * **subscribe**: (listener: (newValue: PartialState\<State\>, oldValue: PartialState\<State\>) => void) => ***UnsubscribeFunction***
36
36
 
package/dist/index.d.ts CHANGED
@@ -1,25 +1,25 @@
1
1
  export class UsageError extends Error {
2
2
  }
3
- export function createContext<T_1 extends State>(): React.Context<Store<T_2>>;
4
- export function useContext<T_1 extends State>(context: React.Context<Store<T_2>>, watchedKeys?: (keyof T_1)[]): Store<T_1>;
3
+ export function createContext<T_1 extends State>(): import("react").Context<Store<T_1>>;
4
+ export function useContext<T_1 extends State>(context: import("react").Context<Store<T_1>>, watchedKeys?: (string | keyof T_1)[]): Store<T_1>;
5
5
  /**
6
6
  * Note: `context` prop is not updateable. Furtther updates to this prop are ignored.
7
7
  *
8
- * @type {React.FC<{
9
- * children?: React.ReactNode,
8
+ * @type {FC<{
9
+ * children?: ReactNode,
10
10
  * context: ObservableContext<T>,
11
11
  * prehooks?: Prehooks<T>
12
12
  * value: PartialState<T>
13
13
  * }>}
14
14
  * @template {State} T
15
15
  */
16
- export const Provider: React.FC<{
17
- children?: React.ReactNode;
18
- context: React.Context<Store<T_1>>;
16
+ export const Provider: FC<{
17
+ children?: ReactNode;
18
+ context: ObservableContext<T>;
19
19
  prehooks?: Prehooks<T>;
20
20
  value: PartialState<T>;
21
21
  }>;
22
- export type ObservableContext<T_1 extends State> = React.Context<Store<T>>;
22
+ export type ObservableContext<T_1 extends State> = Context<Store<T>>;
23
23
  export type OptionalTask<F = void> = F extends void ? () => never : F;
24
24
  export type Listener<T_1 extends State> = (newValue: PartialState<T>, oldValue: PartialState<T>) => void;
25
25
  export type State = {
@@ -43,4 +43,7 @@ export type Store<T_1 extends State> = {
43
43
  subscribe: (listener: Listener<T>) => Unsubscribe;
44
44
  };
45
45
  export type Unsubscribe = VoidFunction;
46
+ export type ReactNode = import("react").ReactNode;
47
+ export type FC<P = {}> = import("react").FC<P>;
48
+ export type Context<T_1> = import("react").Context<T>;
46
49
  //# sourceMappingURL=index.d.ts.map
package/dist/index.js CHANGED
@@ -1,6 +1,5 @@
1
1
  "use strict";
2
2
 
3
- function _typeof(obj) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (obj) { return typeof obj; } : function (obj) { return obj && "function" == typeof Symbol && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }, _typeof(obj); }
4
3
  Object.defineProperty(exports, "__esModule", {
5
4
  value: true
6
5
  });
@@ -9,9 +8,12 @@ var _react = _interopRequireWildcard(require("react"));
9
8
  var _lodash = _interopRequireDefault(require("lodash.clonedeep"));
10
9
  var _lodash2 = _interopRequireDefault(require("lodash.has"));
11
10
  var _lodash3 = _interopRequireDefault(require("lodash.isempty"));
11
+ var _lodash4 = _interopRequireDefault(require("lodash.isequal"));
12
+ var _lodash5 = _interopRequireDefault(require("lodash.omit"));
12
13
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
13
14
  function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
14
15
  function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || _typeof(obj) !== "object" && typeof obj !== "function") { return { "default": obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj["default"] = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
16
+ function _typeof(obj) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (obj) { return typeof obj; } : function (obj) { return obj && "function" == typeof Symbol && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }, _typeof(obj); }
15
17
  function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }
16
18
  function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
17
19
  function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
@@ -72,13 +74,14 @@ var createContext = function createContext() {
72
74
  * Actively monitors the store and triggers component re-render if any of the watched keys in the state objects changes
73
75
  *
74
76
  * @param {ObservableContext<T>} context
75
- * @param {Array<keyof T>} [watchedKeys = []] Names of state properties to watch. A change in any of the referenced properties results in this component render.
77
+ * @param {Array<string|keyof T>} [watchedKeys = []] A list of state object property paths to watch. A change in any of the referenced properties results in this component render.
76
78
  * @returns {Store<T>}
77
79
  * @template {State} T
78
80
  */
79
81
  exports.createContext = createContext;
80
82
  var useContext = function useContext(context) {
81
83
  var watchedKeys = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : [];
84
+ /** @type {Store<T>} */
82
85
  var store = (0, _react.useContext)(context);
83
86
  var _useState = (0, _react.useState)(false),
84
87
  _useState2 = _slicedToArray(_useState, 2),
@@ -143,24 +146,82 @@ var usePrehooksRef = function usePrehooksRef(prehooks) {
143
146
  return prehooksRef;
144
147
  };
145
148
 
149
+ /** @type {FC<{child: ReactNode}>} */
150
+ var ChildMemo = function () {
151
+ var useNodeMemo = function useNodeMemo(node) {
152
+ var nodeRef = (0, _react.useRef)(node);
153
+ if (!(0, _lodash4["default"])((0, _lodash5["default"])(nodeRef.current, '_owner'), (0, _lodash5["default"])(node, '_owner'))) {
154
+ nodeRef.current = node;
155
+ }
156
+ return nodeRef.current;
157
+ };
158
+ var ChildMemo = /*#__PURE__*/(0, _react.memo)(function (_ref) {
159
+ var child = _ref.child;
160
+ return child;
161
+ });
162
+ ChildMemo.displayName = 'ObservableContext.Provider.Internal.Guardian.ChildMemo';
163
+ var Guardian = function Guardian(_ref2) {
164
+ var child = _ref2.child;
165
+ return /*#__PURE__*/_react["default"].createElement(ChildMemo, {
166
+ child: useNodeMemo(child)
167
+ });
168
+ };
169
+ Guardian.displayName = 'ObservableContext.Provider.Internal.Guardian';
170
+ return Guardian;
171
+ }();
172
+
173
+ /** @type {(children: ReactNode) => ReactNode} */
174
+ var memoizeImmediateChildTree = function memoizeImmediateChildTree(children) {
175
+ return _react.Children.map(children, function (child) {
176
+ var _child$props;
177
+ if (_typeof(child.type) === 'object' && 'compare' in child.type) {
178
+ return child;
179
+ }
180
+ if ((_child$props = child.props) !== null && _child$props !== void 0 && _child$props.children) {
181
+ child = /*#__PURE__*/(0, _react.cloneElement)(child, (0, _lodash5["default"])(child.props, 'children'), memoizeImmediateChildTree(child.props.children));
182
+ }
183
+ return /*#__PURE__*/_react["default"].createElement(ChildMemo, {
184
+ child: child
185
+ });
186
+ });
187
+ };
188
+
189
+ /**
190
+ * @type {FC<{
191
+ * children?: ReactNode,
192
+ * context: ObservableContext<T>,
193
+ * value: Store<T>
194
+ * }>}
195
+ * @template {State} T
196
+ */
197
+ var ProviderInternal = function ProviderInternal(_ref3) {
198
+ var children = _ref3.children,
199
+ StoreContext = _ref3.context,
200
+ value = _ref3.value;
201
+ return /*#__PURE__*/_react["default"].createElement(StoreContext.Provider, {
202
+ value: value
203
+ }, memoizeImmediateChildTree(children));
204
+ };
205
+ ProviderInternal.displayName = 'ObservableContext.Provider.Internal';
206
+
146
207
  /**
147
208
  * Note: `context` prop is not updateable. Furtther updates to this prop are ignored.
148
209
  *
149
- * @type {React.FC<{
150
- * children?: React.ReactNode,
210
+ * @type {FC<{
211
+ * children?: ReactNode,
151
212
  * context: ObservableContext<T>,
152
213
  * prehooks?: Prehooks<T>
153
214
  * value: PartialState<T>
154
215
  * }>}
155
216
  * @template {State} T
156
217
  */
157
- var Provider = function Provider(_ref) {
158
- var _ref$children = _ref.children,
159
- children = _ref$children === void 0 ? null : _ref$children,
160
- context = _ref.context,
161
- _ref$prehooks = _ref.prehooks,
162
- prehooks = _ref$prehooks === void 0 ? defaultPrehooks : _ref$prehooks,
163
- value = _ref.value;
218
+ var Provider = function Provider(_ref4) {
219
+ var _ref4$children = _ref4.children,
220
+ children = _ref4$children === void 0 ? null : _ref4$children,
221
+ context = _ref4.context,
222
+ _ref4$prehooks = _ref4.prehooks,
223
+ prehooks = _ref4$prehooks === void 0 ? defaultPrehooks : _ref4$prehooks,
224
+ value = _ref4.value;
164
225
  var prehooksRef = usePrehooksRef(prehooks);
165
226
  var initialState = (0, _react.useRef)(value);
166
227
 
@@ -231,7 +292,8 @@ var Provider = function Provider(_ref) {
231
292
  }),
232
293
  _useState10 = _slicedToArray(_useState9, 1),
233
294
  store = _useState10[0];
234
- return /*#__PURE__*/_react["default"].createElement(StoreContext.Provider, {
295
+ return /*#__PURE__*/_react["default"].createElement(ProviderInternal, {
296
+ context: StoreContext,
235
297
  value: store
236
298
  }, children);
237
299
  };
@@ -239,7 +301,7 @@ exports.Provider = Provider;
239
301
  Provider.displayName = 'ObservableContext.Provider';
240
302
 
241
303
  /**
242
- * @typedef {React.Context<Store<T>>} ObservableContext
304
+ * @typedef {Context<Store<T>>} ObservableContext
243
305
  * @template {State} T
244
306
  */
245
307
 
@@ -283,4 +345,16 @@ Provider.displayName = 'ObservableContext.Provider';
283
345
  * @template {State} T
284
346
  */
285
347
 
286
- /** @typedef {VoidFunction} Unsubscribe */
348
+ /** @typedef {VoidFunction} Unsubscribe */
349
+
350
+ /** @typedef {import("react").ReactNode} ReactNode */
351
+
352
+ /**
353
+ * @typedef {import("react").FC<P>} FC
354
+ * @template [P={}]
355
+ */
356
+
357
+ /**
358
+ * @typedef {import("react").Context<T>} Context
359
+ * @template T
360
+ */
package/package.json CHANGED
@@ -11,6 +11,8 @@
11
11
  "lodash.clonedeep": "^4.5.0",
12
12
  "lodash.has": "^4.5.2",
13
13
  "lodash.isempty": "^4.4.0",
14
+ "lodash.isequal": "^4.5.0",
15
+ "lodash.omit": "^4.5.0",
14
16
  "react": "^18.2.0"
15
17
  },
16
18
  "description": "Observable react context - prevents an automatic total component tree tear-down and re-rendering during context updates.",
@@ -72,9 +74,9 @@
72
74
  },
73
75
  "scripts": {
74
76
  "build": "eslint --fix && rm -rf dist && babel src -d dist && npx -p typescript tsc",
75
- "test": "eslint --fix && jest",
76
- "test:watch": "eslint --fix && jest --watchAll"
77
+ "test": "eslint --fix && jest --updateSnapshot",
78
+ "test:watch": "eslint --fix && jest --updateSnapshot --watchAll"
77
79
  },
78
80
  "types": "dist/index.d.ts",
79
- "version": "2.0.0"
81
+ "version": "2.1.0"
80
82
  }