@webkrafters/react-observable-context 2.1.3 → 2.1.4

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.
Files changed (3) hide show
  1. package/README.md +7 -3
  2. package/dist/index.js +58 -25
  3. package/package.json +2 -1
package/README.md CHANGED
@@ -4,11 +4,13 @@ A context bearing an observable consumer store. State changes within the store's
4
4
 
5
5
  **React::memo** *(and PureComponents)* remain the go-to solution for the repeated automatic re-renderings of entire component trees resulting from ***component*** state changes.
6
6
 
7
- _**Recommendation:**_ Wrap all components calling this package's ***useContext*** function in **React::memo**. This will protect such components and their descendants from unrelated cascading render operations.
7
+ _**Recommendation:**_ For optimum performance, consider wrapping in **React::memo** most components using this package's ***useContext*** function either directly or through another React hook. This will protect such components and their descendants from unrelated cascading render operations.
8
+
9
+ ***Exempt*** from the recommendation are certain components such as those wrapped in the `React-Redux::connect()` Higher Order Component (HOC). Such HOC provides similar cascade-render protections to wrapped components and their descendants.
8
10
 
9
11
  <hr />
10
12
 
11
- <h4><u>Install</u></h4>
13
+ <h3><u>Install</u></h3>
12
14
 
13
15
  npm i -S @webkrafters/react-observable-context
14
16
 
@@ -16,7 +18,7 @@ npm install --save @webkrafters/react-observable-context
16
18
 
17
19
  ## API
18
20
 
19
- The React-Observable-Context package exports **3** modules namely: the **createContext** method, the **useContext** hook and the **Provider** component.
21
+ The React-Observable-Context package exports **4** modules namely: the **createContext** method, the **useContext** hook, the **Provider** component and the **UsageError** class.
20
22
 
