@webkrafters/react-observable-context 1.1.2 → 1.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.
- package/README.md +8 -6
- package/dist/index.js +29 -18
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,12 +1,14 @@
|
|
|
1
1
|
# React-Observable-Context
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
A context bearing an observable consumer store. State changes within the store's internal state are only broadcasted to components subscribed to the store. In this way, the `React-Observable-Context` prevents repeated automatic re-renderings of entire component trees resulting from ***context*** state changes.
|
|
4
|
+
|
|
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.
|
|
4
6
|
|
|
5
7
|
<h4><u>Install</u></h4>
|
|
6
8
|
|
|
7
|
-
npm i -S @webkrafters/react-
|
|
9
|
+
npm i -S @webkrafters/react-observable-context
|
|
8
10
|
|
|
9
|
-
npm install --save @webkrafters/react-
|
|
11
|
+
npm install --save @webkrafters/react-observable-context
|
|
10
12
|
|
|
11
13
|
## API
|
|
12
14
|
|
|
@@ -16,13 +18,13 @@ The React-Observable-Context package exports only **2** modules namely: the **cr
|
|
|
16
18
|
|
|
17
19
|
The `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>.
|
|
18
20
|
|
|
19
|
-
<i><u>Note:</u></i> the Provider `context` prop is not updateable. Once set, all further updates to this prop are
|
|
21
|
+
<i><u>Note:</u></i> the Provider `context` prop is not updateable. Once set, all further updates to this prop are not recorded.
|
|
20
22
|
|
|
21
23
|
The context's `store` exposes **4** methods for interacting with the context's internal state namely:
|
|
22
24
|
|
|
23
25
|
* **getState**: (selector?: (state: State) => any) => any
|
|
24
26
|
|
|
25
|
-
* **resetState**: VoidFunction // resets the state to the Provider
|
|
27
|
+
* **resetState**: VoidFunction // resets the state to the Provider initial `value` prop.
|
|
26
28
|
|
|
27
29
|
* **setState**: (changes: PartialState\<State\>) => void
|
|
28
30
|
|
|
@@ -36,7 +38,7 @@ The context's store update operation adheres to **2** user supplied prehooks whe
|
|
|
36
38
|
|
|
37
39
|
* **setState**: (newChanges: PartialState\<State\>) => boolean
|
|
38
40
|
|
|
39
|
-
|
|
41
|
+
***usecase***: prehooks provide a central place for sanitizing, modifying, transforming, validating etc. all related incoming state updates. The prehook returns a **boolean** value (`true` to continue OR `false` to abort the update operation). The prehook may mutate (i.e. sanitize, transform, transpose) its argument values to accurately reflect the intended update value.
|
|
40
42
|
|
|
41
43
|
## Usage
|
|
42
44
|
|
package/dist/index.js
CHANGED
|
@@ -46,7 +46,8 @@ var reportNonReactUsage = function reportNonReactUsage() {
|
|
|
46
46
|
};
|
|
47
47
|
|
|
48
48
|
/**
|
|
49
|
-
* @
|
|
49
|
+
* @param {T} state
|
|
50
|
+
* @return {PartialState<T>}
|
|
50
51
|
* @template {State} T
|
|
51
52
|
*/
|
|
52
53
|
var defaultSelector = function defaultSelector(state) {
|
|
@@ -96,6 +97,18 @@ var _setState = function _setState(state, newState, onStateChange) {
|
|
|
96
97
|
!(0, _lodash2["default"])(newChanges) && onStateChange(newChanges, replacedValue);
|
|
97
98
|
};
|
|
98
99
|
|
|
100
|
+
/**
|
|
101
|
+
* @param {Prehooks<T>} prehooks
|
|
102
|
+
* @template {State} T
|
|
103
|
+
*/
|
|
104
|
+
var usePrehooksRef = function usePrehooksRef(prehooks) {
|
|
105
|
+
var prehooksRef = (0, _react.useRef)(prehooks);
|
|
106
|
+
(0, _react.useEffect)(function () {
|
|
107
|
+
prehooksRef.current = prehooks;
|
|
108
|
+
}, [prehooks]);
|
|
109
|
+
return prehooksRef;
|
|
110
|
+
};
|
|
111
|
+
|
|
99
112
|
/**
|
|
100
113
|
* Note: `context` prop is not updateable. Furtther updates to this prop are ignored.
|
|
101
114
|
*
|
|
@@ -114,21 +127,25 @@ var Provider = function Provider(_ref) {
|
|
|
114
127
|
_ref$prehooks = _ref.prehooks,
|
|
115
128
|
prehooks = _ref$prehooks === void 0 ? defaultPrehooks : _ref$prehooks,
|
|
116
129
|
value = _ref.value;
|
|
117
|
-
var prehooksRef = (
|
|
130
|
+
var prehooksRef = usePrehooksRef(prehooks);
|
|
118
131
|
var initialState = (0, _react.useRef)(value);
|
|
119
132
|
|
|
120
|
-
/** @type {[T
|
|
133
|
+
/** @type {[Set<Listener<T>>, Function]} */
|
|
121
134
|
var _useState = (0, _react.useState)(function () {
|
|
122
|
-
return
|
|
135
|
+
return new Set();
|
|
123
136
|
}),
|
|
124
137
|
_useState2 = _slicedToArray(_useState, 1),
|
|
125
|
-
|
|
126
|
-
/** @type {[
|
|
138
|
+
listeners = _useState2[0];
|
|
139
|
+
/** @type {[T, Function]} */
|
|
127
140
|
var _useState3 = (0, _react.useState)(function () {
|
|
128
|
-
return
|
|
141
|
+
return (0, _lodash["default"])(value);
|
|
129
142
|
}),
|
|
130
143
|
_useState4 = _slicedToArray(_useState3, 1),
|
|
131
|
-
|
|
144
|
+
state = _useState4[0];
|
|
145
|
+
/** @type {ObservableContext<T>} */
|
|
146
|
+
var _useState5 = (0, _react.useState)(context),
|
|
147
|
+
_useState6 = _slicedToArray(_useState5, 1),
|
|
148
|
+
StoreContext = _useState6[0];
|
|
132
149
|
|
|
133
150
|
/** @type {Listener<T>} */
|
|
134
151
|
var onChange = function onChange(newValue, oldValue) {
|
|
@@ -154,6 +171,7 @@ var Provider = function Provider(_ref) {
|
|
|
154
171
|
|
|
155
172
|
/** @type {Store<T>["setState"]} */
|
|
156
173
|
var setState = (0, _react.useCallback)(function (changes) {
|
|
174
|
+
changes = (0, _lodash["default"])(changes);
|
|
157
175
|
(!('setState' in prehooksRef.current) || prehooksRef.current.setState(changes)) && _setState(state, changes, onChange);
|
|
158
176
|
}, []);
|
|
159
177
|
|
|
@@ -167,12 +185,9 @@ var Provider = function Provider(_ref) {
|
|
|
167
185
|
(0, _react.useEffect)(function () {
|
|
168
186
|
return setState((0, _lodash["default"])(value));
|
|
169
187
|
}, [value]);
|
|
170
|
-
(0, _react.useEffect)(function () {
|
|
171
|
-
prehooksRef.current = prehooks;
|
|
172
|
-
}, [prehooks]);
|
|
173
|
-
/** @type {[Store<T>, Function]} */
|
|
174
188
|
|
|
175
|
-
|
|
189
|
+
/** @type {[Store<T>, Function]} */
|
|
190
|
+
var _useState7 = (0, _react.useState)(function () {
|
|
176
191
|
return {
|
|
177
192
|
getState: getState,
|
|
178
193
|
resetState: resetState,
|
|
@@ -180,12 +195,8 @@ var Provider = function Provider(_ref) {
|
|
|
180
195
|
subscribe: subscribe
|
|
181
196
|
};
|
|
182
197
|
}),
|
|
183
|
-
_useState6 = _slicedToArray(_useState5, 1),
|
|
184
|
-
store = _useState6[0];
|
|
185
|
-
/** @type {ObservableContext<T>} */
|
|
186
|
-
var _useState7 = (0, _react.useState)(context),
|
|
187
198
|
_useState8 = _slicedToArray(_useState7, 1),
|
|
188
|
-
|
|
199
|
+
store = _useState8[0];
|
|
189
200
|
return /*#__PURE__*/_react["default"].createElement(StoreContext.Provider, {
|
|
190
201
|
value: store
|
|
191
202
|
}, children);
|
package/package.json
CHANGED