@webkrafters/react-observable-context 1.1.3 → 1.1.5
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 +324 -322
- package/dist/index.d.ts +9 -9
- package/dist/index.js +33 -22
- package/package.json +79 -79
package/README.md
CHANGED
|
@@ -1,322 +1,324 @@
|
|
|
1
|
-
# React-Observable-Context
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
npm
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
* **
|
|
26
|
-
|
|
27
|
-
* **
|
|
28
|
-
|
|
29
|
-
* **
|
|
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
|
-
import
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
const
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
<tr><td><label>
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
const
|
|
118
|
-
|
|
119
|
-
const
|
|
120
|
-
|
|
121
|
-
const
|
|
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
|
-
const
|
|
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
|
-
const
|
|
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
|
-
import
|
|
230
|
-
import
|
|
231
|
-
|
|
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
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
import
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
const
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
<
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
import
|
|
318
|
-
ReactDOM
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
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
|
+
<h4><u>Install</u></h4>
|
|
8
|
+
|
|
9
|
+
npm i -S @webkrafters/react-observable-context
|
|
10
|
+
|
|
11
|
+
npm install --save @webkrafters/react-observable-context
|
|
12
|
+
|
|
13
|
+
## API
|
|
14
|
+
|
|
15
|
+
The React-Observable-Context package exports only **2** modules namely: the **createContext** method and the **Provider** component.
|
|
16
|
+
|
|
17
|
+
`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`.
|
|
18
|
+
|
|
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>.
|
|
20
|
+
|
|
21
|
+
<i><u>Note:</u></i> the Provider `context` prop is not updateable. Once set, all further updates to this prop are not recorded.
|
|
22
|
+
|
|
23
|
+
The context's `store` exposes **4** methods for interacting with the context's internal state namely:
|
|
24
|
+
|
|
25
|
+
* **getState**: (selector?: (state: State) => any) => any
|
|
26
|
+
|
|
27
|
+
* **resetState**: VoidFunction // resets the state to the Provider initial `value` prop.
|
|
28
|
+
|
|
29
|
+
* **setState**: (changes: PartialState\<State\>) => void
|
|
30
|
+
|
|
31
|
+
* **subscribe**: (listener: (newValue: PartialState\<State\>, oldValue: PartialState\<State\>) => void) => ***UnsubscribeFunction***
|
|
32
|
+
|
|
33
|
+
### Prehooks
|
|
34
|
+
|
|
35
|
+
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.
|
|
36
|
+
|
|
37
|
+
* **resetState**: (state: {current: State, original: State}) => boolean
|
|
38
|
+
|
|
39
|
+
* **setState**: (newChanges: PartialState\<State\>) => boolean
|
|
40
|
+
|
|
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.
|
|
42
|
+
|
|
43
|
+
## Usage
|
|
44
|
+
|
|
45
|
+
<i><u>context.js</u></i>
|
|
46
|
+
|
|
47
|
+
import { createContext } from '@webkrafters/react-observable-context';
|
|
48
|
+
const ObservableContext = createContext();
|
|
49
|
+
export default ObservableContext;
|
|
50
|
+
|
|
51
|
+
<i><u>reset.js</u></i>
|
|
52
|
+
|
|
53
|
+
import React, { useContext } from 'react';
|
|
54
|
+
|
|
55
|
+
import ObservableContext from './context';
|
|
56
|
+
|
|
57
|
+
const Reset = () => {
|
|
58
|
+
|
|
59
|
+
const { resetState } = useContext( ObservableContext );
|
|
60
|
+
|
|
61
|
+
useEffect(() => console.log( 'Reset component rendered.....' ));
|
|
62
|
+
|
|
63
|
+
return ( <button onClick={ resetState }>reset context</button> );
|
|
64
|
+
};
|
|
65
|
+
Reset.displayName = 'Reset';
|
|
66
|
+
|
|
67
|
+
export default Reset;
|
|
68
|
+
|
|
69
|
+
<i><u>tally-display.js</u></i>
|
|
70
|
+
|
|
71
|
+
import React, { useContext, useEffect, useState } from 'react';
|
|
72
|
+
|
|
73
|
+
import ObservableContext from './context';
|
|
74
|
+
|
|
75
|
+
import Reset from './reset';
|
|
76
|
+
|
|
77
|
+
const TallyDisplay = () => {
|
|
78
|
+
|
|
79
|
+
const { getState, subscribe } = useContext( ObservableContext );
|
|
80
|
+
|
|
81
|
+
const [ , tripRender ] = useState( false );
|
|
82
|
+
|
|
83
|
+
useEffect(() => subscribe( newValue => {
|
|
84
|
+
[ 'color', 'price', 'type' ].some( k => k in newValue ) &&
|
|
85
|
+
tripRender( s => !s );
|
|
86
|
+
}), []);
|
|
87
|
+
|
|
88
|
+
useEffect(() => console.log( 'TallyDisplay component rendered.....' ));
|
|
89
|
+
|
|
90
|
+
return (
|
|
91
|
+
<div>
|
|
92
|
+
<table>
|
|
93
|
+
<tbody>
|
|
94
|
+
<tr><td><label>Type:</label></td><td>{ getState( s => s.type ) }</td></tr>
|
|
95
|
+
<tr><td><label>Color:</label></td><td>{ getState( s => s.color ) }</td></tr>
|
|
96
|
+
<tr><td><label>Price:</label></td><td>{ getState( s => s.price ).toFixed( 2 ) }</td></tr>
|
|
97
|
+
</tbody>
|
|
98
|
+
</table>
|
|
99
|
+
<div style={{ textAlign: 'right' }}>
|
|
100
|
+
<Reset />
|
|
101
|
+
</div>
|
|
102
|
+
</div>
|
|
103
|
+
);
|
|
104
|
+
};
|
|
105
|
+
TallyDisplay.displayName = 'TallyDisplay';
|
|
106
|
+
|
|
107
|
+
export default TallyDisplay;
|
|
108
|
+
|
|
109
|
+
<i><u>editor.js</u></i>
|
|
110
|
+
|
|
111
|
+
import React, { useCallback, useContext, useEffect, useRef } from 'react';
|
|
112
|
+
|
|
113
|
+
import ObservableContext from './context';
|
|
114
|
+
|
|
115
|
+
const Editor = () => {
|
|
116
|
+
|
|
117
|
+
const { setState } = useContext( ObservableContext );
|
|
118
|
+
|
|
119
|
+
const priceInputRef = useRef();
|
|
120
|
+
const colorInputRef = useRef();
|
|
121
|
+
const typeInputRef = useRef();
|
|
122
|
+
|
|
123
|
+
const updatePrice = useCallback(() => {
|
|
124
|
+
setState({ price: Number( priceInputRef.current.value ) });
|
|
125
|
+
}, []);
|
|
126
|
+
|
|
127
|
+
const updateColor = useCallback(() => {
|
|
128
|
+
setState({ color: colorInputRef.current.value });
|
|
129
|
+
}, []);
|
|
130
|
+
|
|
131
|
+
const updateType = useCallback(() => {
|
|
132
|
+
setState({ type: typeInputRef.current.value });
|
|
133
|
+
}, []);
|
|
134
|
+
|
|
135
|
+
useEffect(() => console.log( 'Editor component rendered.....' ));
|
|
136
|
+
|
|
137
|
+
return (
|
|
138
|
+
<fieldset style={{ margin: '10px 0' }}>
|
|
139
|
+
<legend>Editor</legend>
|
|
140
|
+
<div style={{ margin: '10px 0' }}>
|
|
141
|
+
<label>New Price: <input ref={ priceInputRef } /></label>
|
|
142
|
+
{ ' ' }
|
|
143
|
+
<button onClick={ updatePrice }>update price</button>
|
|
144
|
+
</div>
|
|
145
|
+
<div style={{ margin: '10px 0' }}>
|
|
146
|
+
<label>New Color: <input ref={ colorInputRef } /></label>
|
|
147
|
+
{ ' ' }
|
|
148
|
+
<button onClick={ updateColor }>update color</button>
|
|
149
|
+
</div>
|
|
150
|
+
<div style={{ margin: '10px 0' }}>
|
|
151
|
+
<label>New Type: <input ref={ typeInputRef } /></label>
|
|
152
|
+
{ ' ' }
|
|
153
|
+
<button onClick={ updateType }>update type</button>
|
|
154
|
+
</div>
|
|
155
|
+
</fieldset>
|
|
156
|
+
);
|
|
157
|
+
};
|
|
158
|
+
Editor.displayName = 'Editor';
|
|
159
|
+
|
|
160
|
+
export default Editor;
|
|
161
|
+
|
|
162
|
+
<i><u>product-description.js</u></i>
|
|
163
|
+
|
|
164
|
+
import React, { useContext, useEffect, useState } from 'react';
|
|
165
|
+
|
|
166
|
+
import ObservableContext from './context';
|
|
167
|
+
|
|
168
|
+
const ProductDescription = () => {
|
|
169
|
+
|
|
170
|
+
const store = useContext( ObservableContext );
|
|
171
|
+
|
|
172
|
+
const [ , tripRender ] = useState( false );
|
|
173
|
+
|
|
174
|
+
useEffect(() => store.subscribe( newValue => {
|
|
175
|
+
( 'color' in newValue || 'type' in newValue ) &&
|
|
176
|
+
tripRender( s => !s );
|
|
177
|
+
} ), []);
|
|
178
|
+
|
|
179
|
+
useEffect(() => console.log( 'ProductDescription component rendered.....' ));
|
|
180
|
+
|
|
181
|
+
const color = store.getState( s => s.color );
|
|
182
|
+
const type = store.getState( s => s.type );
|
|
183
|
+
|
|
184
|
+
return (
|
|
185
|
+
<div style={{ fontSize: 24 }}>
|
|
186
|
+
<strong>Description:</strong> { color } { type }
|
|
187
|
+
</div>
|
|
188
|
+
);
|
|
189
|
+
};
|
|
190
|
+
ProductDescription.displayName = 'ProductDescription';
|
|
191
|
+
|
|
192
|
+
export default ProductDescription;
|
|
193
|
+
|
|
194
|
+
<i><u>price-sticker.js</u></i>
|
|
195
|
+
|
|
196
|
+
import React, { useContext, useEffect, useState } from 'react';
|
|
197
|
+
|
|
198
|
+
import ObservableContext from './context';
|
|
199
|
+
|
|
200
|
+
const PriceSticker = () => {
|
|
201
|
+
|
|
202
|
+
const store = useContext( ObservableContext );
|
|
203
|
+
|
|
204
|
+
const [ price, setPrice ] = useState(() => store.getState( s => s.price ));
|
|
205
|
+
|
|
206
|
+
useEffect(() => store.subscribe( newValue => {
|
|
207
|
+
'price' in newValue && setPrice( newValue.price );
|
|
208
|
+
} ), []);
|
|
209
|
+
|
|
210
|
+
useEffect(() => console.log( 'PriceSticker component rendered.....' ));
|
|
211
|
+
|
|
212
|
+
return (
|
|
213
|
+
<div style={{ fontSize: 36, fontWeight: 800 }}>
|
|
214
|
+
${ price.toFixed( 2 ) }
|
|
215
|
+
</div>
|
|
216
|
+
);
|
|
217
|
+
};
|
|
218
|
+
PriceSticker.displayName = 'PriceSticker';
|
|
219
|
+
|
|
220
|
+
export default PriceSticker;
|
|
221
|
+
|
|
222
|
+
<i><u>product.js</u></i>
|
|
223
|
+
|
|
224
|
+
import React, { useCallback, useEffect, useState } from 'react';
|
|
225
|
+
import { Provider } from '@webkrafters/react-observable-context';
|
|
226
|
+
|
|
227
|
+
import ObservableContext from './context';
|
|
228
|
+
|
|
229
|
+
import Editor from './editor';
|
|
230
|
+
import PriceSticker from './price-sticker';
|
|
231
|
+
import ProductDescription from './product-description';
|
|
232
|
+
import TallyDisplay from './tally-display';
|
|
233
|
+
|
|
234
|
+
const Product = ({ prehooks = undefined, type }) => {
|
|
235
|
+
|
|
236
|
+
const [ state, setState ] = useState(() => ({
|
|
237
|
+
color: 'Burgundy',
|
|
238
|
+
price: 22.5,
|
|
239
|
+
type
|
|
240
|
+
}));
|
|
241
|
+
|
|
242
|
+
useEffect(() => {
|
|
243
|
+
setState({ type }); // use this to update only the changed state
|
|
244
|
+
// setState({ ...state, type }); // this will reset the context internal state
|
|
245
|
+
}, [ type ]);
|
|
246
|
+
|
|
247
|
+
const overridePricing = useCallback( e => setState({ price: Number( e.target.value ) }), [] );
|
|
248
|
+
|
|
249
|
+
return (
|
|
250
|
+
<div>
|
|
251
|
+
<div style={{ marginBottom: 10 }}>
|
|
252
|
+
<label>$ <input onKeyUp={ overridePricing } placeholder="override price here..."/></label>
|
|
253
|
+
</div>
|
|
254
|
+
<Provider
|
|
255
|
+
context={ ObservableContext }
|
|
256
|
+
prehooks={ prehooks }
|
|
257
|
+
value={ state }
|
|
258
|
+
>
|
|
259
|
+
<div style={{
|
|
260
|
+
borderBottom: '1px solid #333',
|
|
261
|
+
marginBottom: 10,
|
|
262
|
+
paddingBottom: 5
|
|
263
|
+
}}>
|
|
264
|
+
<Editor />
|
|
265
|
+
<TallyDisplay />
|
|
266
|
+
</div>
|
|
267
|
+
<ProductDescription />
|
|
268
|
+
<PriceSticker />
|
|
269
|
+
</Provider>
|
|
270
|
+
</div>
|
|
271
|
+
);
|
|
272
|
+
};
|
|
273
|
+
Product.displayName = 'Product';
|
|
274
|
+
|
|
275
|
+
export default Product;
|
|
276
|
+
|
|
277
|
+
<i><u>app.js</u></i>
|
|
278
|
+
|
|
279
|
+
import React, { useCallback, useMemo, useState } from 'react';
|
|
280
|
+
|
|
281
|
+
import Product from './product';
|
|
282
|
+
|
|
283
|
+
const App = () => {
|
|
284
|
+
|
|
285
|
+
const [ productType, setProductType ] = useState( 'Calculator' );
|
|
286
|
+
|
|
287
|
+
const updateType = useCallback( e => setProductType( e.target.value ), [] );
|
|
288
|
+
|
|
289
|
+
const prehooks = React.useMemo(() => ({
|
|
290
|
+
resetState: ( ...args ) => {
|
|
291
|
+
console.log( 'resetting state with >>>> ', JSON.stringify( args ) );
|
|
292
|
+
return true;
|
|
293
|
+
},
|
|
294
|
+
setState: ( ...args ) => {
|
|
295
|
+
console.log( 'setting state with >>>> ', JSON.stringify( args ) );
|
|
296
|
+
return true;
|
|
297
|
+
}
|
|
298
|
+
}), []);
|
|
299
|
+
|
|
300
|
+
return (
|
|
301
|
+
<div className="App">
|
|
302
|
+
<h1>Demo</h1>
|
|
303
|
+
<h2>A contrived product app.</h2>
|
|
304
|
+
<div style={{ marginBottom: 10 }}>
|
|
305
|
+
<label>Type: <input onKeyUp={ updateType } placeholder="override product type here..." /></label>
|
|
306
|
+
</div>
|
|
307
|
+
<Product prehooks={ prehooks } type={ productType } />
|
|
308
|
+
</div>
|
|
309
|
+
);
|
|
310
|
+
};
|
|
311
|
+
App.displayName = 'App';
|
|
312
|
+
|
|
313
|
+
export default App;
|
|
314
|
+
|
|
315
|
+
<i><u>index.js</i></b>
|
|
316
|
+
|
|
317
|
+
import React from 'react';
|
|
318
|
+
import ReactDOM from 'react-dom';
|
|
319
|
+
import App from './app';
|
|
320
|
+
ReactDOM.render(<App />, document.getElementById('root'));
|
|
321
|
+
|
|
322
|
+
## License
|
|
323
|
+
|
|
324
|
+
MIT
|
package/dist/index.d.ts
CHANGED
|
@@ -1,24 +1,24 @@
|
|
|
1
1
|
export class UsageError extends Error {
|
|
2
2
|
}
|
|
3
|
-
export function createContext<T_1 extends State>():
|
|
3
|
+
export function createContext<T_1 extends State>(): React.Context<Store<T_2>>;
|
|
4
4
|
/**
|
|
5
5
|
* Note: `context` prop is not updateable. Furtther updates to this prop are ignored.
|
|
6
6
|
*
|
|
7
|
-
* @type {
|
|
8
|
-
* children?:
|
|
7
|
+
* @type {React.FC<{
|
|
8
|
+
* children?: React.ReactNode,
|
|
9
9
|
* context: ObservableContext<T>,
|
|
10
10
|
* prehooks?: Prehooks<T>
|
|
11
|
-
* value: T
|
|
11
|
+
* value: PartialState<T>
|
|
12
12
|
* }>}
|
|
13
13
|
* @template {State} T
|
|
14
14
|
*/
|
|
15
|
-
export const Provider:
|
|
16
|
-
children?:
|
|
17
|
-
context:
|
|
15
|
+
export const Provider: React.FC<{
|
|
16
|
+
children?: React.ReactNode;
|
|
17
|
+
context: React.Context<Store<T_1>>;
|
|
18
18
|
prehooks?: Prehooks<T>;
|
|
19
|
-
value: T
|
|
19
|
+
value: PartialState<T>;
|
|
20
20
|
}>;
|
|
21
|
-
export type ObservableContext<T_1 extends State> =
|
|
21
|
+
export type ObservableContext<T_1 extends State> = React.Context<Store<T>>;
|
|
22
22
|
export type OptionalTask<F = void> = F extends void ? () => never : F;
|
|
23
23
|
export type Listener<T_1 extends State> = (newValue: PartialState<T>, oldValue: PartialState<T>) => void;
|
|
24
24
|
export type State = {
|
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,14 +97,26 @@ 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
|
*
|
|
102
|
-
* @type {
|
|
103
|
-
* children?:
|
|
115
|
+
* @type {React.FC<{
|
|
116
|
+
* children?: React.ReactNode,
|
|
104
117
|
* context: ObservableContext<T>,
|
|
105
118
|
* prehooks?: Prehooks<T>
|
|
106
|
-
* value: T
|
|
119
|
+
* value: PartialState<T>
|
|
107
120
|
* }>}
|
|
108
121
|
* @template {State} T
|
|
109
122
|
*/
|
|
@@ -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);
|
|
@@ -194,7 +205,7 @@ exports.Provider = Provider;
|
|
|
194
205
|
Provider.displayName = 'ObservableContext.Provider';
|
|
195
206
|
|
|
196
207
|
/**
|
|
197
|
-
* @typedef {
|
|
208
|
+
* @typedef {React.Context<Store<T>>} ObservableContext
|
|
198
209
|
* @template {State} T
|
|
199
210
|
*/
|
|
200
211
|
|
package/package.json
CHANGED
|
@@ -1,79 +1,79 @@
|
|
|
1
|
-
{
|
|
2
|
-
"author": "Stephen Isienyi <stephen.isienyi@webkrafting.com>",
|
|
3
|
-
"bugs": {
|
|
4
|
-
"url": "https://github.com/steveswork/react-observable-context/issues"
|
|
5
|
-
},
|
|
6
|
-
"contributors": [
|
|
7
|
-
"steveswork <stephen.isienyi@webkrafting.com> (https://github.com/steveswork)"
|
|
8
|
-
],
|
|
9
|
-
"dependencies": {
|
|
10
|
-
"@types/react": "^18.0.17",
|
|
11
|
-
"lodash.clonedeep": "^4.5.0",
|
|
12
|
-
"lodash.isempty": "^4.4.0",
|
|
13
|
-
"react": "^18.2.0"
|
|
14
|
-
},
|
|
15
|
-
"description": "Observable react context - prevents an automatic total component tree tear-down and re-rendering during context updates.",
|
|
16
|
-
"devDependencies": {
|
|
17
|
-
"@babel/cli": "^7.17.0",
|
|
18
|
-
"@babel/core": "^7.12.10",
|
|
19
|
-
"@babel/node": "^7.12.10",
|
|
20
|
-
"@babel/plugin-proposal-nullish-coalescing-operator": "^7.18.6",
|
|
21
|
-
"@babel/plugin-transform-runtime": "^7.17.0",
|
|
22
|
-
"@babel/preset-env": "^7.12.11",
|
|
23
|
-
"@babel/preset-react": "^7.18.6",
|
|
24
|
-
"@testing-library/jest-dom": "^5.16.5",
|
|
25
|
-
"@testing-library/react": "^13.4.0",
|
|
26
|
-
"@testing-library/user-event": "^14.4.3",
|
|
27
|
-
"@types/jest-cli": "^24.3.0",
|
|
28
|
-
"babel-jest": "^26.6.3",
|
|
29
|
-
"babel-loader": "^8.2.5",
|
|
30
|
-
"eslint": "^7.15.0",
|
|
31
|
-
"eslint-config-standard": "^16.0.2",
|
|
32
|
-
"eslint-plugin-import": "^2.22.1",
|
|
33
|
-
"eslint-plugin-jest": "^24.1.3",
|
|
34
|
-
"eslint-plugin-node": "^11.1.0",
|
|
35
|
-
"eslint-plugin-promise": "^4.2.1",
|
|
36
|
-
"eslint-plugin-react": "^7.31.11",
|
|
37
|
-
"eslint-plugin-standard": "^5.0.0",
|
|
38
|
-
"jest-cli": "^26.6.3",
|
|
39
|
-
"react-dom": "^18.2.0",
|
|
40
|
-
"react-performance-testing": "^2.0.0",
|
|
41
|
-
"typescript": "^4.8.2"
|
|
42
|
-
},
|
|
43
|
-
"files": [
|
|
44
|
-
"dist/index.js",
|
|
45
|
-
"dist/index.d.ts",
|
|
46
|
-
"index",
|
|
47
|
-
"package.json"
|
|
48
|
-
],
|
|
49
|
-
"homepage": "https://github.com/steveswork/react-observable-context#readme",
|
|
50
|
-
"jest": {
|
|
51
|
-
"transform": {
|
|
52
|
-
"\\.[jt]sx?$": "babel-jest"
|
|
53
|
-
}
|
|
54
|
-
},
|
|
55
|
-
"keywords": [
|
|
56
|
-
"context",
|
|
57
|
-
"react-hooks",
|
|
58
|
-
"react-context",
|
|
59
|
-
"react",
|
|
60
|
-
"useContext"
|
|
61
|
-
],
|
|
62
|
-
"license": "MIT",
|
|
63
|
-
"main": "index.js",
|
|
64
|
-
"name": "@webkrafters/react-observable-context",
|
|
65
|
-
"publishConfig": {
|
|
66
|
-
"access": "public"
|
|
67
|
-
},
|
|
68
|
-
"repository": {
|
|
69
|
-
"type": "git",
|
|
70
|
-
"url": "git+https://github.com/steveswork/react-observable-context.git"
|
|
71
|
-
},
|
|
72
|
-
"scripts": {
|
|
73
|
-
"build": "eslint --fix && rm -rf dist && babel src -d dist && npx -p typescript tsc",
|
|
74
|
-
"test": "eslint --fix && jest",
|
|
75
|
-
"test:watch": "eslint --fix && jest --watchAll"
|
|
76
|
-
},
|
|
77
|
-
"types": "dist/index.d.ts",
|
|
78
|
-
"version": "1.1.
|
|
79
|
-
}
|
|
1
|
+
{
|
|
2
|
+
"author": "Stephen Isienyi <stephen.isienyi@webkrafting.com>",
|
|
3
|
+
"bugs": {
|
|
4
|
+
"url": "https://github.com/steveswork/react-observable-context/issues"
|
|
5
|
+
},
|
|
6
|
+
"contributors": [
|
|
7
|
+
"steveswork <stephen.isienyi@webkrafting.com> (https://github.com/steveswork)"
|
|
8
|
+
],
|
|
9
|
+
"dependencies": {
|
|
10
|
+
"@types/react": "^18.0.17",
|
|
11
|
+
"lodash.clonedeep": "^4.5.0",
|
|
12
|
+
"lodash.isempty": "^4.4.0",
|
|
13
|
+
"react": "^18.2.0"
|
|
14
|
+
},
|
|
15
|
+
"description": "Observable react context - prevents an automatic total component tree tear-down and re-rendering during context updates.",
|
|
16
|
+
"devDependencies": {
|
|
17
|
+
"@babel/cli": "^7.17.0",
|
|
18
|
+
"@babel/core": "^7.12.10",
|
|
19
|
+
"@babel/node": "^7.12.10",
|
|
20
|
+
"@babel/plugin-proposal-nullish-coalescing-operator": "^7.18.6",
|
|
21
|
+
"@babel/plugin-transform-runtime": "^7.17.0",
|
|
22
|
+
"@babel/preset-env": "^7.12.11",
|
|
23
|
+
"@babel/preset-react": "^7.18.6",
|
|
24
|
+
"@testing-library/jest-dom": "^5.16.5",
|
|
25
|
+
"@testing-library/react": "^13.4.0",
|
|
26
|
+
"@testing-library/user-event": "^14.4.3",
|
|
27
|
+
"@types/jest-cli": "^24.3.0",
|
|
28
|
+
"babel-jest": "^26.6.3",
|
|
29
|
+
"babel-loader": "^8.2.5",
|
|
30
|
+
"eslint": "^7.15.0",
|
|
31
|
+
"eslint-config-standard": "^16.0.2",
|
|
32
|
+
"eslint-plugin-import": "^2.22.1",
|
|
33
|
+
"eslint-plugin-jest": "^24.1.3",
|
|
34
|
+
"eslint-plugin-node": "^11.1.0",
|
|
35
|
+
"eslint-plugin-promise": "^4.2.1",
|
|
36
|
+
"eslint-plugin-react": "^7.31.11",
|
|
37
|
+
"eslint-plugin-standard": "^5.0.0",
|
|
38
|
+
"jest-cli": "^26.6.3",
|
|
39
|
+
"react-dom": "^18.2.0",
|
|
40
|
+
"react-performance-testing": "^2.0.0",
|
|
41
|
+
"typescript": "^4.8.2"
|
|
42
|
+
},
|
|
43
|
+
"files": [
|
|
44
|
+
"dist/index.js",
|
|
45
|
+
"dist/index.d.ts",
|
|
46
|
+
"index",
|
|
47
|
+
"package.json"
|
|
48
|
+
],
|
|
49
|
+
"homepage": "https://github.com/steveswork/react-observable-context#readme",
|
|
50
|
+
"jest": {
|
|
51
|
+
"transform": {
|
|
52
|
+
"\\.[jt]sx?$": "babel-jest"
|
|
53
|
+
}
|
|
54
|
+
},
|
|
55
|
+
"keywords": [
|
|
56
|
+
"context",
|
|
57
|
+
"react-hooks",
|
|
58
|
+
"react-context",
|
|
59
|
+
"react",
|
|
60
|
+
"useContext"
|
|
61
|
+
],
|
|
62
|
+
"license": "MIT",
|
|
63
|
+
"main": "index.js",
|
|
64
|
+
"name": "@webkrafters/react-observable-context",
|
|
65
|
+
"publishConfig": {
|
|
66
|
+
"access": "public"
|
|
67
|
+
},
|
|
68
|
+
"repository": {
|
|
69
|
+
"type": "git",
|
|
70
|
+
"url": "git+https://github.com/steveswork/react-observable-context.git"
|
|
71
|
+
},
|
|
72
|
+
"scripts": {
|
|
73
|
+
"build": "eslint --fix && rm -rf dist && babel src -d dist && npx -p typescript tsc",
|
|
74
|
+
"test": "eslint --fix && jest",
|
|
75
|
+
"test:watch": "eslint --fix && jest --watchAll"
|
|
76
|
+
},
|
|
77
|
+
"types": "dist/index.d.ts",
|
|
78
|
+
"version": "1.1.5"
|
|
79
|
+
}
|