@data-client/vue 0.0.2 → 0.0.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +2 -6
- package/dist/index.js +145 -0
- package/dist/index.umd.min.js +1 -1
- package/lib/consumers/index.d.ts +4 -0
- package/lib/consumers/index.d.ts.map +1 -1
- package/lib/consumers/index.js +5 -1
- package/lib/consumers/useDebounce.d.ts +16 -0
- package/lib/consumers/useDebounce.d.ts.map +1 -0
- package/lib/consumers/useDebounce.js +50 -0
- package/lib/consumers/useFetch.d.ts +10 -0
- package/lib/consumers/useFetch.d.ts.map +1 -0
- package/lib/consumers/useFetch.js +41 -0
- package/lib/consumers/useLive.d.ts +13 -0
- package/lib/consumers/useLive.d.ts.map +1 -0
- package/lib/consumers/useLive.js +17 -0
- package/lib/consumers/useLoading.d.ts +18 -0
- package/lib/consumers/useLoading.d.ts.map +1 -0
- package/lib/consumers/useLoading.js +44 -0
- package/lib/index.js +1 -4
- package/node.mjs +1 -0
- package/package.json +1 -19
- package/src/__tests__/useDebounce.web.ts +264 -0
- package/src/__tests__/useFetch.web.ts +200 -0
- package/src/__tests__/useLive.web.ts +162 -0
- package/src/__tests__/useLoading.web.ts +238 -0
- package/src/consumers/index.ts +4 -0
- package/src/consumers/useDebounce.ts +78 -0
- package/src/consumers/useFetch.ts +80 -0
- package/src/consumers/useLive.ts +64 -0
- package/src/consumers/useLoading.ts +47 -0
- package/src/index.ts +0 -3
- package/ts3.4/consumers/index.d.ts +4 -0
- package/ts3.4/consumers/useDebounce.d.ts +19 -0
- package/ts3.4/consumers/useFetch.d.ts +16 -0
- package/ts3.4/consumers/useLive.d.ts +19 -0
- package/ts3.4/consumers/useLoading.d.ts +22 -0
- package/lib/components/DataProvider.d.ts +0 -11
- package/lib/components/DataProvider.d.ts.map +0 -1
- package/lib/components/DataProvider.js +0 -18
- package/src/components/DataProvider.ts +0 -26
- package/ts3.4/components/DataProvider.d.ts +0 -11
|
@@ -0,0 +1,238 @@
|
|
|
1
|
+
import { mount } from '@vue/test-utils';
|
|
2
|
+
import { defineComponent, h, nextTick } from 'vue';
|
|
3
|
+
|
|
4
|
+
import useLoading from '../consumers/useLoading';
|
|
5
|
+
|
|
6
|
+
describe('vue useLoading()', () => {
|
|
7
|
+
async function flushUntil(
|
|
8
|
+
wrapper: any,
|
|
9
|
+
predicate: () => boolean,
|
|
10
|
+
tries = 100,
|
|
11
|
+
) {
|
|
12
|
+
for (let i = 0; i < tries; i++) {
|
|
13
|
+
if (predicate()) return;
|
|
14
|
+
await Promise.resolve();
|
|
15
|
+
await nextTick();
|
|
16
|
+
await new Promise(resolve => setTimeout(resolve, 0));
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
it('tracks loading state for async functions', async () => {
|
|
21
|
+
let resolvePromise: (value: string) => void;
|
|
22
|
+
const mockAsyncFunction = jest.fn(
|
|
23
|
+
() =>
|
|
24
|
+
new Promise<string>(resolve => {
|
|
25
|
+
resolvePromise = resolve;
|
|
26
|
+
}),
|
|
27
|
+
);
|
|
28
|
+
|
|
29
|
+
const TestComponent = defineComponent({
|
|
30
|
+
name: 'TestComponent',
|
|
31
|
+
setup() {
|
|
32
|
+
const [wrappedFunction, loading, error] = useLoading(mockAsyncFunction);
|
|
33
|
+
|
|
34
|
+
const handleClick = async () => {
|
|
35
|
+
await wrappedFunction();
|
|
36
|
+
};
|
|
37
|
+
|
|
38
|
+
return () =>
|
|
39
|
+
h('div', [
|
|
40
|
+
h(
|
|
41
|
+
'button',
|
|
42
|
+
{
|
|
43
|
+
onClick: handleClick,
|
|
44
|
+
'data-testid': 'test-button',
|
|
45
|
+
},
|
|
46
|
+
loading.value ? 'Loading...' : 'Click me',
|
|
47
|
+
),
|
|
48
|
+
h('div', { 'data-testid': 'error' }, error.value?.message || ''),
|
|
49
|
+
h('div', { 'data-testid': 'loading' }, loading.value.toString()),
|
|
50
|
+
]);
|
|
51
|
+
},
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
const wrapper = mount(TestComponent);
|
|
55
|
+
|
|
56
|
+
// Initially not loading
|
|
57
|
+
expect(wrapper.find('[data-testid="loading"]').text()).toBe('false');
|
|
58
|
+
expect(wrapper.find('[data-testid="test-button"]').text()).toBe('Click me');
|
|
59
|
+
|
|
60
|
+
// Click to trigger loading
|
|
61
|
+
await wrapper.find('[data-testid="test-button"]').trigger('click');
|
|
62
|
+
await nextTick();
|
|
63
|
+
|
|
64
|
+
// Should be loading
|
|
65
|
+
expect(wrapper.find('[data-testid="loading"]').text()).toBe('true');
|
|
66
|
+
expect(wrapper.find('[data-testid="test-button"]').text()).toBe(
|
|
67
|
+
'Loading...',
|
|
68
|
+
);
|
|
69
|
+
|
|
70
|
+
// Resolve the promise
|
|
71
|
+
resolvePromise!('success');
|
|
72
|
+
await flushUntil(
|
|
73
|
+
wrapper,
|
|
74
|
+
() => wrapper.find('[data-testid="loading"]').text() === 'false',
|
|
75
|
+
);
|
|
76
|
+
|
|
77
|
+
// Should not be loading anymore
|
|
78
|
+
expect(wrapper.find('[data-testid="loading"]').text()).toBe('false');
|
|
79
|
+
expect(wrapper.find('[data-testid="test-button"]').text()).toBe('Click me');
|
|
80
|
+
expect(mockAsyncFunction).toHaveBeenCalledTimes(1);
|
|
81
|
+
});
|
|
82
|
+
|
|
83
|
+
it('handles errors from async functions', async () => {
|
|
84
|
+
const testError = new Error('Test error');
|
|
85
|
+
const mockAsyncFunction = jest.fn(() => Promise.reject(testError));
|
|
86
|
+
|
|
87
|
+
const TestComponent = defineComponent({
|
|
88
|
+
name: 'TestComponent',
|
|
89
|
+
setup() {
|
|
90
|
+
const [wrappedFunction, loading, error] = useLoading(mockAsyncFunction);
|
|
91
|
+
|
|
92
|
+
const handleClick = async () => {
|
|
93
|
+
await wrappedFunction();
|
|
94
|
+
};
|
|
95
|
+
|
|
96
|
+
return () =>
|
|
97
|
+
h('div', [
|
|
98
|
+
h(
|
|
99
|
+
'button',
|
|
100
|
+
{
|
|
101
|
+
onClick: handleClick,
|
|
102
|
+
'data-testid': 'test-button',
|
|
103
|
+
},
|
|
104
|
+
'Click me',
|
|
105
|
+
),
|
|
106
|
+
h('div', { 'data-testid': 'error' }, error.value?.message || ''),
|
|
107
|
+
h('div', { 'data-testid': 'loading' }, loading.value.toString()),
|
|
108
|
+
]);
|
|
109
|
+
},
|
|
110
|
+
});
|
|
111
|
+
|
|
112
|
+
const wrapper = mount(TestComponent);
|
|
113
|
+
|
|
114
|
+
// Click to trigger error
|
|
115
|
+
await wrapper.find('[data-testid="test-button"]').trigger('click');
|
|
116
|
+
await flushUntil(
|
|
117
|
+
wrapper,
|
|
118
|
+
() => wrapper.find('[data-testid="error"]').text() === 'Test error',
|
|
119
|
+
);
|
|
120
|
+
|
|
121
|
+
// Should capture the error
|
|
122
|
+
expect(wrapper.find('[data-testid="error"]').text()).toBe('Test error');
|
|
123
|
+
expect(wrapper.find('[data-testid="loading"]').text()).toBe('false');
|
|
124
|
+
expect(mockAsyncFunction).toHaveBeenCalledTimes(1);
|
|
125
|
+
});
|
|
126
|
+
|
|
127
|
+
it('does not update loading state after component unmount', async () => {
|
|
128
|
+
let resolvePromise: (value: string) => void;
|
|
129
|
+
const mockAsyncFunction = jest.fn(
|
|
130
|
+
() =>
|
|
131
|
+
new Promise<string>(resolve => {
|
|
132
|
+
resolvePromise = resolve;
|
|
133
|
+
}),
|
|
134
|
+
);
|
|
135
|
+
|
|
136
|
+
const TestComponent = defineComponent({
|
|
137
|
+
name: 'TestComponent',
|
|
138
|
+
setup() {
|
|
139
|
+
const [wrappedFunction, loading] = useLoading(mockAsyncFunction);
|
|
140
|
+
|
|
141
|
+
const handleClick = async () => {
|
|
142
|
+
await wrappedFunction();
|
|
143
|
+
};
|
|
144
|
+
|
|
145
|
+
return () =>
|
|
146
|
+
h('div', [
|
|
147
|
+
h(
|
|
148
|
+
'button',
|
|
149
|
+
{
|
|
150
|
+
onClick: handleClick,
|
|
151
|
+
'data-testid': 'test-button',
|
|
152
|
+
},
|
|
153
|
+
'Click me',
|
|
154
|
+
),
|
|
155
|
+
h('div', { 'data-testid': 'loading' }, loading.value.toString()),
|
|
156
|
+
]);
|
|
157
|
+
},
|
|
158
|
+
});
|
|
159
|
+
|
|
160
|
+
const wrapper = mount(TestComponent);
|
|
161
|
+
|
|
162
|
+
// Click to trigger loading
|
|
163
|
+
await wrapper.find('[data-testid="test-button"]').trigger('click');
|
|
164
|
+
await nextTick();
|
|
165
|
+
|
|
166
|
+
// Should be loading
|
|
167
|
+
expect(wrapper.find('[data-testid="loading"]').text()).toBe('true');
|
|
168
|
+
|
|
169
|
+
// Unmount component before resolving
|
|
170
|
+
wrapper.unmount();
|
|
171
|
+
|
|
172
|
+
// Resolve the promise after unmount
|
|
173
|
+
resolvePromise!('success');
|
|
174
|
+
await new Promise(resolve => setTimeout(resolve, 0));
|
|
175
|
+
|
|
176
|
+
// Component should be unmounted, no errors should occur
|
|
177
|
+
expect(mockAsyncFunction).toHaveBeenCalledTimes(1);
|
|
178
|
+
});
|
|
179
|
+
|
|
180
|
+
it('clears error on subsequent calls', async () => {
|
|
181
|
+
const testError = new Error('Test error');
|
|
182
|
+
let shouldError = true;
|
|
183
|
+
const mockAsyncFunction = jest.fn(() => {
|
|
184
|
+
if (shouldError) {
|
|
185
|
+
return Promise.reject(testError);
|
|
186
|
+
}
|
|
187
|
+
return Promise.resolve('success');
|
|
188
|
+
});
|
|
189
|
+
|
|
190
|
+
const TestComponent = defineComponent({
|
|
191
|
+
name: 'TestComponent',
|
|
192
|
+
setup() {
|
|
193
|
+
const [wrappedFunction, loading, error] = useLoading(mockAsyncFunction);
|
|
194
|
+
|
|
195
|
+
const handleClick = async () => {
|
|
196
|
+
await wrappedFunction();
|
|
197
|
+
};
|
|
198
|
+
|
|
199
|
+
return () =>
|
|
200
|
+
h('div', [
|
|
201
|
+
h(
|
|
202
|
+
'button',
|
|
203
|
+
{
|
|
204
|
+
onClick: handleClick,
|
|
205
|
+
'data-testid': 'test-button',
|
|
206
|
+
},
|
|
207
|
+
'Click me',
|
|
208
|
+
),
|
|
209
|
+
h('div', { 'data-testid': 'error' }, error.value?.message || ''),
|
|
210
|
+
h('div', { 'data-testid': 'loading' }, loading.value.toString()),
|
|
211
|
+
]);
|
|
212
|
+
},
|
|
213
|
+
});
|
|
214
|
+
|
|
215
|
+
const wrapper = mount(TestComponent);
|
|
216
|
+
|
|
217
|
+
// First call - should error
|
|
218
|
+
await wrapper.find('[data-testid="test-button"]').trigger('click');
|
|
219
|
+
await flushUntil(
|
|
220
|
+
wrapper,
|
|
221
|
+
() => wrapper.find('[data-testid="error"]').text() === 'Test error',
|
|
222
|
+
);
|
|
223
|
+
|
|
224
|
+
expect(wrapper.find('[data-testid="error"]').text()).toBe('Test error');
|
|
225
|
+
|
|
226
|
+
// Second call - should succeed and clear error
|
|
227
|
+
shouldError = false;
|
|
228
|
+
await wrapper.find('[data-testid="test-button"]').trigger('click');
|
|
229
|
+
await flushUntil(
|
|
230
|
+
wrapper,
|
|
231
|
+
() => wrapper.find('[data-testid="error"]').text() === '',
|
|
232
|
+
);
|
|
233
|
+
|
|
234
|
+
expect(wrapper.find('[data-testid="error"]').text()).toBe('');
|
|
235
|
+
expect(wrapper.find('[data-testid="loading"]').text()).toBe('false');
|
|
236
|
+
expect(mockAsyncFunction).toHaveBeenCalledTimes(2);
|
|
237
|
+
});
|
|
238
|
+
});
|
package/src/consumers/index.ts
CHANGED
|
@@ -1,4 +1,8 @@
|
|
|
1
1
|
export { default as useSuspense } from './useSuspense.js';
|
|
2
2
|
export { default as useSubscription } from './useSubscription.js';
|
|
3
3
|
export { default as useQuery } from './useQuery.js';
|
|
4
|
+
export { default as useLive } from './useLive.js';
|
|
5
|
+
export { default as useLoading } from './useLoading.js';
|
|
6
|
+
export { default as useDebounce } from './useDebounce.js';
|
|
7
|
+
export { default as useFetch } from './useFetch.js';
|
|
4
8
|
export { useController as useController } from '../context.js';
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
import { ref, watch, onUnmounted, type Ref } from 'vue';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Keeps value updated after delay time
|
|
5
|
+
*
|
|
6
|
+
* @see https://dataclient.io/docs/api/useDebounce
|
|
7
|
+
* @param value Any immutable value (can be a ref)
|
|
8
|
+
* @param delay Time in milliseconds to wait til updating the value
|
|
9
|
+
* @param updatable Whether to update at all
|
|
10
|
+
* @example
|
|
11
|
+
```
|
|
12
|
+
const [debouncedQuery, isPending] = useDebounce(query, 200);
|
|
13
|
+
const list = useSuspense(getThings, { query: debouncedQuery.value });
|
|
14
|
+
```
|
|
15
|
+
*/
|
|
16
|
+
export default function useDebounce<T>(
|
|
17
|
+
value: T | Ref<T>,
|
|
18
|
+
delay: number,
|
|
19
|
+
updatable: boolean | Ref<boolean> = true,
|
|
20
|
+
): [Ref<T>, Ref<boolean>] {
|
|
21
|
+
const debouncedValue = ref(
|
|
22
|
+
typeof value === 'object' && value !== null && 'value' in value ?
|
|
23
|
+
value.value
|
|
24
|
+
: value,
|
|
25
|
+
) as Ref<T>;
|
|
26
|
+
const isPending = ref(false);
|
|
27
|
+
let timeoutId: ReturnType<typeof setTimeout> | null = null;
|
|
28
|
+
|
|
29
|
+
const clearExistingTimeout = () => {
|
|
30
|
+
if (timeoutId !== null) {
|
|
31
|
+
clearTimeout(timeoutId);
|
|
32
|
+
timeoutId = null;
|
|
33
|
+
}
|
|
34
|
+
};
|
|
35
|
+
|
|
36
|
+
// Watch the input value and debounce updates
|
|
37
|
+
watch(
|
|
38
|
+
[
|
|
39
|
+
() =>
|
|
40
|
+
typeof value === 'object' && value !== null && 'value' in value ?
|
|
41
|
+
value.value
|
|
42
|
+
: value,
|
|
43
|
+
() =>
|
|
44
|
+
(
|
|
45
|
+
typeof updatable === 'object' &&
|
|
46
|
+
updatable !== null &&
|
|
47
|
+
'value' in updatable
|
|
48
|
+
) ?
|
|
49
|
+
updatable.value
|
|
50
|
+
: updatable,
|
|
51
|
+
() => delay,
|
|
52
|
+
],
|
|
53
|
+
([newValue, isUpdatable]) => {
|
|
54
|
+
clearExistingTimeout();
|
|
55
|
+
|
|
56
|
+
if (!isUpdatable) {
|
|
57
|
+
isPending.value = false;
|
|
58
|
+
return;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
isPending.value = true;
|
|
62
|
+
|
|
63
|
+
timeoutId = setTimeout(() => {
|
|
64
|
+
debouncedValue.value = newValue;
|
|
65
|
+
isPending.value = false;
|
|
66
|
+
timeoutId = null;
|
|
67
|
+
}, delay);
|
|
68
|
+
},
|
|
69
|
+
{ immediate: false },
|
|
70
|
+
);
|
|
71
|
+
|
|
72
|
+
// Cleanup on unmount
|
|
73
|
+
onUnmounted(() => {
|
|
74
|
+
clearExistingTimeout();
|
|
75
|
+
});
|
|
76
|
+
|
|
77
|
+
return [debouncedValue, isPending];
|
|
78
|
+
}
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
import { ExpiryStatus } from '@data-client/core';
|
|
2
|
+
import type {
|
|
3
|
+
EndpointInterface,
|
|
4
|
+
Denormalize,
|
|
5
|
+
Schema,
|
|
6
|
+
FetchFunction,
|
|
7
|
+
DenormalizeNullable,
|
|
8
|
+
} from '@data-client/core';
|
|
9
|
+
import { computed, watch } from 'vue';
|
|
10
|
+
|
|
11
|
+
import { useController, injectState } from '../context.js';
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Fetch an Endpoint if it is not in cache or stale.
|
|
15
|
+
* Non-suspense; returns the fetch Promise when a request is issued, otherwise undefined.
|
|
16
|
+
* Mirrors React useFetch semantics.
|
|
17
|
+
* @see https://dataclient.io/docs/api/useFetch
|
|
18
|
+
*/
|
|
19
|
+
export default function useFetch<
|
|
20
|
+
E extends EndpointInterface<
|
|
21
|
+
FetchFunction,
|
|
22
|
+
Schema | undefined,
|
|
23
|
+
undefined | false
|
|
24
|
+
>,
|
|
25
|
+
>(
|
|
26
|
+
endpoint: E,
|
|
27
|
+
...args: readonly [...Parameters<E>]
|
|
28
|
+
): E['schema'] extends undefined | null ? ReturnType<E>
|
|
29
|
+
: Promise<Denormalize<E['schema']>>;
|
|
30
|
+
|
|
31
|
+
export default function useFetch<
|
|
32
|
+
E extends EndpointInterface<
|
|
33
|
+
FetchFunction,
|
|
34
|
+
Schema | undefined,
|
|
35
|
+
undefined | false
|
|
36
|
+
>,
|
|
37
|
+
>(
|
|
38
|
+
endpoint: E,
|
|
39
|
+
...args: readonly [...Parameters<E>] | readonly [null]
|
|
40
|
+
): E['schema'] extends undefined | null ? ReturnType<E> | undefined
|
|
41
|
+
: Promise<DenormalizeNullable<E['schema']>> | undefined;
|
|
42
|
+
|
|
43
|
+
export default function useFetch(endpoint: any, ...args: any[]): any {
|
|
44
|
+
const stateRef = injectState();
|
|
45
|
+
const controller = useController();
|
|
46
|
+
|
|
47
|
+
const key: string = args[0] !== null ? endpoint.key(...args) : '';
|
|
48
|
+
|
|
49
|
+
// Compute response meta reactively so we can respond to store updates
|
|
50
|
+
const responseMeta = computed(() =>
|
|
51
|
+
key ? controller.getResponseMeta(endpoint, ...args, stateRef.value) : null,
|
|
52
|
+
);
|
|
53
|
+
|
|
54
|
+
let lastPromise: Promise<any> | undefined = undefined;
|
|
55
|
+
|
|
56
|
+
const maybeFetch = () => {
|
|
57
|
+
if (!key) return;
|
|
58
|
+
const meta = responseMeta.value;
|
|
59
|
+
if (!meta) return;
|
|
60
|
+
const forceFetch = meta.expiryStatus === ExpiryStatus.Invalid;
|
|
61
|
+
if (Date.now() <= meta.expiresAt && !forceFetch) return;
|
|
62
|
+
lastPromise = controller.fetch(endpoint, ...(args as any));
|
|
63
|
+
};
|
|
64
|
+
|
|
65
|
+
// Trigger on initial call
|
|
66
|
+
maybeFetch();
|
|
67
|
+
|
|
68
|
+
// Also watch for store changes that might require refetch (e.g., invalidation)
|
|
69
|
+
watch(
|
|
70
|
+
() => {
|
|
71
|
+
const m = responseMeta.value;
|
|
72
|
+
return m ? [m.expiresAt, m.expiryStatus, stateRef.value.lastReset] : key;
|
|
73
|
+
},
|
|
74
|
+
() => {
|
|
75
|
+
maybeFetch();
|
|
76
|
+
},
|
|
77
|
+
);
|
|
78
|
+
|
|
79
|
+
return lastPromise;
|
|
80
|
+
}
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
EndpointInterface,
|
|
3
|
+
Denormalize,
|
|
4
|
+
Schema,
|
|
5
|
+
FetchFunction,
|
|
6
|
+
ResolveType,
|
|
7
|
+
DenormalizeNullable,
|
|
8
|
+
} from '@data-client/core';
|
|
9
|
+
import type { DeepReadonly, ComputedRef } from 'vue';
|
|
10
|
+
|
|
11
|
+
import useSubscription from './useSubscription.js';
|
|
12
|
+
import useSuspense from './useSuspense.js';
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Ensure an endpoint is available. Keeps it fresh once it is.
|
|
16
|
+
*
|
|
17
|
+
* useSuspense() + useSubscription()
|
|
18
|
+
* @see https://dataclient.io/docs/api/useLive
|
|
19
|
+
* @throws {Promise} If data is not yet available.
|
|
20
|
+
* @throws {NetworkError} If fetch fails.
|
|
21
|
+
*/
|
|
22
|
+
export default function useLive<
|
|
23
|
+
E extends EndpointInterface<
|
|
24
|
+
FetchFunction,
|
|
25
|
+
Schema | undefined,
|
|
26
|
+
undefined | false
|
|
27
|
+
>,
|
|
28
|
+
>(
|
|
29
|
+
endpoint: E,
|
|
30
|
+
...args: readonly [...Parameters<E>]
|
|
31
|
+
): Promise<
|
|
32
|
+
DeepReadonly<
|
|
33
|
+
ComputedRef<
|
|
34
|
+
E['schema'] extends undefined | null ? ResolveType<E>
|
|
35
|
+
: Denormalize<E['schema']>
|
|
36
|
+
>
|
|
37
|
+
>
|
|
38
|
+
>;
|
|
39
|
+
|
|
40
|
+
export default function useLive<
|
|
41
|
+
E extends EndpointInterface<
|
|
42
|
+
FetchFunction,
|
|
43
|
+
Schema | undefined,
|
|
44
|
+
undefined | false
|
|
45
|
+
>,
|
|
46
|
+
>(
|
|
47
|
+
endpoint: E,
|
|
48
|
+
...args: readonly [...Parameters<E>] | readonly [null]
|
|
49
|
+
): Promise<
|
|
50
|
+
DeepReadonly<
|
|
51
|
+
ComputedRef<
|
|
52
|
+
E['schema'] extends undefined | null ? ResolveType<E> | undefined
|
|
53
|
+
: DenormalizeNullable<E['schema']>
|
|
54
|
+
>
|
|
55
|
+
>
|
|
56
|
+
>;
|
|
57
|
+
|
|
58
|
+
export default async function useLive(
|
|
59
|
+
endpoint: any,
|
|
60
|
+
...args: any[]
|
|
61
|
+
): Promise<any> {
|
|
62
|
+
useSubscription(endpoint, ...args);
|
|
63
|
+
return useSuspense(endpoint, ...args);
|
|
64
|
+
}
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import { ref, onUnmounted, type Ref } from 'vue';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Takes an async function and tracks resolution as a boolean.
|
|
5
|
+
*
|
|
6
|
+
* @see https://dataclient.io/docs/api/useLoading
|
|
7
|
+
* @param func A function returning a promise
|
|
8
|
+
* @example
|
|
9
|
+
```
|
|
10
|
+
function Button({ onClick, children, ...props }) {
|
|
11
|
+
const [clickHandler, loading] = useLoading(onClick);
|
|
12
|
+
return h('button', { onClick: clickHandler, ...props },
|
|
13
|
+
loading.value ? 'Loading...' : children
|
|
14
|
+
);
|
|
15
|
+
}
|
|
16
|
+
```
|
|
17
|
+
*/
|
|
18
|
+
export default function useLoading<F extends (...args: any) => Promise<any>>(
|
|
19
|
+
func: F,
|
|
20
|
+
): [F, Ref<boolean>, Ref<Error | undefined>] {
|
|
21
|
+
const loading = ref(false);
|
|
22
|
+
const error = ref<undefined | Error>(undefined);
|
|
23
|
+
const isMounted = ref(true);
|
|
24
|
+
|
|
25
|
+
onUnmounted(() => {
|
|
26
|
+
isMounted.value = false;
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
// Create the wrapped function directly - no need for computed in Vue
|
|
30
|
+
const wrappedFunc = (async (...args: any) => {
|
|
31
|
+
loading.value = true;
|
|
32
|
+
error.value = undefined;
|
|
33
|
+
let ret;
|
|
34
|
+
try {
|
|
35
|
+
ret = await func(...args);
|
|
36
|
+
} catch (e: any) {
|
|
37
|
+
error.value = e;
|
|
38
|
+
} finally {
|
|
39
|
+
if (isMounted.value) {
|
|
40
|
+
loading.value = false;
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
return ret;
|
|
44
|
+
}) as F;
|
|
45
|
+
|
|
46
|
+
return [wrappedFunc, loading, error];
|
|
47
|
+
}
|
package/src/index.ts
CHANGED
|
@@ -1,5 +1,9 @@
|
|
|
1
1
|
export { default as useSuspense } from './useSuspense.js';
|
|
2
2
|
export { default as useSubscription } from './useSubscription.js';
|
|
3
3
|
export { default as useQuery } from './useQuery.js';
|
|
4
|
+
export { default as useLive } from './useLive.js';
|
|
5
|
+
export { default as useLoading } from './useLoading.js';
|
|
6
|
+
export { default as useDebounce } from './useDebounce.js';
|
|
7
|
+
export { default as useFetch } from './useFetch.js';
|
|
4
8
|
export { useController as useController } from '../context.js';
|
|
5
9
|
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { Ref } from 'vue';
|
|
2
|
+
/**
|
|
3
|
+
* Keeps value updated after delay time
|
|
4
|
+
*
|
|
5
|
+
* @see https://dataclient.io/docs/api/useDebounce
|
|
6
|
+
* @param value Any immutable value (can be a ref)
|
|
7
|
+
* @param delay Time in milliseconds to wait til updating the value
|
|
8
|
+
* @param updatable Whether to update at all
|
|
9
|
+
* @example
|
|
10
|
+
```
|
|
11
|
+
const [debouncedQuery, isPending] = useDebounce(query, 200);
|
|
12
|
+
const list = useSuspense(getThings, { query: debouncedQuery.value });
|
|
13
|
+
```
|
|
14
|
+
*/
|
|
15
|
+
export default function useDebounce<T>(value: T | Ref<T>, delay: number, updatable?: boolean | Ref<boolean>): [
|
|
16
|
+
Ref<T>,
|
|
17
|
+
Ref<boolean>
|
|
18
|
+
];
|
|
19
|
+
//# sourceMappingURL=useDebounce.d.ts.map
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { EndpointInterface, Denormalize, Schema, FetchFunction, DenormalizeNullable } from '@data-client/core';
|
|
2
|
+
/**
|
|
3
|
+
* Fetch an Endpoint if it is not in cache or stale.
|
|
4
|
+
* Non-suspense; returns the fetch Promise when a request is issued, otherwise undefined.
|
|
5
|
+
* Mirrors React useFetch semantics.
|
|
6
|
+
* @see https://dataclient.io/docs/api/useFetch
|
|
7
|
+
*/
|
|
8
|
+
export default function useFetch<E extends EndpointInterface<FetchFunction, Schema | undefined, undefined | false>>(endpoint: E, ...args: readonly [
|
|
9
|
+
...Parameters<E>
|
|
10
|
+
]): E['schema'] extends undefined | null ? ReturnType<E> : Promise<Denormalize<E['schema']>>;
|
|
11
|
+
export default function useFetch<E extends EndpointInterface<FetchFunction, Schema | undefined, undefined | false>>(endpoint: E, ...args: readonly [
|
|
12
|
+
...Parameters<E>
|
|
13
|
+
] | readonly [
|
|
14
|
+
null
|
|
15
|
+
]): E['schema'] extends undefined | null ? ReturnType<E> | undefined : Promise<DenormalizeNullable<E['schema']>> | undefined;
|
|
16
|
+
//# sourceMappingURL=useFetch.d.ts.map
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { EndpointInterface, Denormalize, Schema, FetchFunction, ResolveType, DenormalizeNullable } from '@data-client/core';
|
|
2
|
+
import { DeepReadonly, ComputedRef } from 'vue';
|
|
3
|
+
/**
|
|
4
|
+
* Ensure an endpoint is available. Keeps it fresh once it is.
|
|
5
|
+
*
|
|
6
|
+
* useSuspense() + useSubscription()
|
|
7
|
+
* @see https://dataclient.io/docs/api/useLive
|
|
8
|
+
* @throws {Promise} If data is not yet available.
|
|
9
|
+
* @throws {NetworkError} If fetch fails.
|
|
10
|
+
*/
|
|
11
|
+
export default function useLive<E extends EndpointInterface<FetchFunction, Schema | undefined, undefined | false>>(endpoint: E, ...args: readonly [
|
|
12
|
+
...Parameters<E>
|
|
13
|
+
]): Promise<DeepReadonly<ComputedRef<E['schema'] extends undefined | null ? ResolveType<E> : Denormalize<E['schema']>>>>;
|
|
14
|
+
export default function useLive<E extends EndpointInterface<FetchFunction, Schema | undefined, undefined | false>>(endpoint: E, ...args: readonly [
|
|
15
|
+
...Parameters<E>
|
|
16
|
+
] | readonly [
|
|
17
|
+
null
|
|
18
|
+
]): Promise<DeepReadonly<ComputedRef<E['schema'] extends undefined | null ? ResolveType<E> | undefined : DenormalizeNullable<E['schema']>>>>;
|
|
19
|
+
//# sourceMappingURL=useLive.d.ts.map
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { Ref } from 'vue';
|
|
2
|
+
/**
|
|
3
|
+
* Takes an async function and tracks resolution as a boolean.
|
|
4
|
+
*
|
|
5
|
+
* @see https://dataclient.io/docs/api/useLoading
|
|
6
|
+
* @param func A function returning a promise
|
|
7
|
+
* @example
|
|
8
|
+
```
|
|
9
|
+
function Button({ onClick, children, ...props }) {
|
|
10
|
+
const [clickHandler, loading] = useLoading(onClick);
|
|
11
|
+
return h('button', { onClick: clickHandler, ...props },
|
|
12
|
+
loading.value ? 'Loading...' : children
|
|
13
|
+
);
|
|
14
|
+
}
|
|
15
|
+
```
|
|
16
|
+
*/
|
|
17
|
+
export default function useLoading<F extends (...args: any) => Promise<any>>(func: F): [
|
|
18
|
+
F,
|
|
19
|
+
Ref<boolean>,
|
|
20
|
+
Ref<Error | undefined>
|
|
21
|
+
];
|
|
22
|
+
//# sourceMappingURL=useLoading.d.ts.map
|
|
@@ -1,11 +0,0 @@
|
|
|
1
|
-
import { Controller as DataController } from '@data-client/core';
|
|
2
|
-
import type { State, Manager, GCInterface } from '@data-client/core';
|
|
3
|
-
export interface ProviderProps {
|
|
4
|
-
managers?: Manager[];
|
|
5
|
-
initialState?: State<unknown>;
|
|
6
|
-
Controller?: typeof DataController;
|
|
7
|
-
gcPolicy?: GCInterface;
|
|
8
|
-
}
|
|
9
|
-
declare const _default: import("vue").DefineComponent<ProviderProps, {}, {}, {}, {}, import("vue").ComponentOptionsMixin, import("vue").ComponentOptionsMixin, {}, string, import("vue").PublicProps, Readonly<ProviderProps> & Readonly<{}>, {}, {}, {}, {}, string, import("vue").ComponentProvideOptions, false, {}, any>;
|
|
10
|
-
export default _default;
|
|
11
|
-
//# sourceMappingURL=DataProvider.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"DataProvider.d.ts","sourceRoot":"","sources":["../../src/components/DataProvider.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,IAAI,cAAc,EAAE,MAAM,mBAAmB,CAAC;AACjE,OAAO,KAAK,EAAE,KAAK,EAAE,OAAO,EAAE,WAAW,EAAE,MAAM,mBAAmB,CAAC;AAKrE,MAAM,WAAW,aAAa;IAC5B,QAAQ,CAAC,EAAE,OAAO,EAAE,CAAC;IACrB,YAAY,CAAC,EAAE,KAAK,CAAC,OAAO,CAAC,CAAC;IAC9B,UAAU,CAAC,EAAE,OAAO,cAAc,CAAC;IACnC,QAAQ,CAAC,EAAE,WAAW,CAAC;CACxB;;AAED,wBAYG"}
|
|
@@ -1,18 +0,0 @@
|
|
|
1
|
-
import { defineComponent } from 'vue';
|
|
2
|
-
import { provideDataClient } from '../providers/provideDataClient.js';
|
|
3
|
-
export default defineComponent({
|
|
4
|
-
name: 'DataProvider',
|
|
5
|
-
props: ['managers', 'initialState', 'Controller', 'gcPolicy'],
|
|
6
|
-
setup(props, {
|
|
7
|
-
slots
|
|
8
|
-
}) {
|
|
9
|
-
provideDataClient({
|
|
10
|
-
Controller: props.Controller,
|
|
11
|
-
gcPolicy: props.gcPolicy,
|
|
12
|
-
initialState: props.initialState,
|
|
13
|
-
managers: props.managers
|
|
14
|
-
});
|
|
15
|
-
return () => slots.default ? slots.default() : null;
|
|
16
|
-
}
|
|
17
|
-
});
|
|
18
|
-
//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJuYW1lcyI6WyJkZWZpbmVDb21w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-client/core';
|
|
3
|
-
import { defineComponent } from 'vue';
|
|
4
|
-
|
|
5
|
-
import { provideDataClient } from '../providers/provideDataClient.js';
|
|
6
|
-
|
|
7
|
-
export interface ProviderProps {
|
|
8
|
-
managers?: Manager[];
|
|
9
|
-
initialState?: State<unknown>;
|
|
10
|
-
Controller?: typeof DataController;
|
|
11
|
-
gcPolicy?: GCInterface;
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
export default defineComponent<ProviderProps>({
|
|
15
|
-
name: 'DataProvider',
|
|
16
|
-
props: ['managers', 'initialState', 'Controller', 'gcPolicy'] as any,
|
|
17
|
-
setup(props, { slots }) {
|
|
18
|
-
provideDataClient({
|
|
19
|
-
Controller: props.Controller,
|
|
20
|
-
gcPolicy: props.gcPolicy,
|
|
21
|
-
initialState: props.initialState,
|
|
22
|
-
managers: props.managers,
|
|
23
|
-
});
|
|
24
|
-
return () => (slots.default ? slots.default() : null);
|
|
25
|
-
},
|
|
26
|
-
});
|