@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.
Files changed (112) hide show
  1. package/CHANGELOG.md +371 -0
  2. package/README.md +86 -41
  3. package/dist/index.js +223 -158
  4. package/dist/index.umd.min.js +1 -1
  5. package/lib/consumers/refetchTriggers.d.ts +12 -0
  6. package/lib/consumers/refetchTriggers.d.ts.map +1 -0
  7. package/lib/consumers/refetchTriggers.js +9 -0
  8. package/lib/consumers/useCache.d.ts +1 -1
  9. package/lib/consumers/useCache.d.ts.map +1 -1
  10. package/lib/consumers/useCache.js +3 -53
  11. package/lib/consumers/useCacheResponse.d.ts +20 -0
  12. package/lib/consumers/useCacheResponse.d.ts.map +1 -0
  13. package/lib/consumers/useCacheResponse.js +55 -0
  14. package/lib/consumers/useCountRef.d.ts +7 -0
  15. package/lib/consumers/useCountRef.d.ts.map +1 -0
  16. package/lib/consumers/useCountRef.js +10 -0
  17. package/lib/consumers/useDLE.d.ts +1 -1
  18. package/lib/consumers/useDLE.d.ts.map +1 -1
  19. package/lib/consumers/useDLE.js +16 -62
  20. package/lib/consumers/useDebounce.d.ts +1 -1
  21. package/lib/consumers/useDebounce.d.ts.map +1 -1
  22. package/lib/consumers/useDebounce.js +1 -2
  23. package/lib/consumers/useExpired.d.ts +12 -0
  24. package/lib/consumers/useExpired.d.ts.map +1 -0
  25. package/lib/consumers/useExpired.js +19 -0
  26. package/lib/consumers/useFetch.d.ts +8 -11
  27. package/lib/consumers/useFetch.d.ts.map +1 -1
  28. package/lib/consumers/useFetch.js +13 -24
  29. package/lib/consumers/useLive.js +1 -1
  30. package/lib/consumers/useLoading.d.ts +1 -1
  31. package/lib/consumers/useLoading.d.ts.map +1 -1
  32. package/lib/consumers/useLoading.js +1 -2
  33. package/lib/consumers/useQuery.d.ts +1 -1
  34. package/lib/consumers/useQuery.d.ts.map +1 -1
  35. package/lib/consumers/useQuery.js +6 -12
  36. package/lib/consumers/useResponseMeta.d.ts +23 -0
  37. package/lib/consumers/useResponseMeta.d.ts.map +1 -0
  38. package/lib/consumers/useResponseMeta.js +36 -0
  39. package/lib/consumers/useSubscription.js +4 -4
  40. package/lib/consumers/useSuspense.d.ts +1 -1
  41. package/lib/consumers/useSuspense.d.ts.map +1 -1
  42. package/lib/consumers/useSuspense.js +58 -34
  43. package/lib/context.d.ts +1 -1
  44. package/lib/context.d.ts.map +1 -1
  45. package/lib/context.js +3 -3
  46. package/lib/index.d.ts +2 -1
  47. package/lib/index.d.ts.map +1 -1
  48. package/lib/index.js +4 -2
  49. package/lib/internal.d.ts +6 -0
  50. package/lib/internal.d.ts.map +1 -0
  51. package/lib/internal.js +6 -0
  52. package/lib/providers/DataClientPlugin.d.ts +1 -1
  53. package/lib/providers/DataClientPlugin.d.ts.map +1 -1
  54. package/lib/providers/DataClientPlugin.js +16 -4
  55. package/lib/providers/createDataClient.d.ts +10 -4
  56. package/lib/providers/createDataClient.d.ts.map +1 -1
  57. package/lib/providers/createDataClient.js +11 -5
  58. package/lib/providers/getDefaultManagers.d.ts.map +1 -1
  59. package/lib/providers/getDefaultManagers.js +1 -1
  60. package/lib/providers/index.d.ts +2 -1
  61. package/lib/providers/index.d.ts.map +1 -1
  62. package/lib/providers/index.js +1 -1
  63. package/lib/test/MockPlugin.d.ts +1 -1
  64. package/lib/test/MockPlugin.d.ts.map +1 -1
  65. package/lib/test/MockPlugin.js +1 -1
  66. package/lib/test/index.d.ts +2 -1
  67. package/lib/test/index.d.ts.map +1 -1
  68. package/lib/test/index.js +1 -1
  69. package/lib/test/mountDataClient.d.ts +5 -3
  70. package/lib/test/mountDataClient.d.ts.map +1 -1
  71. package/lib/test/mountDataClient.js +15 -10
  72. package/lib/test/renderDataCompose.d.ts +10 -4
  73. package/lib/test/renderDataCompose.d.ts.map +1 -1
  74. package/lib/test/renderDataCompose.js +2 -3
  75. package/package.json +20 -5
  76. package/src/__tests__/__snapshots__/context.test.ts.snap +2 -2
  77. package/src/__tests__/context.test.ts +1 -1
  78. package/src/__tests__/integration-garbage-collection.web.ts +80 -4
  79. package/src/__tests__/useCache.web.ts +160 -93
  80. package/src/__tests__/useDLE.web.ts +204 -96
  81. package/src/__tests__/useFetch.web.ts +100 -37
  82. package/src/__tests__/useQuery.web.ts +60 -50
  83. package/src/__tests__/useSubscription.web.ts +74 -75
  84. package/src/__tests__/useSuspense.web.ts +455 -46
  85. package/src/consumers/refetchTriggers.ts +22 -0
  86. package/src/consumers/useCache.ts +7 -70
  87. package/src/consumers/useCacheResponse.ts +56 -0
  88. package/src/consumers/useCountRef.ts +15 -0
  89. package/src/consumers/useDLE.ts +24 -83
  90. package/src/consumers/useDebounce.ts +2 -1
  91. package/src/consumers/useExpired.ts +26 -0
  92. package/src/consumers/useFetch.ts +34 -49
  93. package/src/consumers/useLive.ts +1 -1
  94. package/src/consumers/useLoading.ts +2 -1
  95. package/src/consumers/useQuery.ts +10 -13
  96. package/src/consumers/useResponseMeta.ts +41 -0
  97. package/src/consumers/useSubscription.ts +3 -3
  98. package/src/consumers/useSuspense.ts +66 -58
  99. package/src/context.ts +4 -3
  100. package/src/index.ts +7 -1
  101. package/src/internal.ts +12 -0
  102. package/src/providers/DataClientPlugin.ts +17 -3
  103. package/src/providers/__tests__/DataClientPlugin.test.ts +40 -1
  104. package/src/providers/createDataClient.ts +15 -5
  105. package/src/providers/getDefaultManagers.ts +2 -6
  106. package/src/providers/index.ts +2 -5
  107. package/src/test/MockPlugin.ts +3 -3
  108. package/src/test/README.md +10 -304
  109. package/src/test/__tests__/mountDataClient.test.ts +38 -0
  110. package/src/test/index.ts +2 -1
  111. package/src/test/mountDataClient.ts +22 -17
  112. 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/docs/getting-started/installation#add-provider-at-top-level-component\n' + 'Comosables only work in script setup: https://vuejs.org/guide/reusability/composables.html#usage-restrictions');
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/docs/getting-started/installation#add-provider-at-top-level-component\n' + 'Comosables only work in script setup: https://vuejs.org/guide/reusability/composables.html#usage-restrictions');
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
- * Ensure an endpoint is available.
36
- * Suspends until it is.
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
- * @see https://dataclient.io/docs/api/useSuspense
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 (Refs are unwrapped). This allows props/refs to trigger updates.
48
- const resolvedArgs = vue.computed(() => args.map(a => vue.unref(a)));
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
- return controller.getResponseMeta(endpoint, ...resolvedArgs.value, stateRef.value);
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
- const meta = responseMeta.value;
61
- const forceFetch = meta.expiryStatus === core.ExpiryStatus.Invalid;
62
- if (Date.now() <= meta.expiresAt && !forceFetch) return;
63
- await controller.fetch(endpoint, ...resolvedArgs.value);
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
- const m = responseMeta.value;
69
- return [m.expiresAt, m.expiryStatus, stateRef.value.lastReset, argsKey.value];
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
- // Maintain GC refcounts on data mount/changes
75
- vue.watch(() => responseMeta.value.data, (_newVal, _oldVal, onCleanup) => {
76
- const decrement = responseMeta.value.countRef();
77
- onCleanup(() => decrement == null ? void 0 : decrement());
78
- }, {
79
- immediate: true
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
- // Trigger on initial call
83
- await maybeFetch();
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 computed ref - Vue automatically unwraps in templates and reactive contexts
86
- return vue.readonly(vue.computed(() => responseMeta.value.data));
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 (Refs are unwrapped). This allows props/refs to trigger resubscribe.
98
- const resolvedArgs = vue.computed(() => args.map(a => vue.unref(a)));
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 (Refs are unwrapped). This allows props/refs to trigger updates.
129
- const resolvedArgs = vue.computed(() => args.map(a => vue.unref(a)));
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 stateRef = injectState();
269
- const controller = useController();
270
-
271
- // Track top-level reactive args (Refs are unwrapped). This allows props/refs to trigger updates.
272
- const resolvedArgs = vue.computed(() => args.map(a => vue.unref(a)));
273
-
274
- // Compute a key that changes when args change (including reactive props)
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
- const meta = responseMeta.value;
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
- * `useCache` is globally memoized.
318
- * @see https://dataclient.io/docs/api/useCache
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
- function useCache(endpoint, ...args) {
322
- const stateRef = injectState();
323
- const controller = useController();
324
-
325
- // Track top-level reactive args (Refs are unwrapped). This allows props/refs to trigger updates.
326
- const resolvedArgs = vue.computed(() => args.map(a => vue.unref(a)));
327
-
328
- // Compute a key that changes when args change (including reactive props)
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 = vue.computed(() => {
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
- // Maintain GC refcounts on data mount/changes
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 stateRef = injectState();
380
- const controller = useController();
381
-
382
- // Track top-level reactive args (Refs are unwrapped). This allows props/refs to trigger updates.
383
- const resolvedArgs = vue.computed(() => args.map(a => vue.unref(a)));
384
-
385
- // Compute a key that changes when args change (including reactive props)
386
- const argsKey = vue.computed(() => resolvedArgs.value[0] !== null ? endpoint.key(...resolvedArgs.value) : '');
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
- const meta = responseMeta.value;
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 that can be used by both composable and plugin.
510
- * This function handles the actual setup of the data client without Vue-specific concerns.
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
- app.onUnmount(() => {
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;