@data-client/vue 0.0.2 → 0.0.3

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.
@@ -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,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
+ }
@@ -1,5 +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';
4
7
  export { useController as useController } from '../context.js';
5
8
  //# 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,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,eyJ2ZXJzaW9uIjozLCJuYW1lcyI6WyJkZWZpbmVDb21wb25lbnQiLCJwcm92aWRlRGF0YUNsaWVudCIsIm5hbWUiLCJwcm9wcyIsInNldHVwIiwic2xvdHMiLCJDb250cm9sbGVyIiwiZ2NQb2xpY3kiLCJpbml0aWFsU3RhdGUiLCJtYW5hZ2VycyIsImRlZmF1bHQiXSwic291cmNlcyI6WyIuLi8uLi9zcmMvY29tcG9uZW50cy9EYXRhUHJvdmlkZXIudHMiXSwic291cmNlc0NvbnRlbnQiOlsiaW1wb3J0IHsgQ29udHJvbGxlciBhcyBEYXRhQ29udHJvbGxlciB9IGZyb20gJ0BkYXRhLWNsaWVudC9jb3JlJztcbmltcG9ydCB0eXBlIHsgU3RhdGUsIE1hbmFnZXIsIEdDSW50ZXJmYWNlIH0gZnJvbSAnQGRhdGEtY2xpZW50L2NvcmUnO1xuaW1wb3J0IHsgZGVmaW5lQ29tcG9uZW50IH0gZnJvbSAndnVlJztcblxuaW1wb3J0IHsgcHJvdmlkZURhdGFDbGllbnQgfSBmcm9tICcuLi9wcm92aWRlcnMvcHJvdmlkZURhdGFDbGllbnQuanMnO1xuXG5leHBvcnQgaW50ZXJmYWNlIFByb3ZpZGVyUHJvcHMge1xuICBtYW5hZ2Vycz86IE1hbmFnZXJbXTtcbiAgaW5pdGlhbFN0YXRlPzogU3RhdGU8dW5rbm93bj47XG4gIENvbnRyb2xsZXI/OiB0eXBlb2YgRGF0YUNvbnRyb2xsZXI7XG4gIGdjUG9saWN5PzogR0NJbnRlcmZhY2U7XG59XG5cbmV4cG9ydCBkZWZhdWx0IGRlZmluZUNvbXBvbmVudDxQcm92aWRlclByb3BzPih7XG4gIG5hbWU6ICdEYXRhUHJvdmlkZXInLFxuICBwcm9wczogWydtYW5hZ2VycycsICdpbml0aWFsU3RhdGUnLCAnQ29udHJvbGxlcicsICdnY1BvbGljeSddIGFzIGFueSxcbiAgc2V0dXAoc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26 +0,0 @@
1
- import { Controller as DataController } from '@data-client/core';
2
- import type { State, Manager, GCInterface } from '@data-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
- });
@@ -1,11 +0,0 @@
1
- import { Controller as DataController } from '@data-client/core';
2
- import { 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