@webkrafters/react-observable-context 2.0.1 → 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/dist/index.d.ts +11 -8
- package/dist/index.js +86 -13
- package/package.json +5 -3
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>():
|
|
4
|
-
export function useContext<T_1 extends State>(context:
|
|
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 {
|
|
9
|
-
* children?:
|
|
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:
|
|
17
|
-
children?:
|
|
18
|
-
context:
|
|
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> =
|
|
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); }
|
|
@@ -144,24 +146,82 @@ var usePrehooksRef = function usePrehooksRef(prehooks) {
|
|
|
144
146
|
return prehooksRef;
|
|
145
147
|
};
|
|
146
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
|
+
|
|
147
207
|
/**
|
|
148
208
|
* Note: `context` prop is not updateable. Furtther updates to this prop are ignored.
|
|
149
209
|
*
|
|
150
|
-
* @type {
|
|
151
|
-
* children?:
|
|
210
|
+
* @type {FC<{
|
|
211
|
+
* children?: ReactNode,
|
|
152
212
|
* context: ObservableContext<T>,
|
|
153
213
|
* prehooks?: Prehooks<T>
|
|
154
214
|
* value: PartialState<T>
|
|
155
215
|
* }>}
|
|
156
216
|
* @template {State} T
|
|
157
217
|
*/
|
|
158
|
-
var Provider = function Provider(
|
|
159
|
-
var
|
|
160
|
-
children =
|
|
161
|
-
context =
|
|
162
|
-
|
|
163
|
-
prehooks =
|
|
164
|
-
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;
|
|
165
225
|
var prehooksRef = usePrehooksRef(prehooks);
|
|
166
226
|
var initialState = (0, _react.useRef)(value);
|
|
167
227
|
|
|
@@ -232,7 +292,8 @@ var Provider = function Provider(_ref) {
|
|
|
232
292
|
}),
|
|
233
293
|
_useState10 = _slicedToArray(_useState9, 1),
|
|
234
294
|
store = _useState10[0];
|
|
235
|
-
return /*#__PURE__*/_react["default"].createElement(
|
|
295
|
+
return /*#__PURE__*/_react["default"].createElement(ProviderInternal, {
|
|
296
|
+
context: StoreContext,
|
|
236
297
|
value: store
|
|
237
298
|
}, children);
|
|
238
299
|
};
|
|
@@ -240,7 +301,7 @@ exports.Provider = Provider;
|
|
|
240
301
|
Provider.displayName = 'ObservableContext.Provider';
|
|
241
302
|
|
|
242
303
|
/**
|
|
243
|
-
* @typedef {
|
|
304
|
+
* @typedef {Context<Store<T>>} ObservableContext
|
|
244
305
|
* @template {State} T
|
|
245
306
|
*/
|
|
246
307
|
|
|
@@ -284,4 +345,16 @@ Provider.displayName = 'ObservableContext.Provider';
|
|
|
284
345
|
* @template {State} T
|
|
285
346
|
*/
|
|
286
347
|
|
|
287
|
-
/** @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
|
|
81
|
+
"version": "2.1.0"
|
|
80
82
|
}
|