Skip to content

Commit

Permalink
fix: fix ws client dispose
Browse files Browse the repository at this point in the history
  • Loading branch information
Pascal Klesse committed Nov 13, 2024
1 parent 754951d commit 86d9553
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 5 deletions.
8 changes: 5 additions & 3 deletions src/runtime/composables/gql-subscription.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import type { Client } from 'graphql-ws';

import { subscription } from 'gql-query-builder';
import gql from 'graphql-tag';
import { useNuxtApp } from 'nuxt/app';
Expand Down Expand Up @@ -137,13 +139,13 @@ export async function gqlSubscription<T = any>(method: string, options: IGraphQL
const data = ref(null);
const error = ref(null);
const loading = ref(true);
let subscriptionState = null;
let subscriptionState: Client | null = null;

const start = () => {
loading.value = true;
error.value = null;

subscriptionState = _wsClient.subscribe(
subscriptionState = (_wsClient as any).subscribe(
{ query: queryBody.query, variables: variables },
{
complete: () => {
Expand All @@ -168,7 +170,7 @@ export async function gqlSubscription<T = any>(method: string, options: IGraphQL

const stop = () => {
if (subscriptionState) {
subscriptionState.unsubscribe();
subscriptionState.dispose();
subscriptionState = null;
}
};
Expand Down
4 changes: 2 additions & 2 deletions src/runtime/plugins/ws.client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,13 @@ export default defineNuxtPlugin({
closed: async (event) => {
const { checkTokenAndRenew } = useAuth();

if (event.code === 4500) {
if (event?.code === 4500) {
await checkTokenAndRenew();
}
},
},
retryAttempts: 5,
url: wsUrl,
url: wsUrl as string,
});

nuxtApp._wsClient = client;
Expand Down

0 comments on commit 86d9553

Please sign in to comment.