@data-client/vue 0.18.1 → 0.18.2
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/CHANGELOG.md +371 -0
- package/README.md +86 -41
- package/dist/index.js +223 -158
- package/dist/index.umd.min.js +1 -1
- package/lib/consumers/refetchTriggers.d.ts +12 -0
- package/lib/consumers/refetchTriggers.d.ts.map +1 -0
- package/lib/consumers/refetchTriggers.js +9 -0
- package/lib/consumers/useCache.d.ts +1 -1
- package/lib/consumers/useCache.d.ts.map +1 -1
- package/lib/consumers/useCache.js +3 -53
- package/lib/consumers/useCacheResponse.d.ts +20 -0
- package/lib/consumers/useCacheResponse.d.ts.map +1 -0
- package/lib/consumers/useCacheResponse.js +55 -0
- package/lib/consumers/useCountRef.d.ts +7 -0
- package/lib/consumers/useCountRef.d.ts.map +1 -0
- package/lib/consumers/useCountRef.js +10 -0
- package/lib/consumers/useDLE.d.ts +1 -1
- package/lib/consumers/useDLE.d.ts.map +1 -1
- package/lib/consumers/useDLE.js +16 -62
- package/lib/consumers/useDebounce.d.ts +1 -1
- package/lib/consumers/useDebounce.d.ts.map +1 -1
- package/lib/consumers/useDebounce.js +1 -2
- package/lib/consumers/useExpired.d.ts +12 -0
- package/lib/consumers/useExpired.d.ts.map +1 -0
- package/lib/consumers/useExpired.js +19 -0
- package/lib/consumers/useFetch.d.ts +8 -11
- package/lib/consumers/useFetch.d.ts.map +1 -1
- package/lib/consumers/useFetch.js +13 -24
- package/lib/consumers/useLive.js +1 -1
- package/lib/consumers/useLoading.d.ts +1 -1
- package/lib/consumers/useLoading.d.ts.map +1 -1
- package/lib/consumers/useLoading.js +1 -2
- package/lib/consumers/useQuery.d.ts +1 -1
- package/lib/consumers/useQuery.d.ts.map +1 -1
- package/lib/consumers/useQuery.js +6 -12
- package/lib/consumers/useResponseMeta.d.ts +23 -0
- package/lib/consumers/useResponseMeta.d.ts.map +1 -0
- package/lib/consumers/useResponseMeta.js +36 -0
- package/lib/consumers/useSubscription.js +4 -4
- package/lib/consumers/useSuspense.d.ts +1 -1
- package/lib/consumers/useSuspense.d.ts.map +1 -1
- package/lib/consumers/useSuspense.js +58 -34
- package/lib/context.d.ts +1 -1
- package/lib/context.d.ts.map +1 -1
- package/lib/context.js +3 -3
- package/lib/index.d.ts +2 -1
- package/lib/index.d.ts.map +1 -1
- package/lib/index.js +4 -2
- package/lib/internal.d.ts +6 -0
- package/lib/internal.d.ts.map +1 -0
- package/lib/internal.js +6 -0
- package/lib/providers/DataClientPlugin.d.ts +1 -1
- package/lib/providers/DataClientPlugin.d.ts.map +1 -1
- package/lib/providers/DataClientPlugin.js +16 -4
- package/lib/providers/createDataClient.d.ts +10 -4
- package/lib/providers/createDataClient.d.ts.map +1 -1
- package/lib/providers/createDataClient.js +11 -5
- package/lib/providers/getDefaultManagers.d.ts.map +1 -1
- package/lib/providers/getDefaultManagers.js +1 -1
- package/lib/providers/index.d.ts +2 -1
- package/lib/providers/index.d.ts.map +1 -1
- package/lib/providers/index.js +1 -1
- package/lib/test/MockPlugin.d.ts +1 -1
- package/lib/test/MockPlugin.d.ts.map +1 -1
- package/lib/test/MockPlugin.js +1 -1
- package/lib/test/index.d.ts +2 -1
- package/lib/test/index.d.ts.map +1 -1
- package/lib/test/index.js +1 -1
- package/lib/test/mountDataClient.d.ts +5 -3
- package/lib/test/mountDataClient.d.ts.map +1 -1
- package/lib/test/mountDataClient.js +15 -10
- package/lib/test/renderDataCompose.d.ts +10 -4
- package/lib/test/renderDataCompose.d.ts.map +1 -1
- package/lib/test/renderDataCompose.js +2 -3
- package/package.json +20 -5
- package/src/__tests__/__snapshots__/context.test.ts.snap +2 -2
- package/src/__tests__/context.test.ts +1 -1
- package/src/__tests__/integration-garbage-collection.web.ts +80 -4
- package/src/__tests__/useCache.web.ts +160 -93
- package/src/__tests__/useDLE.web.ts +204 -96
- package/src/__tests__/useFetch.web.ts +100 -37
- package/src/__tests__/useQuery.web.ts +60 -50
- package/src/__tests__/useSubscription.web.ts +74 -75
- package/src/__tests__/useSuspense.web.ts +455 -46
- package/src/consumers/refetchTriggers.ts +22 -0
- package/src/consumers/useCache.ts +7 -70
- package/src/consumers/useCacheResponse.ts +56 -0
- package/src/consumers/useCountRef.ts +15 -0
- package/src/consumers/useDLE.ts +24 -83
- package/src/consumers/useDebounce.ts +2 -1
- package/src/consumers/useExpired.ts +26 -0
- package/src/consumers/useFetch.ts +34 -49
- package/src/consumers/useLive.ts +1 -1
- package/src/consumers/useLoading.ts +2 -1
- package/src/consumers/useQuery.ts +10 -13
- package/src/consumers/useResponseMeta.ts +41 -0
- package/src/consumers/useSubscription.ts +3 -3
- package/src/consumers/useSuspense.ts +66 -58
- package/src/context.ts +4 -3
- package/src/index.ts +7 -1
- package/src/internal.ts +12 -0
- package/src/providers/DataClientPlugin.ts +17 -3
- package/src/providers/__tests__/DataClientPlugin.test.ts +40 -1
- package/src/providers/createDataClient.ts +15 -5
- package/src/providers/getDefaultManagers.ts +2 -6
- package/src/providers/index.ts +2 -5
- package/src/test/MockPlugin.ts +3 -3
- package/src/test/README.md +10 -304
- package/src/test/__tests__/mountDataClient.test.ts +38 -0
- package/src/test/index.ts +2 -1
- package/src/test/mountDataClient.ts +22 -17
- package/src/test/renderDataCompose.ts +11 -7
package/dist/index.js
CHANGED
|
@@ -3,6 +3,30 @@
|
|
|
3
3
|
var core = require('@data-client/core');
|
|
4
4
|
var vue = require('vue');
|
|
5
5
|
|
|
6
|
+
const initialState = core.__INTERNAL__.initialState;
|
|
7
|
+
const INVALID = core.__INTERNAL__.INVALID;
|
|
8
|
+
const MemoCache = core.__INTERNAL__.MemoCache;
|
|
9
|
+
|
|
10
|
+
var internal = /*#__PURE__*/Object.freeze({
|
|
11
|
+
__proto__: null,
|
|
12
|
+
INVALID: INVALID,
|
|
13
|
+
MemoCache: MemoCache,
|
|
14
|
+
actions: core.actions,
|
|
15
|
+
applyManager: core.applyManager,
|
|
16
|
+
createReducer: core.createReducer,
|
|
17
|
+
initManager: core.initManager,
|
|
18
|
+
initialState: initialState
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
/** Watch sources whose changes may require a refetch.
|
|
22
|
+
*
|
|
23
|
+
* Multi-source form so Vue compares each value; a getter returning a new array
|
|
24
|
+
* would fire on every store update and refetch stale data unexpectedly.
|
|
25
|
+
*/
|
|
26
|
+
function refetchTriggers(responseMeta, stateRef, argsKey) {
|
|
27
|
+
return [() => responseMeta.value.expiresAt, () => responseMeta.value.expiryStatus, () => stateRef.value.lastReset, argsKey];
|
|
28
|
+
}
|
|
29
|
+
|
|
6
30
|
const StateKey = Symbol('DataClientState');
|
|
7
31
|
const ControllerKey = Symbol('DataClientController');
|
|
8
32
|
|
|
@@ -13,7 +37,7 @@ function useController() {
|
|
|
13
37
|
if (!ctrl) {
|
|
14
38
|
/* istanbul ignore else */
|
|
15
39
|
if (process.env.NODE_ENV !== 'production') {
|
|
16
|
-
console.error('Reactive Data Client (Vue) composables dependency injection failed. Either you are missing the DataClientPlugin or you are using composables outside of script setup.\n' + 'Adding DataClientPlugin: https://dataclient.io/
|
|
40
|
+
console.error('Reactive Data Client (Vue) composables dependency injection failed. Either you are missing the DataClientPlugin or you are using composables outside of script setup.\n' + 'Adding DataClientPlugin: https://dataclient.io/vue/api/DataClientPlugin\n' + 'Comosables only work in script setup: https://vuejs.org/guide/reusability/composables.html#usage-restrictions');
|
|
17
41
|
}
|
|
18
42
|
return new core.Controller();
|
|
19
43
|
}
|
|
@@ -24,66 +48,129 @@ function injectState() {
|
|
|
24
48
|
if (!state) {
|
|
25
49
|
/* istanbul ignore else */
|
|
26
50
|
if (process.env.NODE_ENV !== 'production') {
|
|
27
|
-
console.error('Reactive Data Client (Vue) composables dependency injection failed. Either you are missing the DataClientPlugin or you are using composables outside of script setup.\n' + 'Adding DataClientPlugin: https://dataclient.io/
|
|
51
|
+
console.error('Reactive Data Client (Vue) composables dependency injection failed. Either you are missing the DataClientPlugin or you are using composables outside of script setup.\n' + 'Adding DataClientPlugin: https://dataclient.io/vue/api/DataClientPlugin\n' + 'Comosables only work in script setup: https://vuejs.org/guide/reusability/composables.html#usage-restrictions');
|
|
28
52
|
}
|
|
29
53
|
return FallbackStateRef;
|
|
30
54
|
}
|
|
31
55
|
return state;
|
|
32
56
|
}
|
|
33
57
|
|
|
34
|
-
/**
|
|
35
|
-
|
|
36
|
-
|
|
58
|
+
/** Keeps a store response's entities from being garbage collected while mounted. */
|
|
59
|
+
function useCountRef(meta) {
|
|
60
|
+
vue.watch(() => meta.value.data, (_newVal, _oldVal, onCleanup) => {
|
|
61
|
+
onCleanup(meta.value.countRef());
|
|
62
|
+
}, {
|
|
63
|
+
immediate: true
|
|
64
|
+
});
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/** Reactive store response for an endpoint and its (possibly reactive) args.
|
|
37
68
|
*
|
|
38
|
-
*
|
|
39
|
-
* @throws {Promise} If data is not yet available.
|
|
40
|
-
* @throws {NetworkError} If fetch fails.
|
|
69
|
+
* Keeps the response's entities from being garbage collected while mounted.
|
|
41
70
|
*/
|
|
42
|
-
|
|
43
|
-
async function useSuspense(endpoint, ...args) {
|
|
71
|
+
function useResponseMeta(endpoint, args) {
|
|
44
72
|
const stateRef = injectState();
|
|
45
73
|
const controller = useController();
|
|
46
74
|
|
|
47
|
-
// Track top-level reactive args (
|
|
48
|
-
const resolvedArgs = vue.computed(() => args.map(a => vue.
|
|
75
|
+
// Track top-level reactive args (refs and getters are resolved). This allows props/refs/getters to trigger updates.
|
|
76
|
+
const resolvedArgs = vue.computed(() => args.map(a => vue.toValue(a)));
|
|
49
77
|
|
|
50
78
|
// Compute a key that changes when args change (including reactive props)
|
|
51
79
|
const argsKey = vue.computed(() => resolvedArgs.value[0] !== null ? endpoint.key(...resolvedArgs.value) : '');
|
|
52
80
|
|
|
53
81
|
// Compute response meta reactively so we can respond to store updates
|
|
54
|
-
const responseMeta = vue.computed(() =>
|
|
55
|
-
|
|
56
|
-
|
|
82
|
+
const responseMeta = vue.computed(() => controller.getResponseMeta(endpoint, ...resolvedArgs.value, stateRef.value));
|
|
83
|
+
useCountRef(responseMeta);
|
|
84
|
+
return {
|
|
85
|
+
controller,
|
|
86
|
+
stateRef,
|
|
87
|
+
resolvedArgs,
|
|
88
|
+
argsKey,
|
|
89
|
+
responseMeta
|
|
90
|
+
};
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
/** Whether a response needs fetching; hard invalid data must refetch regardless of staleness */
|
|
94
|
+
function isStale(meta) {
|
|
95
|
+
return meta.expiryStatus === core.ExpiryStatus.Invalid || Date.now() > meta.expiresAt;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
/**
|
|
99
|
+
* Ensure an endpoint is available.
|
|
100
|
+
* Suspends until it is.
|
|
101
|
+
*
|
|
102
|
+
* @see https://dataclient.io/docs/api/useSuspense
|
|
103
|
+
* @throws {Promise} If data is not yet available.
|
|
104
|
+
* @throws {NetworkError} If fetch fails.
|
|
105
|
+
*/
|
|
106
|
+
|
|
107
|
+
async function useSuspense(endpoint, ...args) {
|
|
108
|
+
const {
|
|
109
|
+
controller,
|
|
110
|
+
stateRef,
|
|
111
|
+
resolvedArgs,
|
|
112
|
+
argsKey,
|
|
113
|
+
responseMeta
|
|
114
|
+
} = useResponseMeta(endpoint, args);
|
|
115
|
+
|
|
116
|
+
// key of the fetch in flight; staleness alone isn't reactive (time passes without a
|
|
117
|
+
// refetch), so only hold previous data while a fetch is actually running
|
|
118
|
+
const fetchingKey = vue.shallowRef('');
|
|
57
119
|
const maybeFetch = async () => {
|
|
58
120
|
const currentKey = argsKey.value;
|
|
59
121
|
if (!currentKey) return;
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
122
|
+
if (!isStale(responseMeta.value)) return;
|
|
123
|
+
fetchingKey.value = currentKey;
|
|
124
|
+
try {
|
|
125
|
+
await controller.fetch(endpoint, ...resolvedArgs.value);
|
|
126
|
+
} finally {
|
|
127
|
+
// store updates synchronously before fetch resolves, so data is ready here
|
|
128
|
+
if (fetchingKey.value === currentKey) fetchingKey.value = '';
|
|
129
|
+
}
|
|
64
130
|
};
|
|
65
131
|
|
|
66
132
|
// Watch for changes to key, expiry, or store state that require refetch
|
|
67
|
-
vue.watch(() => {
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
}, () => {
|
|
71
|
-
return maybeFetch();
|
|
133
|
+
vue.watch(refetchTriggers(responseMeta, stateRef, argsKey), () => {
|
|
134
|
+
// errors are stored and surfaced through the returned ref
|
|
135
|
+
maybeFetch().catch(() => {});
|
|
72
136
|
});
|
|
73
137
|
|
|
74
|
-
//
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
138
|
+
// Like React, fully "valid" data never suspends, even when stale: show it and refetch in
|
|
139
|
+
// the background. Only missing or invalid data waits for the fetch.
|
|
140
|
+
const suspend = responseMeta.value.expiryStatus !== core.ExpiryStatus.Valid;
|
|
141
|
+
const initialFetch = maybeFetch();
|
|
142
|
+
if (suspend) await initialFetch;else initialFetch.catch(() => {});
|
|
143
|
+
|
|
144
|
+
// While a fetch for new args (or for a key with no data) is in flight, where React's
|
|
145
|
+
// useSuspense would suspend, keep returning the last resolved data. Vue can't re-suspend
|
|
146
|
+
// after setup, so this avoids yielding `undefined` mid-transition. A key that already
|
|
147
|
+
// has data keeps showing current store data, even while stale or refetching.
|
|
148
|
+
let lastData;
|
|
149
|
+
let lastKey = '';
|
|
150
|
+
const loading = vue.computed(() => {
|
|
151
|
+
const meta = responseMeta.value;
|
|
152
|
+
const key = argsKey.value;
|
|
153
|
+
return !!key && fetchingKey.value === key && (lastKey !== key || meta.data === undefined) && meta.expiryStatus !== core.ExpiryStatus.Valid && isStale(meta);
|
|
154
|
+
});
|
|
155
|
+
// surface fetch errors for the current args like React's useSuspense does
|
|
156
|
+
const error = vue.computed(() => loading.value ? undefined : controller.getError(endpoint, ...resolvedArgs.value, stateRef.value));
|
|
157
|
+
const data = vue.computed(() => {
|
|
158
|
+
if (loading.value || error.value) return lastData;
|
|
159
|
+
lastKey = argsKey.value;
|
|
160
|
+
return lastData = responseMeta.value.data;
|
|
80
161
|
});
|
|
81
162
|
|
|
82
|
-
//
|
|
83
|
-
|
|
163
|
+
// Throw on every read; a computed that throws would return its cached value on the next read
|
|
164
|
+
const result = vue.customRef(() => ({
|
|
165
|
+
get() {
|
|
166
|
+
if (error.value) throw error.value;
|
|
167
|
+
return data.value;
|
|
168
|
+
},
|
|
169
|
+
set() {}
|
|
170
|
+
}));
|
|
84
171
|
|
|
85
|
-
// Return readonly
|
|
86
|
-
return vue.readonly(
|
|
172
|
+
// Return readonly ref - Vue automatically unwraps in templates and reactive contexts
|
|
173
|
+
return vue.readonly(result);
|
|
87
174
|
}
|
|
88
175
|
|
|
89
176
|
/**
|
|
@@ -94,8 +181,8 @@ async function useSuspense(endpoint, ...args) {
|
|
|
94
181
|
function useSubscription(endpoint, ...args) {
|
|
95
182
|
const controller = useController();
|
|
96
183
|
|
|
97
|
-
// Track top-level reactive args (
|
|
98
|
-
const resolvedArgs = vue.computed(() => args.map(a => vue.
|
|
184
|
+
// Track top-level reactive args (refs and getters are resolved). This allows props/refs/getters to trigger resubscribe.
|
|
185
|
+
const resolvedArgs = vue.computed(() => args.map(a => vue.toValue(a)));
|
|
99
186
|
const key = vue.computed(() => {
|
|
100
187
|
if (resolvedArgs.value[0] === null) return '';
|
|
101
188
|
return endpoint.key(...resolvedArgs.value);
|
|
@@ -125,20 +212,13 @@ function useQuery(schema, ...args) {
|
|
|
125
212
|
const stateRef = injectState();
|
|
126
213
|
const controller = useController();
|
|
127
214
|
|
|
128
|
-
// Track top-level reactive args (
|
|
129
|
-
const resolvedArgs = vue.computed(() => args.map(a => vue.
|
|
215
|
+
// Track top-level reactive args (refs and getters are resolved). This allows props/refs/getters to trigger updates.
|
|
216
|
+
const resolvedArgs = vue.computed(() => args.map(a => vue.toValue(a)));
|
|
130
217
|
|
|
131
218
|
// Compute query meta based on state and args. This mirrors React's memoization
|
|
132
219
|
// that keys off state.entities/indexes and args.
|
|
133
220
|
const queryMeta = vue.computed(() => controller.getQueryMeta(schema, ...resolvedArgs.value, stateRef.value));
|
|
134
|
-
|
|
135
|
-
// Maintain GC refcounts on data mount/changes
|
|
136
|
-
vue.watch(() => queryMeta.value.data, (_newVal, _oldVal, onCleanup) => {
|
|
137
|
-
const decrement = queryMeta.value.countRef();
|
|
138
|
-
onCleanup(() => decrement());
|
|
139
|
-
}, {
|
|
140
|
-
immediate: true
|
|
141
|
-
});
|
|
221
|
+
useCountRef(queryMeta);
|
|
142
222
|
return vue.computed(() => queryMeta.value.data);
|
|
143
223
|
}
|
|
144
224
|
|
|
@@ -265,19 +345,13 @@ function trackPromise(promise) {
|
|
|
265
345
|
*/
|
|
266
346
|
|
|
267
347
|
function useFetch(endpoint, ...args) {
|
|
268
|
-
const
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
const argsKey = vue.computed(() => resolvedArgs.value[0] !== null ? endpoint.key(...resolvedArgs.value) : '');
|
|
276
|
-
|
|
277
|
-
// Compute response meta reactively so we can respond to store updates
|
|
278
|
-
const responseMeta = vue.computed(() => {
|
|
279
|
-
return controller.getResponseMeta(endpoint, ...resolvedArgs.value, stateRef.value);
|
|
280
|
-
});
|
|
348
|
+
const {
|
|
349
|
+
controller,
|
|
350
|
+
stateRef,
|
|
351
|
+
resolvedArgs,
|
|
352
|
+
argsKey,
|
|
353
|
+
responseMeta
|
|
354
|
+
} = useResponseMeta(endpoint, args);
|
|
281
355
|
const lastPromise = vue.ref(undefined);
|
|
282
356
|
let lastKey = '';
|
|
283
357
|
const maybeFetch = () => {
|
|
@@ -287,9 +361,7 @@ function useFetch(endpoint, ...args) {
|
|
|
287
361
|
lastKey = '';
|
|
288
362
|
return;
|
|
289
363
|
}
|
|
290
|
-
|
|
291
|
-
const forceFetch = meta.expiryStatus === core.ExpiryStatus.Invalid;
|
|
292
|
-
if (Date.now() > meta.expiresAt || forceFetch) {
|
|
364
|
+
if (isStale(responseMeta.value)) {
|
|
293
365
|
lastPromise.value = trackPromise(controller.fetch(endpoint, ...resolvedArgs.value));
|
|
294
366
|
lastKey = key;
|
|
295
367
|
} else if (!lastPromise.value || lastKey !== key) {
|
|
@@ -302,36 +374,42 @@ function useFetch(endpoint, ...args) {
|
|
|
302
374
|
maybeFetch();
|
|
303
375
|
|
|
304
376
|
// Also watch for store changes that might require refetch (e.g., invalidation)
|
|
305
|
-
vue.watch(() => {
|
|
306
|
-
const m = responseMeta.value;
|
|
307
|
-
return [m.expiresAt, m.expiryStatus, stateRef.value.lastReset, argsKey.value];
|
|
308
|
-
}, () => {
|
|
377
|
+
vue.watch(refetchTriggers(responseMeta, stateRef, argsKey), () => {
|
|
309
378
|
maybeFetch();
|
|
310
379
|
});
|
|
311
380
|
return lastPromise;
|
|
312
381
|
}
|
|
313
382
|
|
|
314
|
-
/**
|
|
315
|
-
* Read an Endpoint's response if it is ready.
|
|
383
|
+
/** Whether the response is expired, re-checked only when expiry, args, or a reset changes.
|
|
316
384
|
*
|
|
317
|
-
*
|
|
318
|
-
*
|
|
385
|
+
* Mirrors the useMemo deps in @data-client/react so unrelated store updates
|
|
386
|
+
* don't re-read Date.now(). Relies on Vue >=3.4 computed stability: the narrow
|
|
387
|
+
* computeds below don't notify `expired` when their value is unchanged.
|
|
319
388
|
*/
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
const
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
const argsKey = vue.computed(() => resolvedArgs.value[0] !== null ? endpoint.key(...resolvedArgs.value) : '');
|
|
330
|
-
|
|
331
|
-
// Compute response meta reactively so we can respond to store updates
|
|
332
|
-
const responseMeta = vue.computed(() => {
|
|
333
|
-
return controller.getResponseMeta(endpoint, ...resolvedArgs.value, stateRef.value);
|
|
389
|
+
function useExpired(responseMeta, stateRef, argsKey, forceFetch) {
|
|
390
|
+
const expiresAt = vue.computed(() => responseMeta.value.expiresAt);
|
|
391
|
+
const lastReset = vue.computed(() => stateRef.value.lastReset);
|
|
392
|
+
return vue.computed(() => {
|
|
393
|
+
// read every source up front so short-circuiting doesn't drop tracking
|
|
394
|
+
const key = argsKey.value;
|
|
395
|
+
const force = forceFetch.value;
|
|
396
|
+
void lastReset.value;
|
|
397
|
+
return !!((Date.now() > expiresAt.value || force) && key);
|
|
334
398
|
});
|
|
399
|
+
}
|
|
400
|
+
|
|
401
|
+
/** Reactive store response shared by useCache() and useDLE().
|
|
402
|
+
*
|
|
403
|
+
* `data` excludes cached entities while useSuspense() would suspend.
|
|
404
|
+
*/
|
|
405
|
+
function useCacheResponse(endpoint, args) {
|
|
406
|
+
const {
|
|
407
|
+
controller,
|
|
408
|
+
stateRef,
|
|
409
|
+
resolvedArgs,
|
|
410
|
+
argsKey,
|
|
411
|
+
responseMeta
|
|
412
|
+
} = useResponseMeta(endpoint, args);
|
|
335
413
|
|
|
336
414
|
// If we are hard invalid we must fetch regardless of triggering or staleness
|
|
337
415
|
const forceFetch = vue.computed(() => responseMeta.value.expiryStatus === core.ExpiryStatus.Invalid);
|
|
@@ -341,22 +419,13 @@ function useCache(endpoint, ...args) {
|
|
|
341
419
|
// It only changes the value when expiry or params change.
|
|
342
420
|
// This way, random unrelated re-renders don't cause the concept of expiry
|
|
343
421
|
// to change
|
|
344
|
-
const expired =
|
|
345
|
-
return (Date.now() > responseMeta.value.expiresAt || forceFetch.value) && argsKey.value;
|
|
346
|
-
});
|
|
422
|
+
const expired = useExpired(responseMeta, stateRef, argsKey, forceFetch);
|
|
347
423
|
|
|
348
424
|
// fully "valid" data will not suspend/loading even if it is not fresh
|
|
349
425
|
const loading = vue.computed(() => responseMeta.value.expiryStatus !== core.ExpiryStatus.Valid && expired.value);
|
|
350
426
|
/****************************************************************************************************/
|
|
351
427
|
|
|
352
|
-
|
|
353
|
-
vue.watch(() => responseMeta.value.data, (_newVal, _oldVal, onCleanup) => {
|
|
354
|
-
const decrement = responseMeta.value.countRef();
|
|
355
|
-
onCleanup(() => decrement());
|
|
356
|
-
}, {
|
|
357
|
-
immediate: true
|
|
358
|
-
});
|
|
359
|
-
return vue.computed(() => {
|
|
428
|
+
const data = vue.computed(() => {
|
|
360
429
|
// if useSuspense() would suspend, don't include entities from cache
|
|
361
430
|
if (loading.value) {
|
|
362
431
|
if (!endpoint.schema) return undefined;
|
|
@@ -368,6 +437,26 @@ function useCache(endpoint, ...args) {
|
|
|
368
437
|
}
|
|
369
438
|
return responseMeta.value.data;
|
|
370
439
|
});
|
|
440
|
+
return {
|
|
441
|
+
controller,
|
|
442
|
+
stateRef,
|
|
443
|
+
resolvedArgs,
|
|
444
|
+
argsKey,
|
|
445
|
+
responseMeta,
|
|
446
|
+
loading,
|
|
447
|
+
data
|
|
448
|
+
};
|
|
449
|
+
}
|
|
450
|
+
|
|
451
|
+
/**
|
|
452
|
+
* Read an Endpoint's response if it is ready.
|
|
453
|
+
*
|
|
454
|
+
* `useCache` is globally memoized.
|
|
455
|
+
* @see https://dataclient.io/docs/api/useCache
|
|
456
|
+
*/
|
|
457
|
+
|
|
458
|
+
function useCache(endpoint, ...args) {
|
|
459
|
+
return useCacheResponse(endpoint, args).data;
|
|
371
460
|
}
|
|
372
461
|
|
|
373
462
|
/**
|
|
@@ -376,72 +465,25 @@ function useCache(endpoint, ...args) {
|
|
|
376
465
|
*/
|
|
377
466
|
|
|
378
467
|
function useDLE(endpoint, ...args) {
|
|
379
|
-
const
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
// Compute response meta reactively so we can respond to store updates
|
|
389
|
-
const responseMeta = vue.computed(() => {
|
|
390
|
-
return controller.getResponseMeta(endpoint, ...resolvedArgs.value, stateRef.value);
|
|
391
|
-
});
|
|
392
|
-
|
|
393
|
-
// If we are hard invalid we must fetch regardless of triggering or staleness
|
|
394
|
-
const forceFetch = vue.computed(() => responseMeta.value.expiryStatus === core.ExpiryStatus.Invalid);
|
|
395
|
-
|
|
396
|
-
/*********** This block is to ensure results are only filled when they would not suspend **************/
|
|
397
|
-
// This computation reflects the behavior of useSuspense/useFetch
|
|
398
|
-
// It only changes the value when expiry or params change.
|
|
399
|
-
// This way, random unrelated re-renders don't cause the concept of expiry
|
|
400
|
-
// to change
|
|
401
|
-
const expired = vue.computed(() => {
|
|
402
|
-
return !!((Date.now() > responseMeta.value.expiresAt || forceFetch.value) && argsKey.value);
|
|
403
|
-
});
|
|
404
|
-
|
|
405
|
-
// fully "valid" data will not suspend/loading even if it is not fresh
|
|
406
|
-
const loading = vue.computed(() => {
|
|
407
|
-
return !!(responseMeta.value.expiryStatus !== core.ExpiryStatus.Valid && expired.value);
|
|
408
|
-
});
|
|
409
|
-
/****************************************************************************************************/
|
|
468
|
+
const {
|
|
469
|
+
controller,
|
|
470
|
+
stateRef,
|
|
471
|
+
resolvedArgs,
|
|
472
|
+
argsKey,
|
|
473
|
+
responseMeta,
|
|
474
|
+
loading,
|
|
475
|
+
data
|
|
476
|
+
} = useCacheResponse(endpoint, args);
|
|
410
477
|
|
|
411
478
|
// Trigger fetch when necessary
|
|
412
|
-
vue.watch(() => {
|
|
413
|
-
const m = responseMeta.value;
|
|
414
|
-
return [m.expiresAt, m.expiryStatus, stateRef.value.lastReset, argsKey.value];
|
|
415
|
-
}, async () => {
|
|
479
|
+
vue.watch(refetchTriggers(responseMeta, stateRef, argsKey), async () => {
|
|
416
480
|
const currentKey = argsKey.value;
|
|
417
481
|
if (!currentKey) return;
|
|
418
|
-
|
|
419
|
-
const force = forceFetch.value;
|
|
420
|
-
if (Date.now() <= meta.expiresAt && !force) return;
|
|
482
|
+
if (!isStale(responseMeta.value)) return;
|
|
421
483
|
await controller.fetch(endpoint, ...resolvedArgs.value).catch(() => {});
|
|
422
484
|
}, {
|
|
423
485
|
immediate: true
|
|
424
486
|
});
|
|
425
|
-
|
|
426
|
-
// Maintain GC refcounts on data mount/changes
|
|
427
|
-
vue.watch(() => responseMeta.value.data, (_newVal, _oldVal, onCleanup) => {
|
|
428
|
-
const decrement = responseMeta.value.countRef();
|
|
429
|
-
onCleanup(() => decrement());
|
|
430
|
-
}, {
|
|
431
|
-
immediate: true
|
|
432
|
-
});
|
|
433
|
-
const data = vue.computed(() => {
|
|
434
|
-
// if useSuspense() would suspend, don't include entities from cache
|
|
435
|
-
if (loading.value) {
|
|
436
|
-
if (!endpoint.schema) return undefined;
|
|
437
|
-
// TODO: use getResponse() once it just returns data
|
|
438
|
-
return controller.getResponseMeta(endpoint, ...resolvedArgs.value, {
|
|
439
|
-
...stateRef.value,
|
|
440
|
-
entities: {}
|
|
441
|
-
}).data;
|
|
442
|
-
}
|
|
443
|
-
return responseMeta.value.data;
|
|
444
|
-
});
|
|
445
487
|
const error = vue.computed(() => {
|
|
446
488
|
return controller.getError(endpoint, ...resolvedArgs.value, stateRef.value);
|
|
447
489
|
});
|
|
@@ -505,15 +547,20 @@ function constructManager(Mgr, optionOrInstance) {
|
|
|
505
547
|
return new Mgr(optionOrInstance);
|
|
506
548
|
}
|
|
507
549
|
|
|
550
|
+
/** Options for `app.use(DataClientPlugin, options)` */
|
|
551
|
+
|
|
552
|
+
/** @deprecated Internal to DataClientPlugin; will stop being exported */
|
|
553
|
+
|
|
508
554
|
/**
|
|
509
|
-
* Core provider logic
|
|
510
|
-
*
|
|
555
|
+
* Core provider logic used by DataClientPlugin.
|
|
556
|
+
*
|
|
557
|
+
* @deprecated Internal to DataClientPlugin; will stop being exported. Use DataClientPlugin instead.
|
|
511
558
|
*/
|
|
512
559
|
function createDataClient(options = {}) {
|
|
513
560
|
var _options$managers, _options$initialState;
|
|
514
561
|
const {
|
|
515
562
|
Controller = core.Controller,
|
|
516
|
-
gcPolicy,
|
|
563
|
+
gcPolicy = new core.GCPolicy(),
|
|
517
564
|
app
|
|
518
565
|
} = options;
|
|
519
566
|
|
|
@@ -633,13 +680,26 @@ const DataClientPlugin = {
|
|
|
633
680
|
console.info('Starting DataClientPlugin');
|
|
634
681
|
}
|
|
635
682
|
provider.start();
|
|
636
|
-
|
|
683
|
+
const stop = () => {
|
|
637
684
|
/* istanbul ignore if */
|
|
638
685
|
if (process.env.NODE_ENV === 'development') {
|
|
639
686
|
console.info('Stopping DataClientPlugin');
|
|
640
687
|
}
|
|
641
688
|
provider.stop();
|
|
642
|
-
}
|
|
689
|
+
};
|
|
690
|
+
// app.onUnmount() was added in Vue 3.5
|
|
691
|
+
if (typeof app.onUnmount === 'function') {
|
|
692
|
+
app.onUnmount(stop);
|
|
693
|
+
} else {
|
|
694
|
+
const unmount = app.unmount;
|
|
695
|
+
app.unmount = function () {
|
|
696
|
+
try {
|
|
697
|
+
stop();
|
|
698
|
+
} finally {
|
|
699
|
+
unmount.call(this);
|
|
700
|
+
}
|
|
701
|
+
};
|
|
702
|
+
}
|
|
643
703
|
return provider;
|
|
644
704
|
}
|
|
645
705
|
};
|
|
@@ -662,6 +722,10 @@ Object.defineProperty(exports, "ExpiryStatus", {
|
|
|
662
722
|
enumerable: true,
|
|
663
723
|
get: function () { return core.ExpiryStatus; }
|
|
664
724
|
});
|
|
725
|
+
Object.defineProperty(exports, "GCPolicy", {
|
|
726
|
+
enumerable: true,
|
|
727
|
+
get: function () { return core.GCPolicy; }
|
|
728
|
+
});
|
|
665
729
|
Object.defineProperty(exports, "LogoutManager", {
|
|
666
730
|
enumerable: true,
|
|
667
731
|
get: function () { return core.LogoutManager; }
|
|
@@ -683,6 +747,7 @@ Object.defineProperty(exports, "actionTypes", {
|
|
|
683
747
|
get: function () { return core.actionTypes; }
|
|
684
748
|
});
|
|
685
749
|
exports.DataClientPlugin = DataClientPlugin;
|
|
750
|
+
exports.__INTERNAL__ = internal;
|
|
686
751
|
exports.createDataClient = createDataClient;
|
|
687
752
|
exports.useCache = useCache;
|
|
688
753
|
exports.useController = useController;
|