forked from kodadot/nft-gallery
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request kodadot#10764 from Jarsen136/issue-10749
refactor: Optimize subscription with polling
- Loading branch information
Showing
5 changed files
with
295 additions
and
152 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,59 +1,82 @@ | ||
import { createClient } from 'graphql-ws' | ||
import { getWSUrlByClient } from '@/utils/websocket' | ||
import isEqual from 'lodash/isEqual' | ||
import { apolloClientConfig } from '@/utils/constants' | ||
|
||
export default function ({ | ||
clientName = '', | ||
query, | ||
onChange, | ||
onError, | ||
pollingInterval = 6000, | ||
}: { | ||
clientName?: string | ||
query: string | ||
onChange: (data) => void | ||
onError?: (error) => void | ||
pollingInterval?: number | ||
}) { | ||
const { client: prefixClient } = usePrefix() | ||
const { $consola } = useNuxtApp() | ||
const client = clientName || prefixClient.value | ||
const wsUrl = getWSUrlByClient(client) | ||
const httpUrl = apolloClientConfig[client]?.httpEndpoint | ||
|
||
if (!wsUrl) { | ||
// this client do not subscription | ||
if (!httpUrl) { | ||
return () => {} | ||
} | ||
|
||
const wsClient = createClient({ | ||
url: wsUrl, | ||
}) | ||
let lastQueryResult = null | ||
let intervalId: number | null = null | ||
|
||
const isPolling = ref(false) | ||
|
||
wsClient.subscribe( | ||
{ | ||
query: ` | ||
subscription { | ||
async function pollData() { | ||
try { | ||
const response = await $fetch(httpUrl, { | ||
method: 'POST', | ||
body: { | ||
query: ` | ||
query { | ||
${query} | ||
} | ||
`, | ||
/** | ||
* For each entity types, the following queries are supported for subscriptions: | ||
* ${EntityName}ById -- query a single entity | ||
* ${EntityName}s -- query multiple entities with a where filter | ||
* ref: https://docs.subsquid.io/develop-a-squid/graphql-api/subscriptions/ | ||
*/ | ||
}, | ||
{ | ||
next: (data) => { | ||
$consola.log(`ws subscription: New changes: ${JSON.stringify(data)}`) | ||
onChange(data) | ||
}, | ||
error: (error) => { | ||
$consola.error('ws subscription: error', error) | ||
onError && onError(error) | ||
}, | ||
complete: () => { | ||
$consola.log('ws subscription: done!') | ||
}, | ||
}, | ||
) | ||
|
||
return () => wsClient.dispose() | ||
`, | ||
}, | ||
}) | ||
|
||
const newResult = response.data as any | ||
|
||
if (!isEqual(newResult, lastQueryResult)) { | ||
$consola.log(`[Graphql Subscription] New changes: ${JSON.stringify(newResult)}`) | ||
onChange({ data: newResult }) | ||
lastQueryResult = newResult | ||
} | ||
} | ||
catch (error) { | ||
$consola.error('[Graphql Subscription] Polling error:', error) | ||
onError && onError(error) | ||
} | ||
} | ||
|
||
function startPolling() { | ||
if (!isPolling.value) { | ||
isPolling.value = true | ||
intervalId = setInterval(pollData, pollingInterval) as unknown as number | ||
$consola.log('[Graphql Subscription] Started polling') | ||
} | ||
} | ||
|
||
function stopPolling() { | ||
if (isPolling.value && intervalId !== null) { | ||
clearInterval(intervalId) | ||
isPolling.value = false | ||
$consola.log('[Graphql Subscription] Stopped polling') | ||
} | ||
} | ||
|
||
startPolling() | ||
|
||
// Clean up on component unmount | ||
onUnmounted(() => { | ||
stopPolling() | ||
}) | ||
|
||
return stopPolling | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.