21
23
  * **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`.
22
24
 
@@ -24,6 +26,8 @@ The React-Observable-Context package exports **3** modules namely: the **createC
24
26
 
25
27
  * **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>.
26
28
 
29
+ * **UsageError** class is the Error type reported for attempts to access this context's store outside of its Provider component tree.
30
+
27
31
  ***<u>Note:</u>*** the Provider `context` prop is not updateable. Once set, all further updates to this prop are not recorded.
28
32
 
29
33
  ### The Store
package/dist/index.js CHANGED
@@ -9,7 +9,8 @@ var _lodash = _interopRequireDefault(require("lodash.clonedeep"));
9
9
  var _lodash2 = _interopRequireDefault(require("lodash.has"));
10
10
  var _lodash3 = _interopRequireDefault(require("lodash.isempty"));
11
11
  var _lodash4 = _interopRequireDefault(require("lodash.isequal"));
12
- var _lodash5 = _interopRequireDefault(require("lodash.omit"));
12
+ var _lodash5 = _interopRequireDefault(require("lodash.isplainobject"));
13
+ var _lodash6 = _interopRequireDefault(require("lodash.omit"));
13
14
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
14
15
  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); }
15
16
  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; }
@@ -111,28 +112,59 @@ var useContext = function useContext(context) {
111
112
  */
112
113
  exports.useContext = useContext;
113
114
  var defaultPrehooks = Object.freeze({});
114
-
115
- /**
116
- * @param {T} state
117
- * @param {PartialState<T>} newState
118
- * @param {Listener<T>} onStateChange
119
- * @template {State} T
120
- */
121
- var _setState = function _setState(state, newState, onStateChange) {
122
- /** @type {PartialState<T>} */
123
- var newChanges = {};
124
- /** @type {PartialState<T>} */
125
- var replacedValue = {};
126
- for (var k in newState) {
127
- if (state[k] === newState[k]) {
128
- continue;
115
+ var _setState = function () {
116
+ var setAtomic = function setAtomic(state, newState, changed, replaced, stateKey) {
117
+ if ((0, _lodash4["default"])(state[stateKey], newState[stateKey])) {
118
+ return;
129
119
  }
130
- replacedValue[k] = state[k];
131
- state[k] = newState[k];
132
- newChanges[k] = newState[k];
133
- }
134
- !(0, _lodash3["default"])(newChanges) && onStateChange(newChanges, replacedValue);
135
- };
120
+ var isArrayNewState = Array.isArray(newState[stateKey]);
121
+ if (Array.isArray(state[stateKey]) && isArrayNewState) {
122
+ return setArray(state, newState, changed, replaced, stateKey);
123
+ }
124
+ var isPlainObjectNewState = (0, _lodash5["default"])(newState[stateKey]);
125
+ if ((0, _lodash5["default"])(state[stateKey]) && isPlainObjectNewState) {
126
+ return setPlainObject(state, newState, changed, replaced, stateKey);
127
+ }
128
+ replaced[stateKey] = state[stateKey];
129
+ state[stateKey] = isArrayNewState || isPlainObjectNewState ? (0, _lodash["default"])(newState[stateKey]) : newState[stateKey];
130
+ changed[stateKey] = newState[stateKey];
131
+ };
132
+ var setArray = function setArray(state, newState, changed, replaced, rootKey) {
133
+ changed[rootKey] = {};
134
+ replaced[rootKey] = {};
135
+ for (var i = 0, len = newState[rootKey].length; i < len; i++) {
136
+ setAtomic(state[rootKey], newState[rootKey], changed[rootKey], replaced[rootKey], i);
137
+ }
138
+ };
139
+ var setPlainObject = function setPlainObject(state, newState, changed, replaced, rootKey) {
140
+ changed[rootKey] = {};
141
+ replaced[rootKey] = {};
142
+ for (var k in newState[rootKey]) {
143
+ setAtomic(state[rootKey], newState[rootKey], changed[rootKey], replaced[rootKey], k);
144
+ }
145
+ };
146
+ var set = function set(state, newState) {
147
+ var changed = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
148
+ var replaced = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : {};
149
+ for (var k in newState) {
150
+ setAtomic(state, newState, changed, replaced, k);
151
+ }
152
+ };
153
+ /**
154
+ * @param {T} state
155
+ * @param {PartialState<T>} newState
156
+ * @param {Listener<T>} onStateChange
157
+ * @template {State} T
158
+ */
159
+ return function (state, newState, onStateChange) {
160
+ /** @type {PartialState<T>} */
161
+ var newChanges = {};
162
+ /** @type {PartialState<T>} */
163
+ var replacedValue = {};
164
+ set(state, newState, newChanges, replacedValue);
165
+ !(0, _lodash3["default"])(newChanges) && onStateChange(newChanges, replacedValue);
166
+ };
167
+ }();
136
168
 
137
169
  /**
138
170
  * @param {Prehooks<T>} prehooks
@@ -150,7 +182,7 @@ var usePrehooksRef = function usePrehooksRef(prehooks) {
150
182
  var ChildMemo = function () {
151
183
  var useNodeMemo = function useNodeMemo(node) {
152
184
  var nodeRef = (0, _react.useRef)(node);
153
- if (!(0, _lodash4["default"])((0, _lodash5["default"])(nodeRef.current, '_owner'), (0, _lodash5["default"])(node, '_owner'))) {
185
+ if (!(0, _lodash4["default"])((0, _lodash6["default"])(nodeRef.current, '_owner'), (0, _lodash6["default"])(node, '_owner'))) {
154
186
  nodeRef.current = node;
155
187
  }
156
188
  return nodeRef.current;
@@ -178,7 +210,7 @@ var memoizeImmediateChildTree = function memoizeImmediateChildTree(children) {
178
210
  return child;
179
211
  }
180
212
  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));
213
+ child = /*#__PURE__*/(0, _react.cloneElement)(child, (0, _lodash6["default"])(child.props, 'children'), memoizeImmediateChildTree(child.props.children));
182
214
  }
183
215
  return /*#__PURE__*/_react["default"].createElement(ChildMemo, {
184
216
  child: child
@@ -252,7 +284,8 @@ var Provider = function Provider(_ref4) {
252
284
  /** @type {Store<T>["getState"]} */
253
285
  var getState = (0, _react.useCallback)(function () {
254
286
  var selector = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : defaultSelector;
255
- return selector(state);
287
+ var slice = selector(state);
288
+ return _typeof(slice) === 'object' ? (0, _lodash["default"])(slice) : slice;
256
289
  }, []);
257
290
 
258
291
  /** @type {Store<T>["resetState"]} */
package/package.json CHANGED
@@ -12,6 +12,7 @@
12
12
  "lodash.has": "^4.5.2",
13
13
  "lodash.isempty": "^4.4.0",
14
14
  "lodash.isequal": "^4.5.0",
15
+ "lodash.isplainobject": "^4.0.6",
15
16
  "lodash.omit": "^4.5.0",
16
17
  "react": "^18.2.0"
17
18
  },
@@ -78,5 +79,5 @@
78
79
  "test:watch": "eslint --fix && jest --updateSnapshot --watchAll"
79
80
  },
80
81
  "types": "dist/index.d.ts",
81
- "version": "2.1.3"
82
+ "version": "2.1.4"
82
83
  }