@webkrafters/react-observable-context 1.1.5 → 2.0.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 +324 -324
- package/dist/index.d.ts +1 -0
- package/dist/index.js +50 -16
- package/package.json +2 -1
package/README.md
CHANGED
|
@@ -1,324 +1,324 @@
|
|
|
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
|
|
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
|
-
|
|
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
|
-
<div style={{ margin: '10px 0' }}>
|
|
146
|
-
<label>New
|
|
147
|
-
{ ' ' }
|
|
148
|
-
<button onClick={
|
|
149
|
-
</div>
|
|
150
|
-
<div style={{ margin: '10px 0' }}>
|
|
151
|
-
<label>New
|
|
152
|
-
{ ' ' }
|
|
153
|
-
<button onClick={
|
|
154
|
-
</div>
|
|
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
|
-
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
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
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 =
|
|
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
|
|
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 **3** modules namely: the **createContext** method, the **useContext** hook 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
|
+
* **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 names to watch. Change in any of the referenced properties automatically triggers a render of the component calling this hook.
|
|
20
|
+
|
|
21
|
+
* **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>.
|
|
22
|
+
|
|
23
|
+
***<u>Note:</u>*** the Provider `context` prop is not updateable. Once set, all further updates to this prop are not recorded.
|
|
24
|
+
|
|
25
|
+
### The Store
|
|
26
|
+
|
|
27
|
+
The context's `store` exposes **4** methods for interacting with the context's internal state namely:
|
|
28
|
+
|
|
29
|
+
* **getState**: (selector?: (state: State) => any) => any
|
|
30
|
+
|
|
31
|
+
* **resetState**: VoidFunction // resets the state to the Provider initial `value` prop.
|
|
32
|
+
|
|
33
|
+
* **setState**: (changes: PartialState\<State\>) => void
|
|
34
|
+
|
|
35
|
+
* **subscribe**: (listener: (newValue: PartialState\<State\>, oldValue: PartialState\<State\>) => void) => ***UnsubscribeFunction***
|
|
36
|
+
|
|
37
|
+
### Prehooks
|
|
38
|
+
|
|
39
|
+
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.
|
|
40
|
+
|
|
41
|
+
* **resetState**: (state: {current: State, original: State}) => boolean
|
|
42
|
+
|
|
43
|
+
* **setState**: (newChanges: PartialState\<State\>) => boolean
|
|
44
|
+
|
|
45
|
+
***<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.
|
|
46
|
+
|
|
47
|
+
## Usage
|
|
48
|
+
|
|
49
|
+
<i><u>context.js</u></i>
|
|
50
|
+
|
|
51
|
+
import { createContext } from '@webkrafters/react-observable-context';
|
|
52
|
+
const ObservableContext = createContext();
|
|
53
|
+
export default ObservableContext;
|
|
54
|
+
|
|
55
|
+
<i><u>reset.js</u></i>
|
|
56
|
+
|
|
57
|
+
import React, { useEffect } from 'react';
|
|
58
|
+
|
|
59
|
+
import { useContext } from '@webkrafters/react-observable-context';
|
|
60
|
+
|
|
61
|
+
import ObservableContext from './context';
|
|
62
|
+
|
|
63
|
+
const Reset = () => {
|
|
64
|
+
|
|
65
|
+
const { resetState } = useContext( ObservableContext );
|
|
66
|
+
|
|
67
|
+
useEffect(() => console.log( 'Reset component rendered.....' ));
|
|
68
|
+
|
|
69
|
+
return ( <button onClick={ resetState }>reset context</button> );
|
|
70
|
+
};
|
|
71
|
+
Reset.displayName = 'Reset';
|
|
72
|
+
|
|
73
|
+
export default Reset;
|
|
74
|
+
|
|
75
|
+
<i><u>tally-display.js</u></i>
|
|
76
|
+
|
|
77
|
+
import React, { useEffect } from 'react';
|
|
78
|
+
|
|
79
|
+
import { useContext } from '@webkrafters/react-observable-context';
|
|
80
|
+
|
|
81
|
+
import ObservableContext from './context';
|
|
82
|
+
|
|
83
|
+
import Reset from './reset';
|
|
84
|
+
|
|
85
|
+
const CONTEXT_KEYS = [ 'color', 'price', 'type' ];
|
|
86
|
+
|
|
87
|
+
const TallyDisplay = () => {
|
|
88
|
+
|
|
89
|
+
const { getState } = useContext( ObservableContext, CONTEXT_KEYS );
|
|
90
|
+
|
|
91
|
+
useEffect(() => console.log( 'TallyDisplay component rendered.....' ));
|
|
92
|
+
|
|
93
|
+
return (
|
|
94
|
+
<div>
|
|
95
|
+
<table>
|
|
96
|
+
<tbody>
|
|
97
|
+
<tr><td><label>Type:</label></td><td>{ getState( s => s.type ) }</td></tr>
|
|
98
|
+
<tr><td><label>Color:</label></td><td>{ getState( s => s.color ) }</td></tr>
|
|
99
|
+
<tr><td><label>Price:</label></td><td>{ getState( s => s.price ).toFixed( 2 ) }</td></tr>
|
|
100
|
+
</tbody>
|
|
101
|
+
</table>
|
|
102
|
+
<div style={{ textAlign: 'right' }}>
|
|
103
|
+
<Reset />
|
|
104
|
+
</div>
|
|
105
|
+
</div>
|
|
106
|
+
);
|
|
107
|
+
};
|
|
108
|
+
TallyDisplay.displayName = 'TallyDisplay';
|
|
109
|
+
|
|
110
|
+
export default TallyDisplay;
|
|
111
|
+
|
|
112
|
+
<i><u>editor.js</u></i>
|
|
113
|
+
|
|
114
|
+
import React, { useCallback, useEffect, useRef } from 'react';
|
|
115
|
+
|
|
116
|
+
import { useContext } from '@webkrafters/react-observable-context';
|
|
117
|
+
|
|
118
|
+
import ObservableContext from './context';
|
|
119
|
+
|
|
120
|
+
const Editor = () => {
|
|
121
|
+
|
|
122
|
+
const { setState } = useContext( ObservableContext );
|
|
123
|
+
|
|
124
|
+
const priceInputRef = useRef();
|
|
125
|
+
const colorInputRef = useRef();
|
|
126
|
+
const typeInputRef = useRef();
|
|
127
|
+
|
|
128
|
+
const updatePrice = useCallback(() => {
|
|
129
|
+
setState({ price: Number( priceInputRef.current.value ) });
|
|
130
|
+
}, []);
|
|
131
|
+
|
|
132
|
+
const updateColor = useCallback(() => {
|
|
133
|
+
setState({ color: colorInputRef.current.value });
|
|
134
|
+
}, []);
|
|
135
|
+
|
|
136
|
+
const updateType = useCallback(() => {
|
|
137
|
+
setState({ type: typeInputRef.current.value });
|
|
138
|
+
}, []);
|
|
139
|
+
|
|
140
|
+
useEffect(() => console.log( 'Editor component rendered.....' ));
|
|
141
|
+
|
|
142
|
+
return (
|
|
143
|
+
<fieldset style={{ margin: '10px 0' }}>
|
|
144
|
+
<legend>Editor</legend>
|
|
145
|
+
<div style={{ margin: '10px 0' }}>
|
|
146
|
+
<label>New Price: <input ref={ priceInputRef } /></label>
|
|
147
|
+
{ ' ' }
|
|
148
|
+
<button onClick={ updatePrice }>update price</button>
|
|
149
|
+
</div>
|
|
150
|
+
<div style={{ margin: '10px 0' }}>
|
|
151
|
+
<label>New Color: <input ref={ colorInputRef } /></label>
|
|
152
|
+
{ ' ' }
|
|
153
|
+
<button onClick={ updateColor }>update color</button>
|
|
154
|
+
</div>
|
|
155
|
+
<div style={{ margin: '10px 0' }}>
|
|
156
|
+
<label>New Type: <input ref={ typeInputRef } /></label>
|
|
157
|
+
{ ' ' }
|
|
158
|
+
<button onClick={ updateType }>update type</button>
|
|
159
|
+
</div>
|
|
160
|
+
</fieldset>
|
|
161
|
+
);
|
|
162
|
+
};
|
|
163
|
+
Editor.displayName = 'Editor';
|
|
164
|
+
|
|
165
|
+
export default Editor;
|
|
166
|
+
|
|
167
|
+
<i><u>product-description.js</u></i>
|
|
168
|
+
|
|
169
|
+
import React, { useEffect } from 'react';
|
|
170
|
+
|
|
171
|
+
import { useContext } from '@webkrafters/react-observable-context';
|
|
172
|
+
|
|
173
|
+
import ObservableContext from './context';
|
|
174
|
+
|
|
175
|
+
const CONTEXT_KEYS = [ 'color', 'type' ];
|
|
176
|
+
|
|
177
|
+
const ProductDescription = () => {
|
|
178
|
+
|
|
179
|
+
const store = useContext( ObservableContext, CONTEXT_KEYS );
|
|
180
|
+
|
|
181
|
+
useEffect(() => console.log( 'ProductDescription component rendered.....' ));
|
|
182
|
+
|
|
183
|
+
const color = store.getState( s => s.color );
|
|
184
|
+
const type = store.getState( s => s.type );
|
|
185
|
+
|
|
186
|
+
return (
|
|
187
|
+
<div style={{ fontSize: 24 }}>
|
|
188
|
+
<strong>Description:</strong> { color } { type }
|
|
189
|
+
</div>
|
|
190
|
+
);
|
|
191
|
+
};
|
|
192
|
+
ProductDescription.displayName = 'ProductDescription';
|
|
193
|
+
|
|
194
|
+
export default ProductDescription;
|
|
195
|
+
|
|
196
|
+
<i><u>price-sticker.js</u></i>
|
|
197
|
+
|
|
198
|
+
import React, { useEffect } from 'react';
|
|
199
|
+
|
|
200
|
+
import { useContext } from '@webkrafters/react-observable-context';
|
|
201
|
+
|
|
202
|
+
import ObservableContext from './context';
|
|
203
|
+
|
|
204
|
+
const CONTEXT_KEYS = [ 'price' ];
|
|
205
|
+
|
|
206
|
+
const PriceSticker = () => {
|
|
207
|
+
|
|
208
|
+
const { getState } = useContext( ObservableContext, CONTEXT_KEYS );
|
|
209
|
+
|
|
210
|
+
useEffect(() => console.log( 'PriceSticker component rendered.....' ));
|
|
211
|
+
|
|
212
|
+
return (
|
|
213
|
+
<div style={{ fontSize: 36, fontWeight: 800 }}>
|
|
214
|
+
${ getState( s => s.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 = 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,6 +1,7 @@
|
|
|
1
1
|
export class UsageError extends Error {
|
|
2
2
|
}
|
|
3
3
|
export function createContext<T_1 extends State>(): React.Context<Store<T_2>>;
|
|
4
|
+
export function useContext<T_1 extends State>(context: React.Context<Store<T_2>>, watchedKeys?: (keyof T_1)[]): Store<T_1>;
|
|
4
5
|
/**
|
|
5
6
|
* Note: `context` prop is not updateable. Furtther updates to this prop are ignored.
|
|
6
7
|
*
|
package/dist/index.js
CHANGED
|
@@ -4,10 +4,11 @@ function _typeof(obj) { "@babel/helpers - typeof"; return _typeof = "function" =
|
|
|
4
4
|
Object.defineProperty(exports, "__esModule", {
|
|
5
5
|
value: true
|
|
6
6
|
});
|
|
7
|
-
exports.createContext = exports.UsageError = exports.Provider = void 0;
|
|
7
|
+
exports.useContext = exports.createContext = exports.UsageError = exports.Provider = void 0;
|
|
8
8
|
var _react = _interopRequireWildcard(require("react"));
|
|
9
9
|
var _lodash = _interopRequireDefault(require("lodash.clonedeep"));
|
|
10
|
-
var _lodash2 = _interopRequireDefault(require("lodash.
|
|
10
|
+
var _lodash2 = _interopRequireDefault(require("lodash.has"));
|
|
11
|
+
var _lodash3 = _interopRequireDefault(require("lodash.isempty"));
|
|
11
12
|
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
|
|
12
13
|
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); }
|
|
13
14
|
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; }
|
|
@@ -67,12 +68,45 @@ var createContext = function createContext() {
|
|
|
67
68
|
});
|
|
68
69
|
};
|
|
69
70
|
|
|
71
|
+
/**
|
|
72
|
+
* Actively monitors the store and triggers component re-render if any of the watched keys in the state objects changes
|
|
73
|
+
*
|
|
74
|
+
* @param {ObservableContext<T>} context
|
|
75
|
+
* @param {Array<keyof T>} [watchedKeys = []] Names of state properties to watch. A change in any of the referenced properties results in this component render.
|
|
76
|
+
* @returns {Store<T>}
|
|
77
|
+
* @template {State} T
|
|
78
|
+
*/
|
|
79
|
+
exports.createContext = createContext;
|
|
80
|
+
var useContext = function useContext(context) {
|
|
81
|
+
var watchedKeys = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : [];
|
|
82
|
+
var store = (0, _react.useContext)(context);
|
|
83
|
+
var _useState = (0, _react.useState)(false),
|
|
84
|
+
_useState2 = _slicedToArray(_useState, 2),
|
|
85
|
+
tripRender = _useState2[1];
|
|
86
|
+
var watched = (0, _react.useMemo)(function () {
|
|
87
|
+
return Array.isArray(watchedKeys) ? Array.from(new Set(watchedKeys)) : [];
|
|
88
|
+
}, [watchedKeys]);
|
|
89
|
+
(0, _react.useEffect)(function () {
|
|
90
|
+
if (!watched.length) {
|
|
91
|
+
return;
|
|
92
|
+
}
|
|
93
|
+
return store.subscribe(function (newChanges) {
|
|
94
|
+
watched.some(function (w) {
|
|
95
|
+
return (0, _lodash2["default"])(newChanges, w);
|
|
96
|
+
}) && tripRender(function (s) {
|
|
97
|
+
return !s;
|
|
98
|
+
});
|
|
99
|
+
});
|
|
100
|
+
}, [watched]);
|
|
101
|
+
return store;
|
|
102
|
+
};
|
|
103
|
+
|
|
70
104
|
/**
|
|
71
105
|
* @readonly
|
|
72
106
|
* @type {Prehooks<T>}
|
|
73
107
|
* @template {State} T
|
|
74
108
|
*/
|
|
75
|
-
exports.
|
|
109
|
+
exports.useContext = useContext;
|
|
76
110
|
var defaultPrehooks = Object.freeze({});
|
|
77
111
|
|
|
78
112
|
/**
|
|
@@ -94,7 +128,7 @@ var _setState = function _setState(state, newState, onStateChange) {
|
|
|
94
128
|
state[k] = newState[k];
|
|
95
129
|
newChanges[k] = newState[k];
|
|
96
130
|
}
|
|
97
|
-
!(0,
|
|
131
|
+
!(0, _lodash3["default"])(newChanges) && onStateChange(newChanges, replacedValue);
|
|
98
132
|
};
|
|
99
133
|
|
|
100
134
|
/**
|
|
@@ -131,21 +165,21 @@ var Provider = function Provider(_ref) {
|
|
|
131
165
|
var initialState = (0, _react.useRef)(value);
|
|
132
166
|
|
|
133
167
|
/** @type {[Set<Listener<T>>, Function]} */
|
|
134
|
-
var
|
|
168
|
+
var _useState3 = (0, _react.useState)(function () {
|
|
135
169
|
return new Set();
|
|
136
170
|
}),
|
|
137
|
-
|
|
138
|
-
listeners =
|
|
171
|
+
_useState4 = _slicedToArray(_useState3, 1),
|
|
172
|
+
listeners = _useState4[0];
|
|
139
173
|
/** @type {[T, Function]} */
|
|
140
|
-
var
|
|
174
|
+
var _useState5 = (0, _react.useState)(function () {
|
|
141
175
|
return (0, _lodash["default"])(value);
|
|
142
176
|
}),
|
|
143
|
-
_useState4 = _slicedToArray(_useState3, 1),
|
|
144
|
-
state = _useState4[0];
|
|
145
|
-
/** @type {ObservableContext<T>} */
|
|
146
|
-
var _useState5 = (0, _react.useState)(context),
|
|
147
177
|
_useState6 = _slicedToArray(_useState5, 1),
|
|
148
|
-
|
|
178
|
+
state = _useState6[0];
|
|
179
|
+
/** @type {ObservableContext<T>} */
|
|
180
|
+
var _useState7 = (0, _react.useState)(context),
|
|
181
|
+
_useState8 = _slicedToArray(_useState7, 1),
|
|
182
|
+
StoreContext = _useState8[0];
|
|
149
183
|
|
|
150
184
|
/** @type {Listener<T>} */
|
|
151
185
|
var onChange = function onChange(newValue, oldValue) {
|
|
@@ -187,7 +221,7 @@ var Provider = function Provider(_ref) {
|
|
|
187
221
|
}, [value]);
|
|
188
222
|
|
|
189
223
|
/** @type {[Store<T>, Function]} */
|
|
190
|
-
var
|
|
224
|
+
var _useState9 = (0, _react.useState)(function () {
|
|
191
225
|
return {
|
|
192
226
|
getState: getState,
|
|
193
227
|
resetState: resetState,
|
|
@@ -195,8 +229,8 @@ var Provider = function Provider(_ref) {
|
|
|
195
229
|
subscribe: subscribe
|
|
196
230
|
};
|
|
197
231
|
}),
|
|
198
|
-
|
|
199
|
-
store =
|
|
232
|
+
_useState10 = _slicedToArray(_useState9, 1),
|
|
233
|
+
store = _useState10[0];
|
|
200
234
|
return /*#__PURE__*/_react["default"].createElement(StoreContext.Provider, {
|
|
201
235
|
value: store
|
|
202
236
|
}, children);
|
package/package.json
CHANGED
|
@@ -9,6 +9,7 @@
|
|
|
9
9
|
"dependencies": {
|
|
10
10
|
"@types/react": "^18.0.17",
|
|
11
11
|
"lodash.clonedeep": "^4.5.0",
|
|
12
|
+
"lodash.has": "^4.5.2",
|
|
12
13
|
"lodash.isempty": "^4.4.0",
|
|
13
14
|
"react": "^18.2.0"
|
|
14
15
|
},
|
|
@@ -75,5 +76,5 @@
|
|
|
75
76
|
"test:watch": "eslint --fix && jest --watchAll"
|
|
76
77
|
},
|
|
77
78
|
"types": "dist/index.d.ts",
|
|
78
|
-
"version": "
|
|
79
|
+
"version": "2.0.0"
|
|
79
80
|
}
|