@civet/core 1.0.0-rc1 → 1.0.0-rc2
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/lib/ConfigProvider.js +4 -4
- package/lib/DataProvider.js +2 -2
- package/lib/Resource.js +2 -2
- package/lib/createPlugin.js +5 -5
- package/lib/useResource.js +17 -17
- package/package.json +1 -1
- package/src/ConfigProvider.js +3 -3
- package/src/DataProvider.js +1 -1
- package/src/Resource.js +2 -2
- package/src/createPlugin.js +3 -3
- package/src/useResource.js +26 -16
package/lib/ConfigProvider.js
CHANGED
|
@@ -19,14 +19,14 @@ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "d
|
|
|
19
19
|
* Provides general configuration to its descendants using React's context API.
|
|
20
20
|
*/
|
|
21
21
|
function ConfigProvider(_ref) {
|
|
22
|
-
var
|
|
22
|
+
var dataProvider = _ref.dataProvider,
|
|
23
23
|
children = _ref.children;
|
|
24
24
|
|
|
25
25
|
var context = _react["default"].useMemo(function () {
|
|
26
26
|
return {
|
|
27
|
-
|
|
27
|
+
dataProvider: dataProvider
|
|
28
28
|
};
|
|
29
|
-
}, [
|
|
29
|
+
}, [dataProvider]);
|
|
30
30
|
|
|
31
31
|
return /*#__PURE__*/_react["default"].createElement(_context.ConfigContext.Provider, {
|
|
32
32
|
value: context
|
|
@@ -34,7 +34,7 @@ function ConfigProvider(_ref) {
|
|
|
34
34
|
}
|
|
35
35
|
|
|
36
36
|
ConfigProvider.propTypes = {
|
|
37
|
-
|
|
37
|
+
dataProvider: _DataProvider.dataProviderPropType,
|
|
38
38
|
children: _propTypes["default"].node
|
|
39
39
|
};
|
|
40
40
|
var _default = ConfigProvider;
|
package/lib/DataProvider.js
CHANGED
|
@@ -234,8 +234,8 @@ var DataProvider = /*#__PURE__*/function () {
|
|
|
234
234
|
return DataProvider;
|
|
235
235
|
}();
|
|
236
236
|
|
|
237
|
-
var isDataProvider = function isDataProvider(
|
|
238
|
-
return
|
|
237
|
+
var isDataProvider = function isDataProvider(dataProvider) {
|
|
238
|
+
return dataProvider instanceof DataProvider;
|
|
239
239
|
};
|
|
240
240
|
|
|
241
241
|
exports.isDataProvider = isDataProvider;
|
package/lib/Resource.js
CHANGED
|
@@ -62,10 +62,10 @@ Resource.propTypes = {
|
|
|
62
62
|
/**
|
|
63
63
|
* DataProvider to be used for requests
|
|
64
64
|
*/
|
|
65
|
-
|
|
65
|
+
dataProvider: _DataProvider.dataProviderPropType.isRequired,
|
|
66
66
|
|
|
67
67
|
/**
|
|
68
|
-
* Whether stale data should be retained during the next request - this only applies if neither
|
|
68
|
+
* Whether stale data should be retained during the next request - this only applies if neither dataProvider nor name have changed, unless set to "very"
|
|
69
69
|
*/
|
|
70
70
|
persistent: _propTypes["default"].oneOfType([_propTypes["default"].bool, _propTypes["default"].oneOf(['very'])]),
|
|
71
71
|
children: _propTypes["default"].node
|
package/lib/createPlugin.js
CHANGED
|
@@ -29,14 +29,14 @@ function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.g
|
|
|
29
29
|
|
|
30
30
|
function createPlugin(plugin) {
|
|
31
31
|
if (typeof plugin !== 'function') throw new Error('No valid plugin definition specified');
|
|
32
|
-
return function (
|
|
33
|
-
if (!Object.prototype.isPrototypeOf.call(_DataProvider["default"],
|
|
32
|
+
return function (dataProviderClass) {
|
|
33
|
+
if (!Object.prototype.isPrototypeOf.call(_DataProvider["default"], dataProviderClass)) {
|
|
34
34
|
// eslint-disable-next-line no-console
|
|
35
35
|
console.error('A plugin should be given a derivative of the DataProvider class as its first parameter');
|
|
36
36
|
}
|
|
37
37
|
|
|
38
|
-
var result = plugin( /*#__PURE__*/function (
|
|
39
|
-
_inherits(_class,
|
|
38
|
+
var result = plugin( /*#__PURE__*/function (_dataProviderClass) {
|
|
39
|
+
_inherits(_class, _dataProviderClass);
|
|
40
40
|
|
|
41
41
|
var _super = _createSuper(_class);
|
|
42
42
|
|
|
@@ -47,7 +47,7 @@ function createPlugin(plugin) {
|
|
|
47
47
|
}
|
|
48
48
|
|
|
49
49
|
return _class;
|
|
50
|
-
}(
|
|
50
|
+
}(dataProviderClass));
|
|
51
51
|
|
|
52
52
|
if (result == null) {
|
|
53
53
|
throw new Error('A plugin is expected to return a derivative of the DataProvider class but returned nothing');
|
package/lib/useResource.js
CHANGED
|
@@ -15,7 +15,7 @@ var _Meta = _interopRequireDefault(require("./Meta"));
|
|
|
15
15
|
|
|
16
16
|
var _uniqueIdentifier = _interopRequireDefault(require("./uniqueIdentifier"));
|
|
17
17
|
|
|
18
|
-
var _excluded = ["name", "query", "empty", "options", "
|
|
18
|
+
var _excluded = ["name", "query", "empty", "options", "dataProvider", "persistent"],
|
|
19
19
|
_excluded2 = ["data"];
|
|
20
20
|
|
|
21
21
|
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
|
|
@@ -56,23 +56,23 @@ function useResource(props) {
|
|
|
56
56
|
query = props.query,
|
|
57
57
|
empty = props.empty,
|
|
58
58
|
options = props.options,
|
|
59
|
-
|
|
59
|
+
dataProviderProp = props.dataProvider,
|
|
60
60
|
persistent = props.persistent,
|
|
61
61
|
rest = _objectWithoutProperties(props, _excluded);
|
|
62
62
|
|
|
63
63
|
var configContext = (0, _context2.useConfigContext)();
|
|
64
|
-
var
|
|
64
|
+
var currentDataProvider = dataProviderProp || configContext.dataProvider;
|
|
65
65
|
|
|
66
|
-
var
|
|
67
|
-
return
|
|
66
|
+
var dataProvider = _react["default"].useMemo(function () {
|
|
67
|
+
return currentDataProvider;
|
|
68
68
|
}, []);
|
|
69
69
|
|
|
70
|
-
if (
|
|
71
|
-
throw new Error('Unmet requirement: The DataProvider for the useResource hook is missing - Check your ConfigContext and the
|
|
70
|
+
if (dataProvider == null) {
|
|
71
|
+
throw new Error('Unmet requirement: The DataProvider for the useResource hook is missing - Check your ConfigContext provider and the dataProvider property');
|
|
72
72
|
}
|
|
73
73
|
|
|
74
|
-
if (
|
|
75
|
-
throw new Error('Constant violation: The DataProvider provided to the useResource hook must not be replaced - Check your ConfigContext and the
|
|
74
|
+
if (dataProvider !== currentDataProvider) {
|
|
75
|
+
throw new Error('Constant violation: The DataProvider provided to the useResource hook must not be replaced - Check your ConfigContext provider and the dataProvider property');
|
|
76
76
|
}
|
|
77
77
|
|
|
78
78
|
var comparator = {
|
|
@@ -117,7 +117,7 @@ function useResource(props) {
|
|
|
117
117
|
value = state.value,
|
|
118
118
|
prevPersistent = state.persistent;
|
|
119
119
|
|
|
120
|
-
if (prevComparator !== comparator && !
|
|
120
|
+
if (prevComparator !== comparator && !dataProvider.compareRequests(prevComparator, comparator)) {
|
|
121
121
|
setState(function (prevState) {
|
|
122
122
|
var nextRequest = (0, _uniqueIdentifier["default"])(prevState.request);
|
|
123
123
|
var nextRevision = (0, _uniqueIdentifier["default"])(prevState.revision);
|
|
@@ -190,9 +190,9 @@ function useResource(props) {
|
|
|
190
190
|
|
|
191
191
|
_react["default"].useEffect(function () {
|
|
192
192
|
if (empty) return undefined;
|
|
193
|
-
var unsubscribe =
|
|
193
|
+
var unsubscribe = dataProvider.subscribe(name, notify);
|
|
194
194
|
return unsubscribe;
|
|
195
|
-
}, [!empty,
|
|
195
|
+
}, [!empty, dataProvider, name, notify]);
|
|
196
196
|
|
|
197
197
|
_react["default"].useEffect(function () {
|
|
198
198
|
if (empty) return undefined;
|
|
@@ -232,13 +232,13 @@ function useResource(props) {
|
|
|
232
232
|
return {
|
|
233
233
|
isLoading: !done,
|
|
234
234
|
value: _objectSpread(_objectSpread({}, context), {}, {
|
|
235
|
-
data:
|
|
235
|
+
data: dataProvider.recycleItems(dataProvider.transition(data, prevData, context, prevContext), prevData, context, prevContext)
|
|
236
236
|
})
|
|
237
237
|
};
|
|
238
238
|
});
|
|
239
239
|
};
|
|
240
240
|
|
|
241
|
-
|
|
241
|
+
dataProvider.continuousGet(name, query, options, meta, callback, abortSignal);
|
|
242
242
|
return function () {
|
|
243
243
|
abortSignal.abort();
|
|
244
244
|
};
|
|
@@ -248,14 +248,14 @@ function useResource(props) {
|
|
|
248
248
|
|
|
249
249
|
var context = _react["default"].useMemo(function () {
|
|
250
250
|
return _objectSpread(_objectSpread({}, value), {}, {
|
|
251
|
-
|
|
251
|
+
dataProvider: dataProvider,
|
|
252
252
|
isLoading: isLoading,
|
|
253
253
|
isStale: isStale,
|
|
254
254
|
notify: notify
|
|
255
255
|
});
|
|
256
|
-
}, [value,
|
|
256
|
+
}, [value, dataProvider, isLoading, isStale, notify]);
|
|
257
257
|
|
|
258
|
-
var contextPlugins = Array.isArray(
|
|
258
|
+
var contextPlugins = Array.isArray(dataProvider.contextPlugins) ? dataProvider.contextPlugins : [];
|
|
259
259
|
return contextPlugins.reduce(function (result, fn) {
|
|
260
260
|
return fn(result, rest);
|
|
261
261
|
}, context);
|
package/package.json
CHANGED
package/src/ConfigProvider.js
CHANGED
|
@@ -7,14 +7,14 @@ import { dataProviderPropType } from './DataProvider';
|
|
|
7
7
|
/**
|
|
8
8
|
* Provides general configuration to its descendants using React's context API.
|
|
9
9
|
*/
|
|
10
|
-
function ConfigProvider({
|
|
11
|
-
const context = React.useMemo(() => ({
|
|
10
|
+
function ConfigProvider({ dataProvider, children }) {
|
|
11
|
+
const context = React.useMemo(() => ({ dataProvider }), [dataProvider]);
|
|
12
12
|
|
|
13
13
|
return <ConfigContext.Provider value={context}>{children}</ConfigContext.Provider>;
|
|
14
14
|
}
|
|
15
15
|
|
|
16
16
|
ConfigProvider.propTypes = {
|
|
17
|
-
|
|
17
|
+
dataProvider: dataProviderPropType,
|
|
18
18
|
children: PropTypes.node,
|
|
19
19
|
};
|
|
20
20
|
|
package/src/DataProvider.js
CHANGED
|
@@ -174,7 +174,7 @@ class DataProvider {
|
|
|
174
174
|
}
|
|
175
175
|
}
|
|
176
176
|
|
|
177
|
-
const isDataProvider = (
|
|
177
|
+
const isDataProvider = (dataProvider) => dataProvider instanceof DataProvider;
|
|
178
178
|
|
|
179
179
|
const dataProviderPropType = PropTypes.instanceOf(DataProvider);
|
|
180
180
|
|
package/src/Resource.js
CHANGED
|
@@ -36,9 +36,9 @@ Resource.propTypes = {
|
|
|
36
36
|
/**
|
|
37
37
|
* DataProvider to be used for requests
|
|
38
38
|
*/
|
|
39
|
-
|
|
39
|
+
dataProvider: dataProviderPropType.isRequired,
|
|
40
40
|
/**
|
|
41
|
-
* Whether stale data should be retained during the next request - this only applies if neither
|
|
41
|
+
* Whether stale data should be retained during the next request - this only applies if neither dataProvider nor name have changed, unless set to "very"
|
|
42
42
|
*/
|
|
43
43
|
persistent: PropTypes.oneOfType([PropTypes.bool, PropTypes.oneOf(['very'])]),
|
|
44
44
|
children: PropTypes.node,
|
package/src/createPlugin.js
CHANGED
|
@@ -2,14 +2,14 @@ import DataProvider from './DataProvider';
|
|
|
2
2
|
|
|
3
3
|
function createPlugin(plugin) {
|
|
4
4
|
if (typeof plugin !== 'function') throw new Error('No valid plugin definition specified');
|
|
5
|
-
return (
|
|
6
|
-
if (!Object.prototype.isPrototypeOf.call(DataProvider,
|
|
5
|
+
return (dataProviderClass) => {
|
|
6
|
+
if (!Object.prototype.isPrototypeOf.call(DataProvider, dataProviderClass)) {
|
|
7
7
|
// eslint-disable-next-line no-console
|
|
8
8
|
console.error(
|
|
9
9
|
'A plugin should be given a derivative of the DataProvider class as its first parameter',
|
|
10
10
|
);
|
|
11
11
|
}
|
|
12
|
-
const result = plugin(class extends
|
|
12
|
+
const result = plugin(class extends dataProviderClass {});
|
|
13
13
|
if (result == null) {
|
|
14
14
|
throw new Error(
|
|
15
15
|
'A plugin is expected to return a derivative of the DataProvider class but returned nothing',
|
package/src/useResource.js
CHANGED
|
@@ -11,19 +11,27 @@ import uniqueIdentifier from './uniqueIdentifier';
|
|
|
11
11
|
* The provided DataProvider must not be replaced.
|
|
12
12
|
*/
|
|
13
13
|
function useResource(props) {
|
|
14
|
-
const {
|
|
14
|
+
const {
|
|
15
|
+
name,
|
|
16
|
+
query,
|
|
17
|
+
empty,
|
|
18
|
+
options,
|
|
19
|
+
dataProvider: dataProviderProp,
|
|
20
|
+
persistent,
|
|
21
|
+
...rest
|
|
22
|
+
} = props;
|
|
15
23
|
|
|
16
24
|
const configContext = useConfigContext();
|
|
17
|
-
const
|
|
18
|
-
const
|
|
19
|
-
if (
|
|
25
|
+
const currentDataProvider = dataProviderProp || configContext.dataProvider;
|
|
26
|
+
const dataProvider = React.useMemo(() => currentDataProvider, []);
|
|
27
|
+
if (dataProvider == null) {
|
|
20
28
|
throw new Error(
|
|
21
|
-
'Unmet requirement: The DataProvider for the useResource hook is missing - Check your ConfigContext and the
|
|
29
|
+
'Unmet requirement: The DataProvider for the useResource hook is missing - Check your ConfigContext provider and the dataProvider property',
|
|
22
30
|
);
|
|
23
31
|
}
|
|
24
|
-
if (
|
|
32
|
+
if (dataProvider !== currentDataProvider) {
|
|
25
33
|
throw new Error(
|
|
26
|
-
'Constant violation: The DataProvider provided to the useResource hook must not be replaced - Check your ConfigContext and the
|
|
34
|
+
'Constant violation: The DataProvider provided to the useResource hook must not be replaced - Check your ConfigContext provider and the dataProvider property',
|
|
27
35
|
);
|
|
28
36
|
}
|
|
29
37
|
|
|
@@ -61,7 +69,7 @@ function useResource(props) {
|
|
|
61
69
|
persistent: prevPersistent,
|
|
62
70
|
} = state;
|
|
63
71
|
|
|
64
|
-
if (prevComparator !== comparator && !
|
|
72
|
+
if (prevComparator !== comparator && !dataProvider.compareRequests(prevComparator, comparator)) {
|
|
65
73
|
setState((prevState) => {
|
|
66
74
|
const nextRequest = uniqueIdentifier(prevState.request);
|
|
67
75
|
const nextRevision = uniqueIdentifier(prevState.revision);
|
|
@@ -122,9 +130,9 @@ function useResource(props) {
|
|
|
122
130
|
React.useEffect(() => {
|
|
123
131
|
if (empty) return undefined;
|
|
124
132
|
|
|
125
|
-
const unsubscribe =
|
|
133
|
+
const unsubscribe = dataProvider.subscribe(name, notify);
|
|
126
134
|
return unsubscribe;
|
|
127
|
-
}, [!empty,
|
|
135
|
+
}, [!empty, dataProvider, name, notify]);
|
|
128
136
|
|
|
129
137
|
React.useEffect(() => {
|
|
130
138
|
if (empty) return undefined;
|
|
@@ -167,8 +175,8 @@ function useResource(props) {
|
|
|
167
175
|
isLoading: !done,
|
|
168
176
|
value: {
|
|
169
177
|
...context,
|
|
170
|
-
data:
|
|
171
|
-
|
|
178
|
+
data: dataProvider.recycleItems(
|
|
179
|
+
dataProvider.transition(data, prevData, context, prevContext),
|
|
172
180
|
prevData,
|
|
173
181
|
context,
|
|
174
182
|
prevContext,
|
|
@@ -178,7 +186,7 @@ function useResource(props) {
|
|
|
178
186
|
});
|
|
179
187
|
};
|
|
180
188
|
|
|
181
|
-
|
|
189
|
+
dataProvider.continuousGet(name, query, options, meta, callback, abortSignal);
|
|
182
190
|
|
|
183
191
|
return () => {
|
|
184
192
|
abortSignal.abort();
|
|
@@ -187,11 +195,13 @@ function useResource(props) {
|
|
|
187
195
|
|
|
188
196
|
const isStale = request !== value.request;
|
|
189
197
|
const context = React.useMemo(
|
|
190
|
-
() => ({ ...value,
|
|
191
|
-
[value,
|
|
198
|
+
() => ({ ...value, dataProvider, isLoading, isStale, notify }),
|
|
199
|
+
[value, dataProvider, isLoading, isStale, notify],
|
|
192
200
|
);
|
|
193
201
|
|
|
194
|
-
const contextPlugins = Array.isArray(
|
|
202
|
+
const contextPlugins = Array.isArray(dataProvider.contextPlugins)
|
|
203
|
+
? dataProvider.contextPlugins
|
|
204
|
+
: [];
|
|
195
205
|
return contextPlugins.reduce((result, fn) => fn(result, rest), context);
|
|
196
206
|
}
|
|
197
207
|
|