@webkrafters/react-observable-context 2.1.2 → 2.1.4-rc.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 +287 -267
- package/dist/index.js +58 -25
- package/package.json +2 -1
package/README.md
CHANGED
|
@@ -1,267 +1,287 @@
|
|
|
1
|
-
# React-Observable-Context
|
|
2
|
-
|
|
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.
|
|
6
|
-
|
|
7
|
-
_**Recommendation:**_
|
|
8
|
-
|
|
9
|
-
<
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
npm
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
* **
|
|
22
|
-
|
|
23
|
-
* **
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
The
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
* **
|
|
34
|
-
|
|
35
|
-
* **
|
|
36
|
-
|
|
37
|
-
* **
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
* **
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
<
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
const
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
<
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
1
|
+
# React-Observable-Context
|
|
2
|
+
|
|
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.
|
|
6
|
+
|
|
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. ***Exempt:*** certain components such as those wrapped in the `React-Redux::connect()` HOC. Such HOC provide similar protection to wrapped components and their descendants.
|
|
8
|
+
|
|
9
|
+
<hr />
|
|
10
|
+
|
|
11
|
+
<h4><u>Install</u></h4>
|
|
12
|
+
|
|
13
|
+
npm i -S @webkrafters/react-observable-context
|
|
14
|
+
|
|
15
|
+
npm install --save @webkrafters/react-observable-context
|
|
16
|
+
|
|
17
|
+
## API
|
|
18
|
+
|
|
19
|
+
The React-Observable-Context package exports **3** modules namely: the **createContext** method, the **useContext** hook and the **Provider** component.
|
|
20
|
+
|
|
21
|
+
* **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
|
+
|
|
23
|
+
* **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.
|
|
24
|
+
|
|
25
|
+
* **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
|
+
|
|
27
|
+
***<u>Note:</u>*** the Provider `context` prop is not updateable. Once set, all further updates to this prop are not recorded.
|
|
28
|
+
|
|
29
|
+
### The Store
|
|
30
|
+
|
|
31
|
+
The context's `store` exposes **4** methods for interacting with the context's internal state namely:
|
|
32
|
+
|
|
33
|
+
* **getState**: (selector?: (state: State) => any) => any
|
|
34
|
+
|
|
35
|
+
* **resetState**: VoidFunction // resets the state to the Provider initial `value` prop.
|
|
36
|
+
|
|
37
|
+
* **setState**: (changes: PartialState\<State\>) => void // sets only new/changed top level properties.
|
|
38
|
+
|
|
39
|
+
* **subscribe**: (listener: (newValue: PartialState\<State\>, oldValue: PartialState\<State\>) => void) => ***UnsubscribeFunction***
|
|
40
|
+
|
|
41
|
+
### Prehooks
|
|
42
|
+
|
|
43
|
+
The context's store update operation adheres to **2** user supplied prehooks when present. Otherwise, the update operation proceeds normally to completion. They are named **resetState** and **setState** after the store update methods which utilize them.
|
|
44
|
+
|
|
45
|
+
* **resetState**: (state: {current: State, original: State}) => boolean
|
|
46
|
+
|
|
47
|
+
* **setState**: (newChanges: PartialState\<State\>) => boolean
|
|
48
|
+
|
|
49
|
+
***<u>Use case:</u>*** 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 AND `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.
|
|
50
|
+
|
|
51
|
+
## Usage
|
|
52
|
+
|
|
53
|
+
<i><u>context.js</u></i>
|
|
54
|
+
|
|
55
|
+
import React from 'react';
|
|
56
|
+
|
|
57
|
+
import {
|
|
58
|
+
createContext,
|
|
59
|
+
Provider,
|
|
60
|
+
useContext
|
|
61
|
+
} from '@webkrafters/react-observable-context';
|
|
62
|
+
|
|
63
|
+
export const ObservableContext = createContext();
|
|
64
|
+
|
|
65
|
+
export const ObservableContextProvider = ({ children, prehooks, value }) => (
|
|
66
|
+
<Provider
|
|
67
|
+
context={ ObservableContext }
|
|
68
|
+
prehooks={ prehooks }
|
|
69
|
+
value={ value }
|
|
70
|
+
>
|
|
71
|
+
{ children }
|
|
72
|
+
</Provider>
|
|
73
|
+
);
|
|
74
|
+
ObservableContextProvider.displayName = 'ObservableContextProvider';
|
|
75
|
+
|
|
76
|
+
export const useObservableContext = watchedKeys => useContext( ObservableContext, watchedKeys );
|
|
77
|
+
|
|
78
|
+
export default ObservableContext;
|
|
79
|
+
|
|
80
|
+
<i><u>index.js</i></b>
|
|
81
|
+
|
|
82
|
+
import React from 'react';
|
|
83
|
+
import ReactDOM from 'react-dom';
|
|
84
|
+
import App from './app';
|
|
85
|
+
ReactDOM.render(<App />, document.getElementById('root'));
|
|
86
|
+
|
|
87
|
+
<i><u>app.js</u></i>
|
|
88
|
+
|
|
89
|
+
import React, { useCallback, useState } from 'react';
|
|
90
|
+
import Product from './product';
|
|
91
|
+
const productUpdateHooks = {
|
|
92
|
+
resetState: ( ...args ) => {
|
|
93
|
+
console.log( 'resetting state with >>>> ', JSON.stringify( args ) );
|
|
94
|
+
return true;
|
|
95
|
+
},
|
|
96
|
+
setState: ( ...args ) => {
|
|
97
|
+
console.log( 'setting state with >>>> ', JSON.stringify( args ) );
|
|
98
|
+
return true;
|
|
99
|
+
}
|
|
100
|
+
};
|
|
101
|
+
const App = () => {
|
|
102
|
+
const [ productType, setProductType ] = useState( 'Calculator' );
|
|
103
|
+
const updateType = useCallback( e => setProductType( e.target.value ), [] );
|
|
104
|
+
return (
|
|
105
|
+
<div className="App">
|
|
106
|
+
<h1>Demo</h1>
|
|
107
|
+
<h2>A contrived product app.</h2>
|
|
108
|
+
<div style={{ marginBottom: 10 }}>
|
|
109
|
+
<label>Type: <input onKeyUp={ updateType } placeholder="override product type here..." /></label>
|
|
110
|
+
</div>
|
|
111
|
+
<Product
|
|
112
|
+
type={ productType }
|
|
113
|
+
updateHooks={ productUpdateHooks }
|
|
114
|
+
/>
|
|
115
|
+
</div>
|
|
116
|
+
);
|
|
117
|
+
};
|
|
118
|
+
App.displayName = 'App';
|
|
119
|
+
export default App;
|
|
120
|
+
|
|
121
|
+
<i><u>product.js</u></i>
|
|
122
|
+
|
|
123
|
+
import React, { useCallback, useEffect, useState } from 'react';
|
|
124
|
+
import { ObservableContextProvider } from './context';
|
|
125
|
+
import Editor from './editor';
|
|
126
|
+
import PriceSticker from './price-sticker';
|
|
127
|
+
import ProductDescription from './product-description';
|
|
128
|
+
import TallyDisplay from './tally-display';
|
|
129
|
+
const Product = ({ type, updateHooks }) => {
|
|
130
|
+
const [ state, setState ] = useState(() => ({
|
|
131
|
+
color: 'Burgundy',
|
|
132
|
+
price: 22.5,
|
|
133
|
+
type
|
|
134
|
+
}));
|
|
135
|
+
useEffect(() => {
|
|
136
|
+
setState({ type }); // use this to update only the changed state value
|
|
137
|
+
// setState({ ...state, type }); // this will override the context internal state for these values
|
|
138
|
+
}, [ type ]);
|
|
139
|
+
const overridePricing = useCallback( e => setState({ price: Number( e.target.value ) }), [] );
|
|
140
|
+
return (
|
|
141
|
+
<div>
|
|
142
|
+
<div style={{ marginBottom: 10 }}>
|
|
143
|
+
<label>$ <input onKeyUp={ overridePricing } placeholder="override price here..." /></label>
|
|
144
|
+
</div>
|
|
145
|
+
<ObservableContextProvider
|
|
146
|
+
prehooks={ updateHooks }
|
|
147
|
+
value={ state }
|
|
148
|
+
>
|
|
149
|
+
<div style={{
|
|
150
|
+
borderBottom: '1px solid #333',
|
|
151
|
+
marginBottom: 10,
|
|
152
|
+
paddingBottom: 5
|
|
153
|
+
}}>
|
|
154
|
+
<Editor />
|
|
155
|
+
<TallyDisplay />
|
|
156
|
+
</div>
|
|
157
|
+
<ProductDescription />
|
|
158
|
+
<PriceSticker />
|
|
159
|
+
</ObservableContextProvider>
|
|
160
|
+
</div>
|
|
161
|
+
);
|
|
162
|
+
};
|
|
163
|
+
Product.displayName = 'Product';
|
|
164
|
+
export default Product;
|
|
165
|
+
|
|
166
|
+
<i><u>editor.js</u></i>
|
|
167
|
+
|
|
168
|
+
import React, { memo, useCallback, useEffect, useRef } from 'react';
|
|
169
|
+
import { useObservableContext } from './context';
|
|
170
|
+
const Editor = memo(() => {
|
|
171
|
+
const { setState } = useObservableContext();
|
|
172
|
+
const priceInputRef = useRef();
|
|
173
|
+
const colorInputRef = useRef();
|
|
174
|
+
const typeInputRef = useRef();
|
|
175
|
+
const updatePrice = useCallback(() => {
|
|
176
|
+
setState({ price: Number( priceInputRef.current.value ) });
|
|
177
|
+
}, []);
|
|
178
|
+
const updateColor = useCallback(() => {
|
|
179
|
+
setState({ color: colorInputRef.current.value });
|
|
180
|
+
}, []);
|
|
181
|
+
const updateType = useCallback(() => {
|
|
182
|
+
setState({ type: typeInputRef.current.value });
|
|
183
|
+
}, []);
|
|
184
|
+
useEffect(() => console.log( 'Editor component rendered.....' ));
|
|
185
|
+
return (
|
|
186
|
+
<fieldset style={{ margin: '10px 0' }}>
|
|
187
|
+
<legend>Editor</legend>
|
|
188
|
+
<div style={{ margin: '10px 0' }}>
|
|
189
|
+
<label>New Price: <input ref={ priceInputRef } /></label>
|
|
190
|
+
{ ' ' }
|
|
191
|
+
<button onClick={ updatePrice }>update price</button>
|
|
192
|
+
</div>
|
|
193
|
+
<div style={{ margin: '10px 0' }}>
|
|
194
|
+
<label>New Color: <input ref={ colorInputRef } /></label>
|
|
195
|
+
{ ' ' }
|
|
196
|
+
<button onClick={ updateColor }>update color</button>
|
|
197
|
+
</div>
|
|
198
|
+
<div style={{ margin: '10px 0' }}>
|
|
199
|
+
<label>New Type: <input ref={ typeInputRef } /></label>
|
|
200
|
+
{ ' ' }
|
|
201
|
+
<button onClick={ updateType }>update type</button>
|
|
202
|
+
</div>
|
|
203
|
+
</fieldset>
|
|
204
|
+
);
|
|
205
|
+
});
|
|
206
|
+
Editor.displayName = 'Editor';
|
|
207
|
+
export default Editor;
|
|
208
|
+
|
|
209
|
+
<i><u>price-sticker.js</u></i>
|
|
210
|
+
|
|
211
|
+
import React, { memo, useEffect } from 'react';
|
|
212
|
+
import { useObservableContext } from './context';
|
|
213
|
+
const CONTEXT_KEYS = [ 'price' ];
|
|
214
|
+
const PriceSticker = memo(() => {
|
|
215
|
+
const { getState } = useObservableContext( CONTEXT_KEYS );
|
|
216
|
+
useEffect(() => console.log( 'PriceSticker component rendered.....' ));
|
|
217
|
+
return (
|
|
218
|
+
<div style={{ fontSize: 36, fontWeight: 800 }}>
|
|
219
|
+
${ getState( s => s.price ).toFixed( 2 ) }
|
|
220
|
+
</div>
|
|
221
|
+
);
|
|
222
|
+
});
|
|
223
|
+
PriceSticker.displayName = 'PriceSticker';
|
|
224
|
+
export default PriceSticker;
|
|
225
|
+
|
|
226
|
+
<i><u>product-description.js</u></i>
|
|
227
|
+
|
|
228
|
+
import React, { memo, useEffect } from 'react';
|
|
229
|
+
import { useObservableContext } from './context';
|
|
230
|
+
const CONTEXT_KEYS = [ 'color', 'type' ];
|
|
231
|
+
const ProductDescription = memo(() => {
|
|
232
|
+
const store = useObservableContext( CONTEXT_KEYS );
|
|
233
|
+
useEffect(() => console.log( 'ProductDescription component rendered.....' ));
|
|
234
|
+
const color = store.getState( s => s.color );
|
|
235
|
+
const type = store.getState( s => s.type );
|
|
236
|
+
return (
|
|
237
|
+
<div style={{ fontSize: 24 }}>
|
|
238
|
+
<strong>Description:</strong> { color } { type }
|
|
239
|
+
</div>
|
|
240
|
+
);
|
|
241
|
+
});
|
|
242
|
+
ProductDescription.displayName = 'ProductDescription';
|
|
243
|
+
export default ProductDescription;
|
|
244
|
+
|
|
245
|
+
<i><u>tally-display.js</u></i>
|
|
246
|
+
|
|
247
|
+
import React, { memo, useEffect } from 'react';
|
|
248
|
+
import { useObservableContext } from './context';
|
|
249
|
+
import Reset from './reset';
|
|
250
|
+
const CONTEXT_KEYS = [ 'color', 'price', 'type' ];
|
|
251
|
+
const TallyDisplay = memo(() => {
|
|
252
|
+
const { getState } = useObservableContext( CONTEXT_KEYS );
|
|
253
|
+
useEffect(() => console.log( 'TallyDisplay component rendered.....' ));
|
|
254
|
+
return (
|
|
255
|
+
<div>
|
|
256
|
+
<table>
|
|
257
|
+
<tbody>
|
|
258
|
+
<tr><td><label>Type:</label></td><td>{ getState( s => s.type ) }</td></tr>
|
|
259
|
+
<tr><td><label>Color:</label></td><td>{ getState( s => s.color ) }</td></tr>
|
|
260
|
+
<tr><td><label>Price:</label></td><td>{ getState( s => s.price ).toFixed( 2 ) }</td></tr>
|
|
261
|
+
</tbody>
|
|
262
|
+
</table>
|
|
263
|
+
<div style={{ textAlign: 'right' }}>
|
|
264
|
+
<Reset />
|
|
265
|
+
</div>
|
|
266
|
+
</div>
|
|
267
|
+
);
|
|
268
|
+
});
|
|
269
|
+
TallyDisplay.displayName = 'TallyDisplay';
|
|
270
|
+
export default TallyDisplay;
|
|
271
|
+
|
|
272
|
+
<i><u>reset.js</u></i>
|
|
273
|
+
|
|
274
|
+
import React, { memo, useEffect } from 'react';
|
|
275
|
+
import { useObservableContext } from './context';
|
|
276
|
+
const Reset = memo(() => {
|
|
277
|
+
const { resetState } = useObservableContext();
|
|
278
|
+
useEffect(() => console.log( 'Reset component rendered.....' ));
|
|
279
|
+
return ( <button onClick={ resetState }>reset context</button> );
|
|
280
|
+
});
|
|
281
|
+
Reset.displayName = 'Reset';
|
|
282
|
+
export default Reset;
|
|
283
|
+
|
|
284
|
+
|
|
285
|
+
## License
|
|
286
|
+
|
|
287
|
+
MIT
|
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.
|
|
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
|
-
|
|
117
|
-
|
|
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
|
-
|
|
131
|
-
state[
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
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,
|
|
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,
|
|
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
|
-
|
|
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.
|
|
82
|
+
"version": "2.1.4-rc.0"
|
|
82
83
|
}
|