@webkrafters/react-observable-context 0.0.2 → 0.0.3
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 +29 -17
- package/dist/index.d.ts +16 -8
- package/dist/index.js +26 -23
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -10,13 +10,13 @@ npm install --save @webkrafters/react-hoc-memo
|
|
|
10
10
|
|
|
11
11
|
## API
|
|
12
12
|
|
|
13
|
-
The React-Observable-Context package exports only **2** modules namely: the **
|
|
13
|
+
The React-Observable-Context package exports only **2** modules namely: the **createContext** method and the **Provider** component.
|
|
14
14
|
|
|
15
|
-
|
|
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
16
|
|
|
17
|
-
The `
|
|
17
|
+
The `Provider` can immediately be used as-is anywhere the React-Observable-Context is required. Supply the context to its `context` prop and the initial state to its `value` prop as is customary to React::Provider.
|
|
18
18
|
|
|
19
|
-
The context `store` exposes **4** methods for interacting with the context namely:
|
|
19
|
+
The context's `store` exposes **4** methods for interacting with the context's internal state namely:
|
|
20
20
|
|
|
21
21
|
* **getState**: (selector?: (state: State) => any) => any
|
|
22
22
|
|
|
@@ -28,14 +28,21 @@ The context `store` exposes **4** methods for interacting with the context namel
|
|
|
28
28
|
|
|
29
29
|
## Usage
|
|
30
30
|
|
|
31
|
+
<i><u>context.js</u></i>
|
|
32
|
+
|
|
33
|
+
import { createContext } from '@webkrafters/react-observable-context';
|
|
34
|
+
const ObservableContext = createContext();
|
|
35
|
+
export default ObservableContext;
|
|
36
|
+
|
|
31
37
|
<i><u>tally-display.js</u></i>
|
|
32
38
|
|
|
33
|
-
import React, { useEffect, useState } from 'react';
|
|
34
|
-
|
|
39
|
+
import React, { useContext, useEffect, useState } from 'react';
|
|
40
|
+
|
|
41
|
+
import ObservableContext from './context';
|
|
35
42
|
|
|
36
43
|
const TallyDisplay = () => {
|
|
37
44
|
|
|
38
|
-
const { getState, subscribe } =
|
|
45
|
+
const { getState, subscribe } = useContext( ObservableContext );
|
|
39
46
|
|
|
40
47
|
const [ , setUpdateTs ] = useState();
|
|
41
48
|
|
|
@@ -62,12 +69,13 @@ The context `store` exposes **4** methods for interacting with the context namel
|
|
|
62
69
|
|
|
63
70
|
<i><u>editor.js</u></i>
|
|
64
71
|
|
|
65
|
-
import React, { useCallback, useEffect, useRef } from 'react';
|
|
66
|
-
|
|
72
|
+
import React, { useCallback, useContext, useEffect, useRef } from 'react';
|
|
73
|
+
|
|
74
|
+
import ObservableContext from './context';
|
|
67
75
|
|
|
68
76
|
const Editor = () => {
|
|
69
77
|
|
|
70
|
-
const { setState } =
|
|
78
|
+
const { setState } = useContext( ObservableContext );
|
|
71
79
|
|
|
72
80
|
const priceInputRef = useRef();
|
|
73
81
|
const colorInputRef = useRef();
|
|
@@ -114,12 +122,13 @@ The context `store` exposes **4** methods for interacting with the context namel
|
|
|
114
122
|
|
|
115
123
|
<i><u>product-description.js</u></i>
|
|
116
124
|
|
|
117
|
-
import React, { useEffect, useState } from 'react';
|
|
118
|
-
|
|
125
|
+
import React, { useContext, useEffect, useState } from 'react';
|
|
126
|
+
|
|
127
|
+
import ObservableContext from './context';
|
|
119
128
|
|
|
120
129
|
const ProductDescription = () => {
|
|
121
130
|
|
|
122
|
-
const store =
|
|
131
|
+
const store = useContext( ObservableContext );
|
|
123
132
|
|
|
124
133
|
const [ , setUpdateTs ] = useState();
|
|
125
134
|
|
|
@@ -145,12 +154,13 @@ The context `store` exposes **4** methods for interacting with the context namel
|
|
|
145
154
|
|
|
146
155
|
<i><u>price-sticker.js</u></i>
|
|
147
156
|
|
|
148
|
-
import React, { useEffect, useState } from 'react'
|
|
149
|
-
|
|
157
|
+
import React, { useContext, useEffect, useState } from 'react';
|
|
158
|
+
|
|
159
|
+
import ObservableContext from './context';
|
|
150
160
|
|
|
151
161
|
const PriceSticker = () => {
|
|
152
162
|
|
|
153
|
-
const store =
|
|
163
|
+
const store = useContext( ObservableContext );
|
|
154
164
|
|
|
155
165
|
const [ price, setPrice ] = useState(() => store.getState( s => s.price ));
|
|
156
166
|
|
|
@@ -174,6 +184,8 @@ The context `store` exposes **4** methods for interacting with the context namel
|
|
|
174
184
|
|
|
175
185
|
import React, { useCallback, useEffect, useState } from 'react';
|
|
176
186
|
import { Provider } from '@webkrafters/react-observable-context';
|
|
187
|
+
|
|
188
|
+
import ObservableContext from './context';
|
|
177
189
|
|
|
178
190
|
import Editor from './editor';
|
|
179
191
|
import PriceSticker from './price-sticker';
|
|
@@ -200,7 +212,7 @@ The context `store` exposes **4** methods for interacting with the context namel
|
|
|
200
212
|
<div style={{ marginBottom: 10 }}>
|
|
201
213
|
<label>$ <input onKeyUp={ overridePricing } placeholder="override price here..."/></label>
|
|
202
214
|
</div>
|
|
203
|
-
<Provider value={ state }>
|
|
215
|
+
<Provider context={ ObservableContext } value={ state }>
|
|
204
216
|
<div style={{
|
|
205
217
|
borderBottom: '1px solid #333',
|
|
206
218
|
marginBottom: 10,
|
package/dist/index.d.ts
CHANGED
|
@@ -1,22 +1,30 @@
|
|
|
1
1
|
export class UsageError extends Error {
|
|
2
2
|
}
|
|
3
|
-
export function
|
|
3
|
+
export function createContext<T extends State>(): import("react").Context<Store<T>>;
|
|
4
4
|
/**
|
|
5
|
-
* @type {import("react").
|
|
5
|
+
* @type {import("react").FC<{
|
|
6
|
+
* children?: import("react").ReactNode,
|
|
7
|
+
* context: ObservableContext<T>,
|
|
8
|
+
* value: T
|
|
9
|
+
* }>}
|
|
6
10
|
* @template {State} T
|
|
7
11
|
*/
|
|
8
|
-
export const Provider: import("react").
|
|
9
|
-
|
|
12
|
+
export const Provider: import("react").FC<{
|
|
13
|
+
children?: import("react").ReactNode;
|
|
14
|
+
context: import("react").Context<Store<T>>;
|
|
15
|
+
value: T;
|
|
16
|
+
}>;
|
|
17
|
+
export type ObservableContext<T extends State> = import("react").Context<Store<T>>;
|
|
10
18
|
export type OptionalTask<F = void> = F extends void ? () => never : F;
|
|
11
|
-
export type Listener<
|
|
19
|
+
export type Listener<T extends State> = (newValue: PartialState<T>, oldValue: PartialState<T>) => void;
|
|
12
20
|
export type State = {
|
|
13
21
|
[x: string]: any;
|
|
14
22
|
};
|
|
15
|
-
export type PartialState<
|
|
23
|
+
export type PartialState<T extends State> = {
|
|
16
24
|
[x: string]: any;
|
|
17
25
|
} & { [K in keyof T]?: T[K]; };
|
|
18
|
-
export type Selector<
|
|
19
|
-
export type Store<
|
|
26
|
+
export type Selector<T extends State> = (state: T) => any;
|
|
27
|
+
export type Store<T extends State> = {
|
|
20
28
|
getState: (selector?: Selector<T>) => any;
|
|
21
29
|
resetState: OptionalTask<VoidFunction>;
|
|
22
30
|
setState: (changes: PartialState<T>) => void;
|
package/dist/index.js
CHANGED
|
@@ -4,7 +4,7 @@ function _typeof(obj) { "@babel/helpers - typeof"; return _typeof = "function" =
|
|
|
4
4
|
Object.defineProperty(exports, "__esModule", {
|
|
5
5
|
value: true
|
|
6
6
|
});
|
|
7
|
-
exports.
|
|
7
|
+
exports.createContext = exports.UsageError = exports.Provider = void 0;
|
|
8
8
|
var _react = _interopRequireWildcard(require("react"));
|
|
9
9
|
var _lodash = _interopRequireDefault(require("lodash.isempty"));
|
|
10
10
|
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
|
|
@@ -53,40 +53,43 @@ var defaultSelector = function defaultSelector(state) {
|
|
|
53
53
|
};
|
|
54
54
|
|
|
55
55
|
/**
|
|
56
|
-
* @
|
|
56
|
+
* @returns {ObservableContext<T>}
|
|
57
57
|
* @template {State} T
|
|
58
58
|
*/
|
|
59
|
-
var
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
/**
|
|
67
|
-
* @return {Store<T>}
|
|
68
|
-
* @template {State} T
|
|
69
|
-
*/
|
|
70
|
-
var useStore = function useStore() {
|
|
71
|
-
return (0, _react.useContext)(StoreContext);
|
|
59
|
+
var createContext = function createContext() {
|
|
60
|
+
return /*#__PURE__*/(0, _react.createContext)({
|
|
61
|
+
getState: reportNonReactUsage,
|
|
62
|
+
resetState: reportNonReactUsage,
|
|
63
|
+
setState: reportNonReactUsage,
|
|
64
|
+
subscribe: reportNonReactUsage
|
|
65
|
+
});
|
|
72
66
|
};
|
|
73
67
|
|
|
74
68
|
/**
|
|
75
|
-
* @type {import("react").
|
|
69
|
+
* @type {import("react").FC<{
|
|
70
|
+
* children?: import("react").ReactNode,
|
|
71
|
+
* context: ObservableContext<T>,
|
|
72
|
+
* value: T
|
|
73
|
+
* }>}
|
|
76
74
|
* @template {State} T
|
|
77
75
|
*/
|
|
78
|
-
exports.
|
|
76
|
+
exports.createContext = createContext;
|
|
79
77
|
var Provider = function Provider(_ref) {
|
|
80
78
|
var _ref$children = _ref.children,
|
|
81
79
|
children = _ref$children === void 0 ? null : _ref$children,
|
|
80
|
+
context = _ref.context,
|
|
82
81
|
value = _ref.value;
|
|
83
82
|
var valueRef = (0, _react.useRef)(value);
|
|
83
|
+
/** @type {ObservableContext<T>} */
|
|
84
|
+
var _useState = (0, _react.useState)(context),
|
|
85
|
+
_useState2 = _slicedToArray(_useState, 1),
|
|
86
|
+
StoreContext = _useState2[0];
|
|
84
87
|
/** @type {[Set<Listener<T>>, Function]} */
|
|
85
|
-
var
|
|
88
|
+
var _useState3 = (0, _react.useState)(function () {
|
|
86
89
|
return new Set();
|
|
87
90
|
}),
|
|
88
|
-
|
|
89
|
-
listeners =
|
|
91
|
+
_useState4 = _slicedToArray(_useState3, 1),
|
|
92
|
+
listeners = _useState4[0];
|
|
90
93
|
/** @type {Listener<T>} */
|
|
91
94
|
var onChange = function onChange(newValue, oldValue) {
|
|
92
95
|
return listeners.forEach(function (listener) {
|
|
@@ -129,7 +132,7 @@ var Provider = function Provider(_ref) {
|
|
|
129
132
|
return setState(value);
|
|
130
133
|
}, [value]);
|
|
131
134
|
/** @type {[Store<T>, Function]} */
|
|
132
|
-
var
|
|
135
|
+
var _useState5 = (0, _react.useState)(function () {
|
|
133
136
|
return {
|
|
134
137
|
getState: getState,
|
|
135
138
|
resetState: resetState,
|
|
@@ -137,8 +140,8 @@ var Provider = function Provider(_ref) {
|
|
|
137
140
|
subscribe: subscribe
|
|
138
141
|
};
|
|
139
142
|
}),
|
|
140
|
-
|
|
141
|
-
store =
|
|
143
|
+
_useState6 = _slicedToArray(_useState5, 1),
|
|
144
|
+
store = _useState6[0];
|
|
142
145
|
return /*#__PURE__*/_react["default"].createElement(StoreContext.Provider, {
|
|
143
146
|
value: store
|
|
144
147
|
}, children);
|
package/package.json
CHANGED