Composables

useGlobalEvent

Source
Subscribe to a Lynx GlobalEventEmitter event (native-to-JS signals from the host app) with automatic cleanup.

Overview

GlobalEventEmitter is the background-thread channel for native → JS events: Lynx's own (keyboardstatuschanged, exposure), and anything your host app sends with sendGlobalEvent. useGlobalEvent wraps the subscription with component lifecycle, attaching in onMounted and detaching in onUnmounted:

<script setup lang="ts">
import { useGlobalEvent } from '@vyui/core'

useGlobalEvent('connectivitychanged', (...args) => {
  const online = args[0] === 'online'
  // ...
})
</script>

Listeners receive the event's variadic args verbatim, so cast to the payload your host sends. On web / vitest there is no lynx global and the call degrades to a no-op, so components stay portable and mountable without mocks.

Vy UI uses this channel itself: keyboard awareness (keyboardstatuschanged), lazy mounting (exposure / disexposure), and live theme changes (themechanged. See useColorMode). Sparkling handles the sending side for most apps; if you need to build a custom host, see native host integration guide for platform code.

Signature

useGlobalEvent(name: string, listener: (...args: unknown[]) => void, options?: UseGlobalEventOptions): void

  • name is the global event name.
  • listener receives the event's args verbatim.
  • options.immediate subscribes synchronously during setup() instead of onMounted, for events that can fire before mount (Lynx can emit exposure for views in the initial layout). Defaults to false.

Call during setup().