Download mobile/modules/bluetooth-sdk/src/react/useBluetoothEvent.ts from Anirudh1950/MentraOS: direct link, hf CLI and curl.
- Browser
- Download file 903 Bytes
-
https://huggingface.co/Anirudh1950/MentraOS/resolve/main/mobile/modules/bluetooth-sdk/src/react/useBluetoothEvent.ts
- Command line
-
hf download hf://Anirudh1950/MentraOS/mobile/modules/bluetooth-sdk/src/react/useBluetoothEvent.ts
-
curl -L -o useBluetoothEvent.ts https://huggingface.co/Anirudh1950/MentraOS/resolve/main/mobile/modules/bluetooth-sdk/src/react/useBluetoothEvent.ts
903 Bytes
| import {useEffect, useRef} from "react" | |
| import BluetoothSdk from "../index" | |
| import type { | |
| BluetoothSdkEventListener, | |
| BluetoothSdkEventName, | |
| } from "../BluetoothSdk.types" | |
| export type UseBluetoothEventOptions = { | |
| enabled?: boolean | |
| } | |
| export function useBluetoothEvent<EventName extends BluetoothSdkEventName>( | |
| eventName: EventName, | |
| listener: BluetoothSdkEventListener<EventName>, | |
| options: UseBluetoothEventOptions = {}, | |
| ): void { | |
| const enabled = options.enabled ?? true | |
| const listenerRef = useRef(listener) | |
| useEffect(() => { | |
| listenerRef.current = listener | |
| }, [listener]) | |
| useEffect(() => { | |
| if (!enabled) { | |
| return undefined | |
| } | |
| const subscription = BluetoothSdk.addListener(eventName, (event) => { | |
| listenerRef.current(event) | |
| }) | |
| return () => { | |
| subscription.remove() | |
| } | |
| }, [enabled, eventName]) | |
| } | |