@webkrafters/react-observable-context 3.0.0 → 4.0.0-alpha.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +11 -3
- package/dist/constants.d.ts +1 -0
- package/dist/constants.js +8 -0
- package/dist/main/hooks/use-prehooks-ref/index.d.ts +1 -0
- package/dist/main/hooks/use-prehooks-ref/index.js +16 -0
- package/dist/main/hooks/use-render-keys-manager/index.d.ts +1 -0
- package/dist/main/hooks/use-render-keys-manager/index.js +34 -0
- package/dist/main/hooks/use-state-manager/index.d.ts +1 -0
- package/dist/main/hooks/use-state-manager/index.js +48 -0
- package/dist/main/hooks/use-store/index.d.ts +1 -0
- package/dist/main/hooks/use-store/index.js +121 -0
- package/dist/main/index.d.ts +1 -0
- package/dist/main/index.js +172 -0
- package/dist/main/set-state/index.d.ts +1 -0
- package/dist/main/set-state/index.js +104 -0
- package/dist/model/accessor/index.d.ts +1 -0
- package/dist/model/accessor/index.js +258 -0
- package/dist/model/accessor-cache/index.d.ts +1 -0
- package/dist/model/accessor-cache/index.js +165 -0
- package/dist/model/atom/index.d.ts +1 -0
- package/dist/model/atom/index.js +76 -0
- package/dist/model/storage/index.d.ts +1 -0
- package/dist/model/storage/index.js +129 -0
- package/dist/types.d.ts +1 -0
- package/dist/utils/index.d.ts +1 -0
- package/dist/utils/index.js +26 -0
- package/index.js +1 -1
- package/package.json +39 -12
- package/dist/index.d.ts +0 -33
- package/dist/index.js +0 -389
package/dist/index.js
DELETED
|
@@ -1,389 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
Object.defineProperty(exports, "__esModule", {
|
|
4
|
-
value: true
|
|
5
|
-
});
|
|
6
|
-
exports.useContext = exports.createContext = exports.UsageError = void 0;
|
|
7
|
-
var _react = _interopRequireWildcard(require("react"));
|
|
8
|
-
var _lodash = _interopRequireDefault(require("lodash.clonedeep"));
|
|
9
|
-
var _lodash2 = _interopRequireDefault(require("lodash.has"));
|
|
10
|
-
var _lodash3 = _interopRequireDefault(require("lodash.isempty"));
|
|
11
|
-
var _lodash4 = _interopRequireDefault(require("lodash.isequal"));
|
|
12
|
-
var _lodash5 = _interopRequireDefault(require("lodash.isplainobject"));
|
|
13
|
-
var _lodash6 = _interopRequireDefault(require("lodash.omit"));
|
|
14
|
-
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
|
|
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); }
|
|
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; }
|
|
17
|
-
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); }
|
|
18
|
-
function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }
|
|
19
|
-
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."); }
|
|
20
|
-
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); }
|
|
21
|
-
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; }
|
|
22
|
-
function _iterableToArrayLimit(arr, i) { var _i = arr == null ? null : typeof Symbol !== "undefined" && arr[Symbol.iterator] || arr["@@iterator"]; if (_i == null) return; var _arr = []; var _n = true; var _d = false; var _s, _e; try { for (_i = _i.call(arr); !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"] != null) _i["return"](); } finally { if (_d) throw _e; } } return _arr; }
|
|
23
|
-
function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
|
|
24
|
-
function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }
|
|
25
|
-
function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; }
|
|
26
|
-
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
|
|
27
|
-
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); Object.defineProperty(subClass, "prototype", { writable: false }); if (superClass) _setPrototypeOf(subClass, superClass); }
|
|
28
|
-
function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = _getPrototypeOf(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = _getPrototypeOf(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return _possibleConstructorReturn(this, result); }; }
|
|
29
|
-
function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } else if (call !== void 0) { throw new TypeError("Derived constructors may only return object or undefined"); } return _assertThisInitialized(self); }
|
|
30
|
-
function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }
|
|
31
|
-
function _wrapNativeSuper(Class) { var _cache = typeof Map === "function" ? new Map() : undefined; _wrapNativeSuper = function _wrapNativeSuper(Class) { if (Class === null || !_isNativeFunction(Class)) return Class; if (typeof Class !== "function") { throw new TypeError("Super expression must either be null or a function"); } if (typeof _cache !== "undefined") { if (_cache.has(Class)) return _cache.get(Class); _cache.set(Class, Wrapper); } function Wrapper() { return _construct(Class, arguments, _getPrototypeOf(this).constructor); } Wrapper.prototype = Object.create(Class.prototype, { constructor: { value: Wrapper, enumerable: false, writable: true, configurable: true } }); return _setPrototypeOf(Wrapper, Class); }; return _wrapNativeSuper(Class); }
|
|
32
|
-
function _construct(Parent, args, Class) { if (_isNativeReflectConstruct()) { _construct = Reflect.construct.bind(); } else { _construct = function _construct(Parent, args, Class) { var a = [null]; a.push.apply(a, args); var Constructor = Function.bind.apply(Parent, a); var instance = new Constructor(); if (Class) _setPrototypeOf(instance, Class.prototype); return instance; }; } return _construct.apply(null, arguments); }
|
|
33
|
-
function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); return true; } catch (e) { return false; } }
|
|
34
|
-
function _isNativeFunction(fn) { return Function.toString.call(fn).indexOf("[native code]") !== -1; }
|
|
35
|
-
function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf ? Object.setPrototypeOf.bind() : function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }
|
|
36
|
-
function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf.bind() : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }
|
|
37
|
-
var UsageError = /*#__PURE__*/function (_Error) {
|
|
38
|
-
_inherits(UsageError, _Error);
|
|
39
|
-
var _super = _createSuper(UsageError);
|
|
40
|
-
function UsageError() {
|
|
41
|
-
_classCallCheck(this, UsageError);
|
|
42
|
-
return _super.apply(this, arguments);
|
|
43
|
-
}
|
|
44
|
-
return _createClass(UsageError);
|
|
45
|
-
}( /*#__PURE__*/_wrapNativeSuper(Error));
|
|
46
|
-
/**
|
|
47
|
-
* @param {T} state
|
|
48
|
-
* @return {PartialState<T>}
|
|
49
|
-
* @template {State} T
|
|
50
|
-
*/
|
|
51
|
-
exports.UsageError = UsageError;
|
|
52
|
-
var defaultSelector = function defaultSelector(state) {
|
|
53
|
-
return state;
|
|
54
|
-
};
|
|
55
|
-
|
|
56
|
-
/**
|
|
57
|
-
* @readonly
|
|
58
|
-
* @type {Prehooks<T>}
|
|
59
|
-
* @template {State} T
|
|
60
|
-
*/
|
|
61
|
-
var defaultPrehooks = Object.freeze({});
|
|
62
|
-
|
|
63
|
-
/** @type {OptionalTask} */
|
|
64
|
-
var reportNonReactUsage = function reportNonReactUsage() {
|
|
65
|
-
throw new UsageError('Detected usage outside of this context\'s Provider component tree. Please apply the exported Provider component');
|
|
66
|
-
};
|
|
67
|
-
var _setState = function () {
|
|
68
|
-
var initDiff = function initDiff(propKey, changed, replaced) {
|
|
69
|
-
changed[propKey] = {};
|
|
70
|
-
replaced[propKey] = {};
|
|
71
|
-
};
|
|
72
|
-
var setAtomic = function setAtomic(state, newState, changed, replaced, stateKey) {
|
|
73
|
-
if ((0, _lodash4["default"])(state[stateKey], newState[stateKey])) {
|
|
74
|
-
return;
|
|
75
|
-
}
|
|
76
|
-
var isArrayNewState = Array.isArray(newState[stateKey]);
|
|
77
|
-
if (Array.isArray(state[stateKey]) && isArrayNewState) {
|
|
78
|
-
return setArray(state, newState, changed, replaced, stateKey);
|
|
79
|
-
}
|
|
80
|
-
var isPlainObjectNewState = (0, _lodash5["default"])(newState[stateKey]);
|
|
81
|
-
if ((0, _lodash5["default"])(state[stateKey]) && isPlainObjectNewState) {
|
|
82
|
-
return setPlainObject(state, newState, changed, replaced, stateKey);
|
|
83
|
-
}
|
|
84
|
-
if (stateKey in state) {
|
|
85
|
-
replaced[stateKey] = state[stateKey];
|
|
86
|
-
}
|
|
87
|
-
state[stateKey] = isArrayNewState || isPlainObjectNewState ? (0, _lodash["default"])(newState[stateKey]) : newState[stateKey];
|
|
88
|
-
changed[stateKey] = newState[stateKey];
|
|
89
|
-
};
|
|
90
|
-
var setArray = function setArray(state, newState, changed, replaced, rootKey) {
|
|
91
|
-
initDiff(rootKey, changed, replaced);
|
|
92
|
-
for (var i = 0, len = newState[rootKey].length; i < len; i++) {
|
|
93
|
-
setAtomic(state[rootKey], newState[rootKey], changed[rootKey], replaced[rootKey], i);
|
|
94
|
-
}
|
|
95
|
-
};
|
|
96
|
-
var setPlainObject = function setPlainObject(state, newState, changed, replaced, rootKey) {
|
|
97
|
-
initDiff(rootKey, changed, replaced);
|
|
98
|
-
set(state[rootKey], newState[rootKey], changed[rootKey], replaced[rootKey]);
|
|
99
|
-
};
|
|
100
|
-
var set = function set(state, newState) {
|
|
101
|
-
var changed = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
|
|
102
|
-
var replaced = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : {};
|
|
103
|
-
for (var k in newState) {
|
|
104
|
-
setAtomic(state, newState, changed, replaced, k);
|
|
105
|
-
}
|
|
106
|
-
};
|
|
107
|
-
/**
|
|
108
|
-
* @param {T} state
|
|
109
|
-
* @param {PartialState<T>} newState
|
|
110
|
-
* @param {Listener<T>} onStateChange
|
|
111
|
-
* @template {State} T
|
|
112
|
-
*/
|
|
113
|
-
return function (state, newState, onStateChange) {
|
|
114
|
-
/** @type {PartialState<T>} */
|
|
115
|
-
var newChanges = {};
|
|
116
|
-
/** @type {PartialState<T>} */
|
|
117
|
-
var replacedValue = {};
|
|
118
|
-
set(state, newState, newChanges, replacedValue);
|
|
119
|
-
!(0, _lodash3["default"])(newChanges) && onStateChange(newChanges, replacedValue);
|
|
120
|
-
};
|
|
121
|
-
}();
|
|
122
|
-
|
|
123
|
-
/**
|
|
124
|
-
* @param {Prehooks<T>} prehooks
|
|
125
|
-
* @template {State} T
|
|
126
|
-
*/
|
|
127
|
-
var usePrehooksRef = function usePrehooksRef(prehooks) {
|
|
128
|
-
var prehooksRef = (0, _react.useRef)(prehooks);
|
|
129
|
-
(0, _react.useEffect)(function () {
|
|
130
|
-
prehooksRef.current = prehooks;
|
|
131
|
-
}, [prehooks]);
|
|
132
|
-
return prehooksRef;
|
|
133
|
-
};
|
|
134
|
-
|
|
135
|
-
/**
|
|
136
|
-
* @param {Prehooks<T>} prehooks
|
|
137
|
-
* @param {T} value
|
|
138
|
-
* @returns {Store<T>}
|
|
139
|
-
* @template {State} T
|
|
140
|
-
*/
|
|
141
|
-
var useStore = function useStore(prehooks, value) {
|
|
142
|
-
var prehooksRef = usePrehooksRef(prehooks);
|
|
143
|
-
var initialState = (0, _react.useRef)(value);
|
|
144
|
-
|
|
145
|
-
/** @type {[Set<Listener<T>>, Function]} */
|
|
146
|
-
var _useState = (0, _react.useState)(function () {
|
|
147
|
-
return new Set();
|
|
148
|
-
}),
|
|
149
|
-
_useState2 = _slicedToArray(_useState, 1),
|
|
150
|
-
listeners = _useState2[0];
|
|
151
|
-
/** @type {[T, Function]} */
|
|
152
|
-
var _useState3 = (0, _react.useState)(function () {
|
|
153
|
-
return (0, _lodash["default"])(value);
|
|
154
|
-
}),
|
|
155
|
-
_useState4 = _slicedToArray(_useState3, 1),
|
|
156
|
-
state = _useState4[0];
|
|
157
|
-
|
|
158
|
-
/** @type {Listener<T>} */
|
|
159
|
-
var onChange = function onChange(newValue, oldValue) {
|
|
160
|
-
return listeners.forEach(function (listener) {
|
|
161
|
-
return listener(newValue, oldValue);
|
|
162
|
-
});
|
|
163
|
-
};
|
|
164
|
-
|
|
165
|
-
/** @type {Store<T>["getState"]} */
|
|
166
|
-
var getState = (0, _react.useCallback)(function () {
|
|
167
|
-
var selector = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : defaultSelector;
|
|
168
|
-
var slice = selector(state);
|
|
169
|
-
return _typeof(slice) === 'object' ? (0, _lodash["default"])(slice) : slice;
|
|
170
|
-
}, []);
|
|
171
|
-
|
|
172
|
-
/** @type {Store<T>["resetState"]} */
|
|
173
|
-
var resetState = (0, _react.useCallback)(function () {
|
|
174
|
-
var original = (0, _lodash["default"])(initialState.current);
|
|
175
|
-
(!('resetState' in prehooksRef.current) || prehooksRef.current.resetState({
|
|
176
|
-
current: (0, _lodash["default"])(state),
|
|
177
|
-
original: original
|
|
178
|
-
})) && _setState(state, original, onChange);
|
|
179
|
-
}, []);
|
|
180
|
-
|
|
181
|
-
/** @type {Store<T>["setState"]} */
|
|
182
|
-
var setState = (0, _react.useCallback)(function (changes) {
|
|
183
|
-
changes = (0, _lodash["default"])(changes);
|
|
184
|
-
(!('setState' in prehooksRef.current) || prehooksRef.current.setState(changes)) && _setState(state, changes, onChange);
|
|
185
|
-
}, []);
|
|
186
|
-
|
|
187
|
-
/** @type {Store<T>["subscribe"]} */
|
|
188
|
-
var subscribe = (0, _react.useCallback)(function (listener) {
|
|
189
|
-
listeners.add(listener);
|
|
190
|
-
return function () {
|
|
191
|
-
return listeners["delete"](listener);
|
|
192
|
-
};
|
|
193
|
-
}, []);
|
|
194
|
-
(0, _react.useEffect)(function () {
|
|
195
|
-
return setState((0, _lodash["default"])(value));
|
|
196
|
-
}, [value]);
|
|
197
|
-
|
|
198
|
-
/** @type {[Store<T>, Function]} */
|
|
199
|
-
var _useState5 = (0, _react.useState)(function () {
|
|
200
|
-
return {
|
|
201
|
-
getState: getState,
|
|
202
|
-
resetState: resetState,
|
|
203
|
-
setState: setState,
|
|
204
|
-
subscribe: subscribe
|
|
205
|
-
};
|
|
206
|
-
}),
|
|
207
|
-
_useState6 = _slicedToArray(_useState5, 1),
|
|
208
|
-
store = _useState6[0];
|
|
209
|
-
return store;
|
|
210
|
-
};
|
|
211
|
-
|
|
212
|
-
/** @type {FC<{child: ReactNode}>} */
|
|
213
|
-
var ChildMemo = function () {
|
|
214
|
-
var useNodeMemo = function useNodeMemo(node) {
|
|
215
|
-
var nodeRef = (0, _react.useRef)(node);
|
|
216
|
-
if (!(0, _lodash4["default"])((0, _lodash6["default"])(nodeRef.current, '_owner'), (0, _lodash6["default"])(node, '_owner'))) {
|
|
217
|
-
nodeRef.current = node;
|
|
218
|
-
}
|
|
219
|
-
return nodeRef.current;
|
|
220
|
-
};
|
|
221
|
-
var ChildMemo = /*#__PURE__*/(0, _react.memo)(function (_ref) {
|
|
222
|
-
var child = _ref.child;
|
|
223
|
-
return child;
|
|
224
|
-
});
|
|
225
|
-
ChildMemo.displayName = 'ObservableContext.Provider.Internal.Guardian.ChildMemo';
|
|
226
|
-
var Guardian = function Guardian(_ref2) {
|
|
227
|
-
var child = _ref2.child;
|
|
228
|
-
return /*#__PURE__*/_react["default"].createElement(ChildMemo, {
|
|
229
|
-
child: useNodeMemo(child)
|
|
230
|
-
});
|
|
231
|
-
};
|
|
232
|
-
Guardian.displayName = 'ObservableContext.Provider.Internal.Guardian';
|
|
233
|
-
return Guardian;
|
|
234
|
-
}();
|
|
235
|
-
|
|
236
|
-
/** @type {(children: ReactNode) => ReactNode} */
|
|
237
|
-
var memoizeImmediateChildTree = function memoizeImmediateChildTree(children) {
|
|
238
|
-
return _react.Children.map(children, function (child) {
|
|
239
|
-
var _child$props;
|
|
240
|
-
if (_typeof(child.type) === 'object' && 'compare' in child.type) {
|
|
241
|
-
return child;
|
|
242
|
-
} // memo element
|
|
243
|
-
if ((_child$props = child.props) !== null && _child$props !== void 0 && _child$props.children) {
|
|
244
|
-
child = /*#__PURE__*/(0, _react.cloneElement)(child, (0, _lodash6["default"])(child.props, 'children'), memoizeImmediateChildTree(child.props.children));
|
|
245
|
-
}
|
|
246
|
-
return /*#__PURE__*/_react["default"].createElement(ChildMemo, {
|
|
247
|
-
child: child
|
|
248
|
-
});
|
|
249
|
-
});
|
|
250
|
-
};
|
|
251
|
-
|
|
252
|
-
/** @param {Provider<Store<T>>} Provider */
|
|
253
|
-
var makeObservable = function makeObservable(Provider) {
|
|
254
|
-
/**
|
|
255
|
-
* @type {FC<{
|
|
256
|
-
* children?: ReactNode,
|
|
257
|
-
* prehooks?: Prehooks<T>
|
|
258
|
-
* value: PartialState<T>
|
|
259
|
-
* }>}
|
|
260
|
-
* @template {State} T
|
|
261
|
-
*/
|
|
262
|
-
var Observable = function Observable(_ref3) {
|
|
263
|
-
var _ref3$children = _ref3.children,
|
|
264
|
-
children = _ref3$children === void 0 ? null : _ref3$children,
|
|
265
|
-
_ref3$prehooks = _ref3.prehooks,
|
|
266
|
-
prehooks = _ref3$prehooks === void 0 ? defaultPrehooks : _ref3$prehooks,
|
|
267
|
-
value = _ref3.value;
|
|
268
|
-
return /*#__PURE__*/_react["default"].createElement(Provider, {
|
|
269
|
-
value: useStore(prehooks, value)
|
|
270
|
-
}, memoizeImmediateChildTree(children));
|
|
271
|
-
};
|
|
272
|
-
Observable.displayName = 'ObservableContext.Provider';
|
|
273
|
-
return Observable;
|
|
274
|
-
};
|
|
275
|
-
|
|
276
|
-
/**
|
|
277
|
-
* @returns {ObservableContext<T>}
|
|
278
|
-
* @template {State} T
|
|
279
|
-
*/
|
|
280
|
-
var createContext = function createContext() {
|
|
281
|
-
var Context = /*#__PURE__*/(0, _react.createContext)({
|
|
282
|
-
getState: reportNonReactUsage,
|
|
283
|
-
resetState: reportNonReactUsage,
|
|
284
|
-
setState: reportNonReactUsage,
|
|
285
|
-
subscribe: reportNonReactUsage
|
|
286
|
-
});
|
|
287
|
-
var provider = Context.Provider;
|
|
288
|
-
Context.Provider = makeObservable(provider);
|
|
289
|
-
return Context;
|
|
290
|
-
};
|
|
291
|
-
|
|
292
|
-
/**
|
|
293
|
-
* Actively monitors the store and triggers component re-render if any of the watched keys in the state objects changes
|
|
294
|
-
*
|
|
295
|
-
* @param {ObservableContext<T>} context
|
|
296
|
-
* @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.
|
|
297
|
-
* @returns {Store<T>}
|
|
298
|
-
* @template {State} T
|
|
299
|
-
*/
|
|
300
|
-
exports.createContext = createContext;
|
|
301
|
-
var useContext = function useContext(context) {
|
|
302
|
-
var watchedKeys = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : [];
|
|
303
|
-
/** @type {Store<T>} */
|
|
304
|
-
var store = (0, _react.useContext)(context);
|
|
305
|
-
var _useState7 = (0, _react.useState)(false),
|
|
306
|
-
_useState8 = _slicedToArray(_useState7, 2),
|
|
307
|
-
tripRender = _useState8[1];
|
|
308
|
-
var watched = (0, _react.useMemo)(function () {
|
|
309
|
-
return Array.isArray(watchedKeys) ? Array.from(new Set(watchedKeys)) : [];
|
|
310
|
-
}, [watchedKeys]);
|
|
311
|
-
(0, _react.useEffect)(function () {
|
|
312
|
-
if (!watched.length) {
|
|
313
|
-
return;
|
|
314
|
-
}
|
|
315
|
-
return store.subscribe(function (newChanges) {
|
|
316
|
-
watched.some(function (w) {
|
|
317
|
-
return (0, _lodash2["default"])(newChanges, w);
|
|
318
|
-
}) && tripRender(function (s) {
|
|
319
|
-
return !s;
|
|
320
|
-
});
|
|
321
|
-
});
|
|
322
|
-
}, [watched]);
|
|
323
|
-
return store;
|
|
324
|
-
};
|
|
325
|
-
|
|
326
|
-
/**
|
|
327
|
-
* @typedef {Context<Store<T>>} ObservableContext
|
|
328
|
-
* @template {State} T
|
|
329
|
-
*/
|
|
330
|
-
|
|
331
|
-
/**
|
|
332
|
-
* @typedef {F extends void ? () => never : F} OptionalTask
|
|
333
|
-
* @template [F=void]
|
|
334
|
-
*/
|
|
335
|
-
|
|
336
|
-
/**
|
|
337
|
-
* @typedef {(newValue: PartialState<T>, oldValue: PartialState<T>) => void} Listener
|
|
338
|
-
* @template {State} T
|
|
339
|
-
*/
|
|
340
|
-
|
|
341
|
-
/** @typedef {{[x:string]: *}} State */
|
|
342
|
-
|
|
343
|
-
/**
|
|
344
|
-
* @typedef {{[x:string]: *} & {[K in keyof T]?: T[K]}} PartialState
|
|
345
|
-
* @template {State} T
|
|
346
|
-
*/
|
|
347
|
-
|
|
348
|
-
/**
|
|
349
|
-
* @typedef {(state: T) => *} Selector
|
|
350
|
-
* @template {State} T
|
|
351
|
-
*/
|
|
352
|
-
|
|
353
|
-
/**
|
|
354
|
-
* @typedef {{
|
|
355
|
-
* resetState?: (state: { current: T, original: T}) => boolean,
|
|
356
|
-
* setState?: (newChanges: PartialState<T>) => boolean
|
|
357
|
-
* }} Prehooks
|
|
358
|
-
* @template {State} T
|
|
359
|
-
*/
|
|
360
|
-
|
|
361
|
-
/**
|
|
362
|
-
* @typedef {{
|
|
363
|
-
* getState: OptionalTask<(selector?: Selector<T>) => *>,
|
|
364
|
-
* resetState: OptionalTask<VoidFunction>,
|
|
365
|
-
* setState: OptionalTask<(changes: PartialState<T>) => void>,
|
|
366
|
-
* subscribe: OptionalTask<(listener: Listener<T>) => Unsubscribe>
|
|
367
|
-
* }} Store
|
|
368
|
-
* @template {State} T
|
|
369
|
-
*/
|
|
370
|
-
|
|
371
|
-
/** @typedef {VoidFunction} Unsubscribe */
|
|
372
|
-
|
|
373
|
-
/** @typedef {import("react").ReactNode} ReactNode */
|
|
374
|
-
|
|
375
|
-
/**
|
|
376
|
-
* @typedef {import("react").FC<P>} FC
|
|
377
|
-
* @template [P={}]
|
|
378
|
-
*/
|
|
379
|
-
|
|
380
|
-
/**
|
|
381
|
-
* @typedef {import("react").Provider<T>} Provider
|
|
382
|
-
* @template T
|
|
383
|
-
*/
|
|
384
|
-
|
|
385
|
-
/**
|
|
386
|
-
* @typedef {import("react").Context<T>} Context
|
|
387
|
-
* @template T
|
|
388
|
-
*/
|
|
389
|
-
exports.useContext = useContext;
|