refactor: simplify GraphView event listeners with VueUse

Amp-Thread-ID: https://ampcode.com/threads/T-019bf491-17c4-75d1-a218-85842c463232
Co-authored-by: Amp <amp@ampcode.com>
This commit is contained in:
Alexander Brown
2026-01-25 01:57:23 -08:00
parent 326154f2b2
commit 0a73c5a131

View File

@@ -24,7 +24,7 @@
</template> </template>
<script setup lang="ts"> <script setup lang="ts">
import { useEventListener } from '@vueuse/core' import { useEventListener, useIntervalFn } from '@vueuse/core'
import { storeToRefs } from 'pinia' import { storeToRefs } from 'pinia'
import type { ToastMessageOptions } from 'primevue/toast' import type { ToastMessageOptions } from 'primevue/toast'
import { useToast } from 'primevue/usetoast' import { useToast } from 'primevue/usetoast'
@@ -102,9 +102,6 @@ const { linearMode } = storeToRefs(useCanvasStore())
const telemetry = useTelemetry() const telemetry = useTelemetry()
const firebaseAuthStore = useFirebaseAuthStore() const firebaseAuthStore = useFirebaseAuthStore()
let hasTrackedLogin = false let hasTrackedLogin = false
let visibilityListener: (() => void) | null = null
let tabCountInterval: number | null = null
let tabCountChannel: BroadcastChannel | null = null
watch( watch(
() => colorPaletteStore.completedActivePalette, () => colorPaletteStore.completedActivePalette,
@@ -274,11 +271,12 @@ if (isCloud) {
) )
} }
useEventListener(api, 'status', onStatus)
useEventListener(api, 'execution_success', onExecutionSuccess)
useEventListener(api, 'reconnecting', onReconnecting)
useEventListener(api, 'reconnected', onReconnected)
onMounted(() => { onMounted(() => {
api.addEventListener('status', onStatus)
api.addEventListener('execution_success', onExecutionSuccess)
api.addEventListener('reconnecting', onReconnecting)
api.addEventListener('reconnected', onReconnected)
executionStore.bindExecutionEvents() executionStore.bindExecutionEvents()
try { try {
@@ -291,27 +289,7 @@ onMounted(() => {
}) })
onBeforeUnmount(() => { onBeforeUnmount(() => {
api.removeEventListener('status', onStatus)
api.removeEventListener('execution_success', onExecutionSuccess)
api.removeEventListener('reconnecting', onReconnecting)
api.removeEventListener('reconnected', onReconnected)
executionStore.unbindExecutionEvents() executionStore.unbindExecutionEvents()
// Clean up page visibility listener
if (visibilityListener) {
document.removeEventListener('visibilitychange', visibilityListener)
visibilityListener = null
}
// Clean up tab count tracking
if (tabCountInterval) {
window.clearInterval(tabCountInterval)
tabCountInterval = null
}
if (tabCountChannel) {
tabCountChannel.close()
tabCountChannel = null
}
}) })
useEventListener(window, 'keydown', useKeybindingService().keybindHandler) useEventListener(window, 'keydown', useKeybindingService().keybindHandler)
@@ -337,18 +315,17 @@ const onGraphReady = () => {
} }
// Set up page visibility tracking (cloud only) // Set up page visibility tracking (cloud only)
if (isCloud && telemetry && !visibilityListener) { if (isCloud && telemetry) {
visibilityListener = () => { useEventListener(document, 'visibilitychange', () => {
telemetry.trackPageVisibilityChanged({ telemetry.trackPageVisibilityChanged({
visibility_state: document.visibilityState as 'visible' | 'hidden' visibility_state: document.visibilityState as 'visible' | 'hidden'
}) })
} })
document.addEventListener('visibilitychange', visibilityListener)
} }
// Set up tab count tracking (cloud only) // Set up tab count tracking (cloud only)
if (isCloud && telemetry && !tabCountInterval) { if (isCloud && telemetry) {
tabCountChannel = new BroadcastChannel('comfyui-tab-count') const tabCountChannel = new BroadcastChannel('comfyui-tab-count')
const activeTabs = new Map<string, number>() const activeTabs = new Map<string, number>()
const currentTabId = crypto.randomUUID() const currentTabId = crypto.randomUUID()
@@ -363,7 +340,7 @@ const onGraphReady = () => {
} }
// 5-minute heartbeat interval // 5-minute heartbeat interval
tabCountInterval = window.setInterval(() => { useIntervalFn(() => {
const now = Date.now() const now = Date.now()
// Clean up stale tabs (no heartbeat for 45 seconds) // Clean up stale tabs (no heartbeat for 45 seconds)
@@ -374,7 +351,7 @@ const onGraphReady = () => {
}) })
// Broadcast our heartbeat // Broadcast our heartbeat
tabCountChannel?.postMessage({ tabCountChannel.postMessage({
type: 'heartbeat', type: 'heartbeat',
tabId: currentTabId tabId: currentTabId
}) })