@civet/core 0.6.8 → 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/AbortSignal.js +48 -5
- package/lib/ConfigProvider.js +9 -13
- package/lib/{DataStore.js → DataProvider.js} +111 -54
- package/lib/Meta.js +4 -1
- package/lib/Resource.js +22 -382
- package/lib/context.js +4 -8
- package/lib/createPlugin.js +8 -8
- package/lib/index.js +7 -15
- package/lib/useResource.js +265 -0
- package/package.json +5 -5
- package/src/AbortSignal.js +44 -5
- package/src/ConfigProvider.js +5 -5
- package/src/DataProvider.js +182 -0
- package/src/Meta.js +4 -1
- package/src/Resource.js +15 -247
- package/src/context.js +3 -3
- package/src/createPlugin.js +6 -6
- package/src/index.js +1 -2
- package/src/useResource.js +208 -0
- package/lib/DefaultDataStore.js +0 -93
- package/src/DataStore.js +0 -141
- package/src/DefaultDataStore.js +0 -38
package/src/Resource.js
CHANGED
|
@@ -1,236 +1,19 @@
|
|
|
1
|
-
import deepEquals from 'fast-deep-equal';
|
|
2
1
|
import PropTypes from 'prop-types';
|
|
3
|
-
import React
|
|
2
|
+
import React from 'react';
|
|
4
3
|
|
|
5
|
-
import
|
|
6
|
-
import {
|
|
7
|
-
import
|
|
8
|
-
import Meta from './Meta';
|
|
9
|
-
import uniqueIdentifier from './uniqueIdentifier';
|
|
10
|
-
|
|
11
|
-
function getComparator({ name, ids, query, empty, options }) {
|
|
12
|
-
return {
|
|
13
|
-
name,
|
|
14
|
-
ids,
|
|
15
|
-
query,
|
|
16
|
-
empty,
|
|
17
|
-
options,
|
|
18
|
-
};
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
function compareRequests(dataStore, prevComparator, nextComparator) {
|
|
22
|
-
if (dataStore != null) return dataStore.compareRequests(prevComparator, nextComparator);
|
|
23
|
-
return deepEquals(prevComparator, nextComparator);
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
function getEmptyValue({ name, ids, query, options, dataStore }, request, revision, empty) {
|
|
27
|
-
return {
|
|
28
|
-
name,
|
|
29
|
-
ids,
|
|
30
|
-
query,
|
|
31
|
-
options,
|
|
32
|
-
dataStore,
|
|
33
|
-
request,
|
|
34
|
-
revision,
|
|
35
|
-
data: [],
|
|
36
|
-
meta: {},
|
|
37
|
-
error: undefined,
|
|
38
|
-
isEmpty: empty,
|
|
39
|
-
isIncomplete: !empty,
|
|
40
|
-
isInitial: !empty,
|
|
41
|
-
};
|
|
42
|
-
}
|
|
4
|
+
import { ResourceContext } from './context';
|
|
5
|
+
import { dataProviderPropType } from './DataProvider';
|
|
6
|
+
import useResource from './useResource';
|
|
43
7
|
|
|
44
8
|
/**
|
|
45
|
-
* Makes data from an
|
|
9
|
+
* Makes data from an DataProvider available to its descendants using React's context API.
|
|
46
10
|
* If not explicitly specified, necessary configuration is taken from the nearest <ConfigProvider>.
|
|
11
|
+
* The provided DataProvider must not be replaced.
|
|
47
12
|
*/
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
const { empty, dataStore: ds, persistent } = nextProps;
|
|
51
|
-
const _ = getComparator(nextProps);
|
|
52
|
-
if (prevState.ds !== ds || !compareRequests(ds, prevState._, _)) {
|
|
53
|
-
const request = uniqueIdentifier(prevState.request);
|
|
54
|
-
const revision = uniqueIdentifier(prevState.revision);
|
|
55
|
-
const isEmpty = ds == null || empty;
|
|
56
|
-
const nextState = {
|
|
57
|
-
_,
|
|
58
|
-
ds,
|
|
59
|
-
request,
|
|
60
|
-
revision,
|
|
61
|
-
isLoading: !isEmpty,
|
|
62
|
-
persistent,
|
|
63
|
-
};
|
|
64
|
-
let isPersistent;
|
|
65
|
-
if (
|
|
66
|
-
prevState.value.meta.persistent === 'very' ||
|
|
67
|
-
(persistent === 'very' && prevState.persistent === 'very')
|
|
68
|
-
) {
|
|
69
|
-
isPersistent = 'very';
|
|
70
|
-
} else if (prevState.value.meta.persistent || (persistent && prevState.persistent)) {
|
|
71
|
-
isPersistent = true;
|
|
72
|
-
}
|
|
73
|
-
if (
|
|
74
|
-
prevState.ds == null ||
|
|
75
|
-
prevState.ds !== ds ||
|
|
76
|
-
isEmpty ||
|
|
77
|
-
!isPersistent ||
|
|
78
|
-
(isPersistent !== 'very' && prevState._.name !== _.name)
|
|
79
|
-
) {
|
|
80
|
-
nextState.value = getEmptyValue(nextProps, request, revision, isEmpty);
|
|
81
|
-
}
|
|
82
|
-
return nextState;
|
|
83
|
-
}
|
|
84
|
-
if (prevState.persistent !== persistent) {
|
|
85
|
-
return { persistent };
|
|
86
|
-
}
|
|
87
|
-
return null;
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
constructor(props) {
|
|
91
|
-
super(props);
|
|
92
|
-
const { empty, dataStore: ds, persistent } = props;
|
|
93
|
-
const request = uniqueIdentifier();
|
|
94
|
-
const revision = uniqueIdentifier();
|
|
95
|
-
const isEmpty = ds == null || empty;
|
|
96
|
-
this.state = {
|
|
97
|
-
_: getComparator(props),
|
|
98
|
-
ds,
|
|
99
|
-
request,
|
|
100
|
-
revision,
|
|
101
|
-
isLoading: !isEmpty,
|
|
102
|
-
value: getEmptyValue(props, request, revision, isEmpty),
|
|
103
|
-
persistent,
|
|
104
|
-
};
|
|
105
|
-
}
|
|
106
|
-
|
|
107
|
-
componentDidMount() {
|
|
108
|
-
const { name, empty, dataStore } = this.props;
|
|
109
|
-
const { request, revision } = this.state;
|
|
110
|
-
if (dataStore == null || empty) return;
|
|
111
|
-
this.unsubscribe = dataStore.subscribe(name, this.notify);
|
|
112
|
-
this.fetch(request, revision);
|
|
113
|
-
}
|
|
114
|
-
|
|
115
|
-
componentDidUpdate(prevProps, prevState) {
|
|
116
|
-
const { name, empty, dataStore } = this.props;
|
|
117
|
-
const { request, revision } = this.state;
|
|
118
|
-
if (prevState.request !== request || prevState.revision !== revision) {
|
|
119
|
-
if (this.abortSignal) this.abortSignal.abort();
|
|
120
|
-
this.abortSignal = undefined;
|
|
121
|
-
}
|
|
122
|
-
if (dataStore == null || prevProps.dataStore !== dataStore || prevProps.name !== name) {
|
|
123
|
-
if (this.unsubscribe) this.unsubscribe();
|
|
124
|
-
this.unsubscribe = undefined;
|
|
125
|
-
if (dataStore == null || empty) return;
|
|
126
|
-
this.unsubscribe = dataStore.subscribe(name, this.notify);
|
|
127
|
-
}
|
|
128
|
-
if ((prevState.request !== request || prevState.revision !== revision) && !empty) {
|
|
129
|
-
this.fetch(request, revision);
|
|
130
|
-
}
|
|
131
|
-
}
|
|
132
|
-
|
|
133
|
-
componentWillUnmount() {
|
|
134
|
-
this.isUnmounted = true;
|
|
135
|
-
if (this.unsubscribe) this.unsubscribe();
|
|
136
|
-
if (this.abortSignal) this.abortSignal.abort();
|
|
137
|
-
}
|
|
138
|
-
|
|
139
|
-
notify = async () =>
|
|
140
|
-
new Promise((resolve) => {
|
|
141
|
-
this.setState((currentState) => {
|
|
142
|
-
if (currentState.ds == null || currentState.empty) return null;
|
|
143
|
-
const nextRevision = uniqueIdentifier(currentState.revision);
|
|
144
|
-
resolve({ request: currentState.request, revision: nextRevision });
|
|
145
|
-
return {
|
|
146
|
-
isLoading: true,
|
|
147
|
-
revision: nextRevision,
|
|
148
|
-
};
|
|
149
|
-
});
|
|
150
|
-
});
|
|
151
|
-
|
|
152
|
-
fetch(request, revision) {
|
|
153
|
-
const { ...props } = this.props;
|
|
154
|
-
const { name, ids, query, options, dataStore } = props;
|
|
155
|
-
const { value } = this.state;
|
|
13
|
+
function Resource({ children, ...props }) {
|
|
14
|
+
const context = useResource(props);
|
|
156
15
|
|
|
157
|
-
|
|
158
|
-
return;
|
|
159
|
-
}
|
|
160
|
-
|
|
161
|
-
const meta = new Meta({ ...value.meta });
|
|
162
|
-
const signal = new AbortSignal();
|
|
163
|
-
this.abortSignal = signal;
|
|
164
|
-
|
|
165
|
-
const callback = (error, done, data) => {
|
|
166
|
-
if (this.isUnmounted) return;
|
|
167
|
-
this.setState((currentState) => {
|
|
168
|
-
if (request !== currentState.request || revision !== currentState.revision) return null;
|
|
169
|
-
|
|
170
|
-
if (error != null) {
|
|
171
|
-
return {
|
|
172
|
-
isLoading: false,
|
|
173
|
-
value: {
|
|
174
|
-
...currentState.value,
|
|
175
|
-
error,
|
|
176
|
-
isIncomplete: false,
|
|
177
|
-
},
|
|
178
|
-
};
|
|
179
|
-
}
|
|
180
|
-
|
|
181
|
-
const { data: prevData, ...prevContext } = currentState.value;
|
|
182
|
-
const { data: emptyData, ...emptyValue } = getEmptyValue(props, request, revision, false);
|
|
183
|
-
const context = {
|
|
184
|
-
...emptyValue,
|
|
185
|
-
meta: meta.commit(prevContext.meta),
|
|
186
|
-
isIncomplete: !done,
|
|
187
|
-
isInitial: currentState.isInitial && !done,
|
|
188
|
-
};
|
|
189
|
-
|
|
190
|
-
return {
|
|
191
|
-
isLoading: !done,
|
|
192
|
-
value: {
|
|
193
|
-
...context,
|
|
194
|
-
data: dataStore.recycleItems(
|
|
195
|
-
dataStore.transition(data, prevData, context, prevContext),
|
|
196
|
-
prevData,
|
|
197
|
-
context,
|
|
198
|
-
prevContext,
|
|
199
|
-
),
|
|
200
|
-
},
|
|
201
|
-
};
|
|
202
|
-
});
|
|
203
|
-
};
|
|
204
|
-
|
|
205
|
-
dataStore.continuousGet(name, ids, query, options, meta, callback, signal);
|
|
206
|
-
}
|
|
207
|
-
|
|
208
|
-
render() {
|
|
209
|
-
const { children, ...rest } = this.props;
|
|
210
|
-
const { request, isLoading, value } = this.state;
|
|
211
|
-
if (value.dataStore == null) return null;
|
|
212
|
-
const contextValue = {
|
|
213
|
-
...value,
|
|
214
|
-
isLoading,
|
|
215
|
-
isStale: request !== value.request,
|
|
216
|
-
notify: this.notify,
|
|
217
|
-
};
|
|
218
|
-
const plugins = Array.isArray(value.dataStore.resourcePlugins)
|
|
219
|
-
? value.dataStore.resourcePlugins
|
|
220
|
-
: [];
|
|
221
|
-
const renderProvider = (context) => (
|
|
222
|
-
<ResourceContext.Provider value={context}>{children}</ResourceContext.Provider>
|
|
223
|
-
);
|
|
224
|
-
return plugins.reduceRight((next, Plugin) => {
|
|
225
|
-
if (Plugin == null) return next;
|
|
226
|
-
return (context) => (
|
|
227
|
-
// eslint-disable-next-line react/jsx-props-no-spreading
|
|
228
|
-
<Plugin {...rest} context={context}>
|
|
229
|
-
{next}
|
|
230
|
-
</Plugin>
|
|
231
|
-
);
|
|
232
|
-
}, renderProvider)(contextValue);
|
|
233
|
-
}
|
|
16
|
+
return <ResourceContext.Provider value={context}>{children}</ResourceContext.Provider>;
|
|
234
17
|
}
|
|
235
18
|
|
|
236
19
|
Resource.propTypes = {
|
|
@@ -239,11 +22,7 @@ Resource.propTypes = {
|
|
|
239
22
|
*/
|
|
240
23
|
name: PropTypes.string.isRequired,
|
|
241
24
|
/**
|
|
242
|
-
*
|
|
243
|
-
*/
|
|
244
|
-
ids: PropTypes.array,
|
|
245
|
-
/**
|
|
246
|
-
* Query filter (in place of ids)
|
|
25
|
+
* Query
|
|
247
26
|
*/
|
|
248
27
|
query: PropTypes.any,
|
|
249
28
|
/**
|
|
@@ -251,29 +30,18 @@ Resource.propTypes = {
|
|
|
251
30
|
*/
|
|
252
31
|
empty: PropTypes.bool,
|
|
253
32
|
/**
|
|
254
|
-
*
|
|
33
|
+
* DataProvider options for requests
|
|
255
34
|
*/
|
|
256
35
|
options: PropTypes.object,
|
|
257
36
|
/**
|
|
258
|
-
*
|
|
37
|
+
* DataProvider to be used for requests
|
|
259
38
|
*/
|
|
260
|
-
|
|
39
|
+
dataProvider: dataProviderPropType.isRequired,
|
|
261
40
|
/**
|
|
262
|
-
* 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"
|
|
263
42
|
*/
|
|
264
43
|
persistent: PropTypes.oneOfType([PropTypes.bool, PropTypes.oneOf(['very'])]),
|
|
265
44
|
children: PropTypes.node,
|
|
266
45
|
};
|
|
267
46
|
|
|
268
|
-
|
|
269
|
-
const withContext = (ChildComponent) => {
|
|
270
|
-
const WithContext = (props) => (
|
|
271
|
-
<ConfigContext.Consumer>
|
|
272
|
-
{({ dataStore }) => <ChildComponent dataStore={dataStore} {...props} />}
|
|
273
|
-
</ConfigContext.Consumer>
|
|
274
|
-
);
|
|
275
|
-
return WithContext;
|
|
276
|
-
};
|
|
277
|
-
/* eslint-enable react/jsx-props-no-spreading */
|
|
278
|
-
|
|
279
|
-
export default withContext(Resource);
|
|
47
|
+
export default Resource;
|
package/src/context.js
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
|
-
import React
|
|
1
|
+
import React from 'react';
|
|
2
2
|
|
|
3
3
|
const noop = () => {};
|
|
4
4
|
|
|
5
5
|
export const ConfigContext = React.createContext({});
|
|
6
6
|
ConfigContext.displayName = 'ConfigContext';
|
|
7
|
-
export const useConfigContext = () => useContext(ConfigContext);
|
|
7
|
+
export const useConfigContext = () => React.useContext(ConfigContext);
|
|
8
8
|
|
|
9
9
|
export const ResourceContext = React.createContext({ data: [], notify: noop });
|
|
10
10
|
ResourceContext.displayName = 'ResourceContext';
|
|
11
|
-
export const useResourceContext = () => useContext(ResourceContext);
|
|
11
|
+
export const useResourceContext = () => React.useContext(ResourceContext);
|
package/src/createPlugin.js
CHANGED
|
@@ -1,18 +1,18 @@
|
|
|
1
|
-
import
|
|
1
|
+
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(
|
|
5
|
+
return (dataProviderClass) => {
|
|
6
|
+
if (!Object.prototype.isPrototypeOf.call(DataProvider, dataProviderClass)) {
|
|
7
7
|
// eslint-disable-next-line no-console
|
|
8
8
|
console.error(
|
|
9
|
-
'A plugin should be given a derivative of the
|
|
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
|
-
'A plugin is expected to return a derivative of the
|
|
15
|
+
'A plugin is expected to return a derivative of the DataProvider class but returned nothing',
|
|
16
16
|
);
|
|
17
17
|
}
|
|
18
18
|
return result;
|
package/src/index.js
CHANGED
|
@@ -7,8 +7,7 @@ export { default as ChannelNotifier } from './ChannelNotifier';
|
|
|
7
7
|
export { default as compose } from './compose';
|
|
8
8
|
export { default as ConfigProvider } from './ConfigProvider';
|
|
9
9
|
export { default as createPlugin } from './createPlugin';
|
|
10
|
-
export {
|
|
11
|
-
export { default as DataStore } from './DefaultDataStore';
|
|
10
|
+
export { dataProviderPropType, default as DataProvider, isDataProvider } from './DataProvider';
|
|
12
11
|
export { default as Meta } from './Meta';
|
|
13
12
|
export { default as Notifier } from './Notifier';
|
|
14
13
|
export { default as Resource } from './Resource';
|
|
@@ -0,0 +1,208 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
|
|
3
|
+
import AbortSignal from './AbortSignal';
|
|
4
|
+
import { useConfigContext } from './context';
|
|
5
|
+
import Meta from './Meta';
|
|
6
|
+
import uniqueIdentifier from './uniqueIdentifier';
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Makes data from an DataProvider available.
|
|
10
|
+
* If not explicitly specified, necessary configuration is taken from the nearest <ConfigProvider>.
|
|
11
|
+
* The provided DataProvider must not be replaced.
|
|
12
|
+
*/
|
|
13
|
+
function useResource(props) {
|
|
14
|
+
const {
|
|
15
|
+
name,
|
|
16
|
+
query,
|
|
17
|
+
empty,
|
|
18
|
+
options,
|
|
19
|
+
dataProvider: dataProviderProp,
|
|
20
|
+
persistent,
|
|
21
|
+
...rest
|
|
22
|
+
} = props;
|
|
23
|
+
|
|
24
|
+
const configContext = useConfigContext();
|
|
25
|
+
const currentDataProvider = dataProviderProp || configContext.dataProvider;
|
|
26
|
+
const dataProvider = React.useMemo(() => currentDataProvider, []);
|
|
27
|
+
if (dataProvider == null) {
|
|
28
|
+
throw new Error(
|
|
29
|
+
'Unmet requirement: The DataProvider for the useResource hook is missing - Check your ConfigContext provider and the dataProvider property',
|
|
30
|
+
);
|
|
31
|
+
}
|
|
32
|
+
if (dataProvider !== currentDataProvider) {
|
|
33
|
+
throw new Error(
|
|
34
|
+
'Constant violation: The DataProvider provided to the useResource hook must not be replaced - Check your ConfigContext provider and the dataProvider property',
|
|
35
|
+
);
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
const comparator = { name, query, empty, options };
|
|
39
|
+
const [state, setState] = React.useState(() => {
|
|
40
|
+
const request = uniqueIdentifier();
|
|
41
|
+
const revision = uniqueIdentifier();
|
|
42
|
+
return {
|
|
43
|
+
comparator,
|
|
44
|
+
request,
|
|
45
|
+
revision,
|
|
46
|
+
isLoading: !empty,
|
|
47
|
+
value: {
|
|
48
|
+
name,
|
|
49
|
+
query,
|
|
50
|
+
options,
|
|
51
|
+
request,
|
|
52
|
+
revision,
|
|
53
|
+
data: [],
|
|
54
|
+
meta: {},
|
|
55
|
+
error: undefined,
|
|
56
|
+
isEmpty: empty,
|
|
57
|
+
isIncomplete: !empty,
|
|
58
|
+
isInitial: !empty,
|
|
59
|
+
},
|
|
60
|
+
persistent,
|
|
61
|
+
};
|
|
62
|
+
});
|
|
63
|
+
const {
|
|
64
|
+
comparator: prevComparator,
|
|
65
|
+
request,
|
|
66
|
+
revision,
|
|
67
|
+
isLoading,
|
|
68
|
+
value,
|
|
69
|
+
persistent: prevPersistent,
|
|
70
|
+
} = state;
|
|
71
|
+
|
|
72
|
+
if (prevComparator !== comparator && !dataProvider.compareRequests(prevComparator, comparator)) {
|
|
73
|
+
setState((prevState) => {
|
|
74
|
+
const nextRequest = uniqueIdentifier(prevState.request);
|
|
75
|
+
const nextRevision = uniqueIdentifier(prevState.revision);
|
|
76
|
+
let isPersistent;
|
|
77
|
+
if (
|
|
78
|
+
prevState.value.meta.persistent === 'very' ||
|
|
79
|
+
(persistent === 'very' && prevState.persistent === 'very')
|
|
80
|
+
) {
|
|
81
|
+
isPersistent = 'very';
|
|
82
|
+
} else if (prevState.value.meta.persistent || (persistent && prevState.persistent)) {
|
|
83
|
+
isPersistent = true;
|
|
84
|
+
}
|
|
85
|
+
const shouldValuePersist =
|
|
86
|
+
!empty &&
|
|
87
|
+
isPersistent &&
|
|
88
|
+
(isPersistent === 'very' || prevState.comparator.name === comparator.name);
|
|
89
|
+
return {
|
|
90
|
+
comparator,
|
|
91
|
+
request: nextRequest,
|
|
92
|
+
revision: nextRevision,
|
|
93
|
+
isLoading: !empty,
|
|
94
|
+
value: shouldValuePersist
|
|
95
|
+
? prevState.value
|
|
96
|
+
: {
|
|
97
|
+
name,
|
|
98
|
+
query,
|
|
99
|
+
options,
|
|
100
|
+
request: nextRequest,
|
|
101
|
+
revision: nextRevision,
|
|
102
|
+
data: [],
|
|
103
|
+
meta: {},
|
|
104
|
+
error: undefined,
|
|
105
|
+
isEmpty: empty,
|
|
106
|
+
isIncomplete: !empty,
|
|
107
|
+
isInitial: !empty,
|
|
108
|
+
},
|
|
109
|
+
persistent,
|
|
110
|
+
};
|
|
111
|
+
});
|
|
112
|
+
} else if (prevPersistent !== persistent) {
|
|
113
|
+
setState((prevState) => ({ ...prevState, persistent }));
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
const notify = React.useCallback(
|
|
117
|
+
async () =>
|
|
118
|
+
new Promise((resolve) => {
|
|
119
|
+
setState((currentState) => {
|
|
120
|
+
if (currentState.empty) return currentState;
|
|
121
|
+
const nextRevision = uniqueIdentifier(currentState.revision);
|
|
122
|
+
resolve({ request: currentState.request, revision: nextRevision });
|
|
123
|
+
return { ...currentState, isLoading: true, revision: nextRevision };
|
|
124
|
+
});
|
|
125
|
+
}),
|
|
126
|
+
[],
|
|
127
|
+
);
|
|
128
|
+
|
|
129
|
+
// DataProvider events
|
|
130
|
+
React.useEffect(() => {
|
|
131
|
+
if (empty) return undefined;
|
|
132
|
+
|
|
133
|
+
const unsubscribe = dataProvider.subscribe(name, notify);
|
|
134
|
+
return unsubscribe;
|
|
135
|
+
}, [!empty, dataProvider, name, notify]);
|
|
136
|
+
|
|
137
|
+
React.useEffect(() => {
|
|
138
|
+
if (empty) return undefined;
|
|
139
|
+
|
|
140
|
+
const abortSignal = new AbortSignal();
|
|
141
|
+
|
|
142
|
+
const meta = new Meta({ ...value.meta });
|
|
143
|
+
|
|
144
|
+
const callback = (error, done, data) => {
|
|
145
|
+
setState((prevState) => {
|
|
146
|
+
if (request !== prevState.request || revision !== prevState.revision) return prevState;
|
|
147
|
+
|
|
148
|
+
if (error != null) {
|
|
149
|
+
return {
|
|
150
|
+
...prevState,
|
|
151
|
+
isLoading: false,
|
|
152
|
+
value: {
|
|
153
|
+
...prevState.value,
|
|
154
|
+
error,
|
|
155
|
+
isIncomplete: false,
|
|
156
|
+
},
|
|
157
|
+
};
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
const { data: prevData, ...prevContext } = prevState.value;
|
|
161
|
+
const context = {
|
|
162
|
+
name,
|
|
163
|
+
query,
|
|
164
|
+
options,
|
|
165
|
+
request,
|
|
166
|
+
revision,
|
|
167
|
+
meta: meta.commit(prevContext.meta),
|
|
168
|
+
error: undefined,
|
|
169
|
+
isEmpty: false,
|
|
170
|
+
isIncomplete: !done,
|
|
171
|
+
isInitial: prevState.isInitial && !done,
|
|
172
|
+
};
|
|
173
|
+
|
|
174
|
+
return {
|
|
175
|
+
isLoading: !done,
|
|
176
|
+
value: {
|
|
177
|
+
...context,
|
|
178
|
+
data: dataProvider.recycleItems(
|
|
179
|
+
dataProvider.transition(data, prevData, context, prevContext),
|
|
180
|
+
prevData,
|
|
181
|
+
context,
|
|
182
|
+
prevContext,
|
|
183
|
+
),
|
|
184
|
+
},
|
|
185
|
+
};
|
|
186
|
+
});
|
|
187
|
+
};
|
|
188
|
+
|
|
189
|
+
dataProvider.continuousGet(name, query, options, meta, callback, abortSignal);
|
|
190
|
+
|
|
191
|
+
return () => {
|
|
192
|
+
abortSignal.abort();
|
|
193
|
+
};
|
|
194
|
+
}, [request, revision]);
|
|
195
|
+
|
|
196
|
+
const isStale = request !== value.request;
|
|
197
|
+
const context = React.useMemo(
|
|
198
|
+
() => ({ ...value, dataProvider, isLoading, isStale, notify }),
|
|
199
|
+
[value, dataProvider, isLoading, isStale, notify],
|
|
200
|
+
);
|
|
201
|
+
|
|
202
|
+
const contextPlugins = Array.isArray(dataProvider.contextPlugins)
|
|
203
|
+
? dataProvider.contextPlugins
|
|
204
|
+
: [];
|
|
205
|
+
return contextPlugins.reduce((result, fn) => fn(result, rest), context);
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
export default useResource;
|
package/lib/DefaultDataStore.js
DELETED
|
@@ -1,93 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
function _typeof(obj) { "@babel/helpers - typeof"; if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }
|
|
4
|
-
|
|
5
|
-
Object.defineProperty(exports, "__esModule", {
|
|
6
|
-
value: true
|
|
7
|
-
});
|
|
8
|
-
exports["default"] = void 0;
|
|
9
|
-
|
|
10
|
-
var _objectHash = _interopRequireDefault(require("object-hash"));
|
|
11
|
-
|
|
12
|
-
var _DataStore = _interopRequireDefault(require("./DataStore"));
|
|
13
|
-
|
|
14
|
-
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
|
|
15
|
-
|
|
16
|
-
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
|
|
17
|
-
|
|
18
|
-
function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }
|
|
19
|
-
|
|
20
|
-
function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }
|
|
21
|
-
|
|
22
|
-
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); if (superClass) _setPrototypeOf(subClass, superClass); }
|
|
23
|
-
|
|
24
|
-
function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }
|
|
25
|
-
|
|
26
|
-
function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = _getPrototypeOf(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = _getPrototypeOf(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return _possibleConstructorReturn(this, result); }; }
|
|
27
|
-
|
|
28
|
-
function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } else if (call !== void 0) { throw new TypeError("Derived constructors may only return object or undefined"); } return _assertThisInitialized(self); }
|
|
29
|
-
|
|
30
|
-
function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }
|
|
31
|
-
|
|
32
|
-
function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); return true; } catch (e) { return false; } }
|
|
33
|
-
|
|
34
|
-
function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }
|
|
35
|
-
|
|
36
|
-
var DefaultDataStore = /*#__PURE__*/function (_BaseDataStore) {
|
|
37
|
-
_inherits(DefaultDataStore, _BaseDataStore);
|
|
38
|
-
|
|
39
|
-
var _super = _createSuper(DefaultDataStore);
|
|
40
|
-
|
|
41
|
-
function DefaultDataStore() {
|
|
42
|
-
_classCallCheck(this, DefaultDataStore);
|
|
43
|
-
|
|
44
|
-
return _super.apply(this, arguments);
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
_createClass(DefaultDataStore, [{
|
|
48
|
-
key: "recycleItemsUniqueIdentifier",
|
|
49
|
-
value: function recycleItemsUniqueIdentifier(item) {
|
|
50
|
-
return (0, _objectHash["default"])(item);
|
|
51
|
-
}
|
|
52
|
-
}, {
|
|
53
|
-
key: "recycleItemsIsUnchanged",
|
|
54
|
-
value: function recycleItemsIsUnchanged() {
|
|
55
|
-
return true;
|
|
56
|
-
}
|
|
57
|
-
}, {
|
|
58
|
-
key: "recycleItems",
|
|
59
|
-
value: function recycleItems(nextData, prevData) {
|
|
60
|
-
var _this = this;
|
|
61
|
-
|
|
62
|
-
var prevMapping = {};
|
|
63
|
-
prevData.forEach(function (item) {
|
|
64
|
-
var id = _this.recycleItemsUniqueIdentifier(item);
|
|
65
|
-
|
|
66
|
-
if (id != null) prevMapping[id] = item;
|
|
67
|
-
});
|
|
68
|
-
var result = nextData.map(function (nextItem) {
|
|
69
|
-
var id = _this.recycleItemsUniqueIdentifier(nextItem);
|
|
70
|
-
|
|
71
|
-
if (id != null && Object.prototype.hasOwnProperty.call(prevMapping, id)) {
|
|
72
|
-
var prevItem = prevMapping[id];
|
|
73
|
-
if (_this.recycleItemsIsUnchanged(nextItem, prevItem)) return prevItem;
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
return nextItem;
|
|
77
|
-
});
|
|
78
|
-
|
|
79
|
-
if (prevData.length === result.length && result.reduce(function (sum, item, i) {
|
|
80
|
-
return sum && Object.is(prevData[i], item);
|
|
81
|
-
}, true)) {
|
|
82
|
-
return prevData;
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
return result;
|
|
86
|
-
}
|
|
87
|
-
}]);
|
|
88
|
-
|
|
89
|
-
return DefaultDataStore;
|
|
90
|
-
}(_DataStore["default"]);
|
|
91
|
-
|
|
92
|
-
var _default = DefaultDataStore;
|
|
93
|
-
exports["default"] = _default;
|