Skip to content

Commit

Permalink
Start accepting refs beside simple arguments
Browse files Browse the repository at this point in the history
  • Loading branch information
sistrall committed Nov 22, 2022
1 parent fa9a16e commit bb6554f
Showing 1 changed file with 24 additions and 43 deletions.
67 changes: 24 additions & 43 deletions src/composables/useQuerySubscription/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Ref, ref, watchEffect } from 'vue-demi';
import { Ref, ref, unref, watchEffect } from 'vue-demi';

import {
subscribeToQuery,
Expand All @@ -15,14 +15,14 @@ export type SubscribeToQueryOptions<QueryResult, QueryVariables> = Omit<

export type EnabledQueryListenerOptions<QueryResult, QueryVariables> = {
/** Whether the subscription has to be performed or not */
enabled?: true;
enabled?: true | Ref<boolean>;
/** The initial data to use while the initial request is being performed */
initialData?: QueryResult;
} & SubscribeToQueryOptions<QueryResult, QueryVariables>;

export type DisabledQueryListenerOptions<QueryResult, QueryVariables> = {
/** Whether the subscription has to be performed or not */
enabled: false;
enabled: false | Ref<boolean>;
/** The initial data to use while the initial request is being performed */
initialData?: QueryResult;
} & Partial<SubscribeToQueryOptions<QueryResult, QueryVariables>>;
Expand All @@ -31,59 +31,40 @@ export type QueryListenerOptions<QueryResult, QueryVariables> =
| EnabledQueryListenerOptions<QueryResult, QueryVariables>
| DisabledQueryListenerOptions<QueryResult, QueryVariables>;

export function useQuerySubscription<
export const useQuerySubscription = <
QueryResult = any,
QueryVariables = Record<string, any>,
>(options: QueryListenerOptions<QueryResult, QueryVariables>) {
const { enabled, initialData, ...other } = options;

>(
{ enabled = true, initialData, query, token, ...other }: QueryListenerOptions<QueryResult, QueryVariables>
) => {
const error = ref<ChannelErrorData | null>(null);
const data = ref<QueryResult | null>(null) as Ref<QueryResult | null>;
const status = ref<ConnectionStatus>(
enabled ? 'connecting' : 'closed',
);

const subscribeToQueryOptions = other as EnabledQueryListenerOptions<
QueryResult,
QueryVariables
>;

watchEffect((onCleanup) => {
if (enabled === false) {
status.value = 'closed';

return () => {
// we don't have to perform any uninstall
};
}
const data = ref(unref(initialData) || null);
const status = ref<ConnectionStatus>(unref(enabled) ? "connecting" : "closed");

let unsubscribe: UnsubscribeFn | null;
const subscribeToQueryOptions = other;

async function subscribe() {
unsubscribe = await subscribeToQuery<QueryResult, QueryVariables>({
watchEffect(async (onCleanup) => {
if (query && token && unref(enabled)) {
const unsubscribe = await subscribeToQuery({
...subscribeToQueryOptions,
query,
token,
onStatusChange: (connectionStatus) => {
status.value = connectionStatus;
},
onUpdate: (updateData) => {
onUpdate: ({ response }) => {
error.value = null;
data.value = updateData.response.data;
data.value = response.data;
},
onChannelError: (errorData) => {
data.value = null;
error.value = errorData;
},
});
})

onCleanup(unsubscribe)
}

subscribe();

onCleanup(() => {
if (unsubscribe) {
unsubscribe();
}
})
});

return { error, status, data: data || initialData };
}
})

return { data, status, error }
}

4 comments on commit bb6554f

@vercel
Copy link

@vercel vercel bot commented on bb6554f Nov 22, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

vue-datocms-query-subscription-example – ./examples/query-subscription

vue-datocms-query-subscription-example-git-master-datocms.vercel.app
vue-datocms-query-subscription-example-datocms.vercel.app
vue-datocms-query-subscription-example.vercel.app

@vercel
Copy link

@vercel vercel bot commented on bb6554f Nov 22, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

vue-datocms-vue-cli-babel-javascript-vue2-example – ./examples/vue-cli-babel-javascript-vue2

vue-datocms-vue-cli-babel-javascript-vue2-example.vercel.app
vue-datocms-vue-cli-babel-javascript-vue2-ex-git-00872c-datocms.vercel.app
vue-datocms-vue-cli-babel-javascript-vue2-example-datocms.vercel.app

@vercel
Copy link

@vercel vercel bot commented on bb6554f Nov 22, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

vue-datocms-site-search-example – ./examples/site-search

vue-datocms-site-search-example-git-master-datocms.vercel.app
vue-datocms-site-search-example.vercel.app
vue-datocms-site-search-example-datocms.vercel.app

@vercel
Copy link

@vercel vercel bot commented on bb6554f Nov 22, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

vue-datocms-vite-typescript-vue3-example – ./examples/vite-typescript-vue3

vue-datocms-vite-typescript-vue3-example-git-master-datocms.vercel.app
vue-datocms-vite-typescript-vue3-example.vercel.app
vue-datocms-vite-typescript-vue3-example-datocms.vercel.app

Please sign in to comment.