@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 CHANGED
@@ -1,322 +1,324 @@
1
- # React-Observable-Context
2
-
3
- Observable react context - prevents an automatic total component tree re-rendering at context change.
4
-
5
- <h4><u>Install</u></h4>
6
-
7
- npm i -S @webkrafters/react-observable-context
8
-
9
- npm install --save @webkrafters/react-observable-context
10
-
11
- ## API
12
-
13
- The React-Observable-Context package exports only **2** modules namely: the **createContext** method and the **Provider** component.
14
-
15
- `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`.
16
-
17
- 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
-
19
- <i><u>Note:</u></i> the Provider `context` prop is not updateable. Once set, all further updates to this prop are ignored.
20
-
21
- The context's `store` exposes **4** methods for interacting with the context's internal state namely:
22
-
23
- * **getState**: (selector?: (state: State) => any) => any
24
-
25
- * **resetState**: VoidFunction // resets the state to the Provider latest `value` prop.
26
-
27
- * **setState**: (changes: PartialState\<State\>) => void
28
-
29
- * **subscribe**: (listener: (newValue: PartialState\<State\>, oldValue: PartialState\<State\>) => void) => ***UnsubscribeFunction***
30
-
31
- ### Prehooks
32
-
33
- 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.
34
-
35
- * **resetState**: (state: {current: State, original: State}) => boolean
36
-
37
- * **setState**: (newChanges: PartialState\<State\>) => boolean
38
-
39
- **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
-
41
- ## Usage
42
-
43
- <i><u>context.js</u></i>
44
-
45
- import { createContext } from '@webkrafters/react-observable-context';
46
- const ObservableContext = createContext();
47
- export default ObservableContext;
48
-
49
- <i><u>reset.js</u></i>
50
-
51
- import React, { useContext } from 'react';
52
-
53
- import ObservableContext from './context';
54
-
55
- const Reset = () => {
56
-
57
- const { resetState } = useContext( ObservableContext );
58
-
59
- useEffect(() => console.log( 'Reset component rendered.....' ));
60
-
61
- return ( <button onClick={ resetState }>reset context</button> );
62
- };
63
- Reset.displayName = 'Reset';
64
-
65
- export default Reset;
66
-
67
- <i><u>tally-display.js</u></i>
68
-
69
- import React, { useContext, useEffect, useState } from 'react';
70
-
71
- import ObservableContext from './context';
72
-
73
- import Reset from './reset';
74
-
75
- const TallyDisplay = () => {
76
-
77
- const { getState, subscribe } = useContext( ObservableContext );
78
-
79
- const [ , tripRender ] = useState( false );
80
-
81
- useEffect(() => subscribe( newValue => {
82
- [ 'color', 'price', 'type' ].some( k => k in newValue ) &&
83
- tripRender( s => !s );
84
- }), []);
85
-
86
- useEffect(() => console.log( 'TallyDisplay component rendered.....' ));
87
-
88
- return (
89
- <div>
90
- <table>
91
- <tbody>
92
- <tr><td><label>Type:</label></td><td>{ getState( s => s.type ) }</td></tr>
93
- <tr><td><label>Color:</label></td><td>{ getState( s => s.color ) }</td></tr>
94
- <tr><td><label>Price:</label></td><td>{ getState( s => s.price ).toFixed( 2 ) }</td></tr>
95
- </tbody>
96
- </table>
97
- <div style={{ textAlign: 'right' }}>
98
- <Reset />
99
- </div>
100
- </div>
101
- );
102
- };
103
- TallyDisplay.displayName = 'TallyDisplay';
104
-
105
- export default TallyDisplay;
106
-
107
- <i><u>editor.js</u></i>
108
-
109
- import React, { useCallback, useContext, useEffect, useRef } from 'react';
110
-
111
- import ObservableContext from './context';
112
-
113
- const Editor = () => {
114
-
115
- const { setState } = useContext( ObservableContext );
116
-
117
- const priceInputRef = useRef();
118
- const colorInputRef = useRef();
119
- const typeInputRef = useRef();
120
-
121
- const updatePrice = useCallback(() => {
122
- setState({ price: Number( priceInputRef.current.value ) });
123
- }, []);
124
-
125
- const updateColor = useCallback(() => {
126
- setState({ color: colorInputRef.current.value });
127
- }, []);
128
-
129
- const updateType = useCallback(() => {
130
- setState({ type: typeInputRef.current.value });
131
- }, []);
132
-
133
- useEffect(() => console.log( 'Editor component rendered.....' ));
134
-
135
- return (
136
- <fieldset style={{ margin: '10px 0' }}>
137
- <legend>Editor</legend>
138
- <div style={{ margin: '10px 0' }}>
139
- <label>New Price: <input ref={ priceInputRef } /></label>
140
- { ' ' }
141
- <button onClick={ updatePrice }>update price</button>
142
- </div>
143
- <div style={{ margin: '10px 0' }}>
144
- <label>New Color: <input ref={ colorInputRef } /></label>
145
- { ' ' }
146
- <button onClick={ updateColor }>update color</button>
147
- </div>
148
- <div style={{ margin: '10px 0' }}>
149
- <label>New Type: <input ref={ typeInputRef } /></label>
150
- { ' ' }
151
- <button onClick={ updateType }>update type</button>
152
- </div>
153
- </fieldset>
154
- );
155
- };
156
- Editor.displayName = 'Editor';
157
-
158
- export default Editor;
159
-
160
- <i><u>product-description.js</u></i>
161
-
162
- import React, { useContext, useEffect, useState } from 'react';
163
-
164
- import ObservableContext from './context';
165
-
166
- const ProductDescription = () => {
167
-
168
- const store = useContext( ObservableContext );
169
-
170
- const [ , tripRender ] = useState( false );
171
-
172
- useEffect(() => store.subscribe( newValue => {
173
- ( 'color' in newValue || 'type' in newValue ) &&
174
- tripRender( s => !s );
175
- } ), []);
176
-
177
- useEffect(() => console.log( 'ProductDescription component rendered.....' ));
178
-
179
- const color = store.getState( s => s.color );
180
- const type = store.getState( s => s.type );
181
-
182
- return (
183
- <div style={{ fontSize: 24 }}>
184
- <strong>Description:</strong> { color } { type }
185
- </div>
186
- );
187
- };
188
- ProductDescription.displayName = 'ProductDescription';
189
-
190
- export default ProductDescription;
191
-
192
- <i><u>price-sticker.js</u></i>
193
-
194
- import React, { useContext, useEffect, useState } from 'react';
195
-
196
- import ObservableContext from './context';
197
-
198
- const PriceSticker = () => {
199
-
200
- const store = useContext( ObservableContext );
201
-
202
- const [ price, setPrice ] = useState(() => store.getState( s => s.price ));
203
-
204
- useEffect(() => store.subscribe( newValue => {
205
- 'price' in newValue && setPrice( newValue.price );
206
- } ), []);
207
-
208
- useEffect(() => console.log( 'PriceSticker component rendered.....' ));
209
-
210
- return (
211
- <div style={{ fontSize: 36, fontWeight: 800 }}>
212
- ${ price.toFixed( 2 ) }
213
- </div>
214
- );
215
- };
216
- PriceSticker.displayName = 'PriceSticker';
217
-
218
- export default PriceSticker;
219
-
220
- <i><u>product.js</u></i>
221
-
222
- import React, { useCallback, useEffect, useState } from 'react';
223
- import { Provider } from '@webkrafters/react-observable-context';
224
-
225
- import ObservableContext from './context';
226
-
227
- import Editor from './editor';
228
- import PriceSticker from './price-sticker';
229
- import ProductDescription from './product-description';
230
- import TallyDisplay from './tally-display';
231
-
232
- const Product = ({ prehooks = undefined, type }) => {
233
-
234
- const [ state, setState ] = useState(() => ({
235
- color: 'Burgundy',
236
- price: 22.5,
237
- type
238
- }));
239
-
240
- useEffect(() => {
241
- setState({ type }); // use this to update only the changed state
242
- // setState({ ...state, type }); // this will reset the context internal state
243
- }, [ type ]);
244
-
245
- const overridePricing = useCallback( e => setState({ price: Number( e.target.value ) }), [] );
246
-
247
- return (
248
- <div>
249
- <div style={{ marginBottom: 10 }}>
250
- <label>$ <input onKeyUp={ overridePricing } placeholder="override price here..."/></label>
251
- </div>
252
- <Provider
253
- context={ ObservableContext }
254
- prehooks={ prehooks }
255
- value={ state }
256
- >
257
- <div style={{
258
- borderBottom: '1px solid #333',
259
- marginBottom: 10,
260
- paddingBottom: 5
261
- }}>
262
- <Editor />
263
- <TallyDisplay />
264
- </div>
265
- <ProductDescription />
266
- <PriceSticker />
267
- </Provider>
268
- </div>
269
- );
270
- };
271
- Product.displayName = 'Product';
272
-
273
- export default Product;
274
-
275
- <i><u>app.js</u></i>
276
-
277
- import React, { useCallback, useMemo, useState } from 'react';
278
-
279
- import Product from './product';
280
-
281
- const App = () => {
282
-
283
- const [ productType, setProductType ] = useState( 'Calculator' );
284
-
285
- const updateType = useCallback( e => setProductType( e.target.value ), [] );
286
-
287
- const prehooks = React.useMemo(() => ({
288
- resetState: ( ...args ) => {
289
- console.log( 'resetting state with >>>> ', JSON.stringify( args ) );
290
- return true;
291
- },
292
- setState: ( ...args ) => {
293
- console.log( 'setting state with >>>> ', JSON.stringify( args ) );
294
- return true;
295
- }
296
- }), []);
297
-
298
- return (
299
- <div className="App">
300
- <h1>Demo</h1>
301
- <h2>A contrived product app.</h2>
302
- <div style={{ marginBottom: 10 }}>
303
- <label>Type: <input onKeyUp={ updateType } placeholder="override product type here..." /></label>
304
- </div>
305
- <Product prehooks={ prehooks } type={ productType } />
306
- </div>
307
- );
308
- };
309
- App.displayName = 'App';
310
-
311
- export default App;
312
-
313
- <i><u>index.js</i></b>
314
-
315
- import React from 'react';
316
- import ReactDOM from 'react-dom';
317
- import App from './app';
318
- ReactDOM.render(<App />, document.getElementById('root'));
319
-
320
- ## License
321
-
322
- 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 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>(): import("react").Context<Store<T_1>>;
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 {import("react").FC<{
8
- * children?: import("react").ReactNode,
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: import("react").FC<{
16
- children?: import("react").ReactNode;
17
- context: ObservableContext<T>;
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> = import("react").Context<Store<T>>;
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
- * @type {(state: T) => PartialState<T>}
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 {import("react").FC<{
103
- * children?: import("react").ReactNode,
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 = (0, _react.useRef)(prehooks);
130
+ var prehooksRef = usePrehooksRef(prehooks);
118
131
  var initialState = (0, _react.useRef)(value);
119
132
 
120
- /** @type {[T, Function]} */
133
+ /** @type {[Set<Listener<T>>, Function]} */
121
134
  var _useState = (0, _react.useState)(function () {
122
- return (0, _lodash["default"])(value);
135
+ return new Set();
123
136
  }),
124
137
  _useState2 = _slicedToArray(_useState, 1),
125
- state = _useState2[0];
126
- /** @type {[Set<Listener<T>>, Function]} */
138
+ listeners = _useState2[0];
139
+ /** @type {[T, Function]} */
127
140
  var _useState3 = (0, _react.useState)(function () {
128
- return new Set();
141
+ return (0, _lodash["default"])(value);
129
142
  }),
130
143
  _useState4 = _slicedToArray(_useState3, 1),
131
- listeners = _useState4[0];
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
- var _useState5 = (0, _react.useState)(function () {
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
- StoreContext = _useState8[0];
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 {import("react").Context<Store<T>>} ObservableContext
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.3"
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
+ }