import { NitroModules } from 'react-native-nitro-modules'; import type { DeviceInfo, PowerState, NitroDeviceType, BatteryState, NavigationMode, } from './DeviceInfo.nitro'; export type DeviceType = NitroDeviceType; /** * Create the DeviceInfo HybridObject instance * * Each call creates a DeviceInfo HybridObject. For a shared lazy instance, * use DeviceInfoModule. * * @example * ```typescript * import { createDeviceInfo } from 'react-native-nitro-device-info' * * const deviceInfo = createDeviceInfo() * * // Synchronous access * console.log(deviceInfo.deviceId) * console.log(deviceInfo.systemVersion) * * // Asynchronous access * const ipAddress = await deviceInfo.getIpAddress() * const powerState = deviceInfo.getPowerState() * ``` */ export function createDeviceInfo(): DeviceInfo { return NitroModules.createHybridObject('DeviceInfo'); } /** * Lazily-created native singleton. The real HybridObject is instantiated on * first property access, not at module load. * * Why lazy: `createHybridObject` throws in any runtime without Nitro's native * bindings (browser, Node/SSR). On web targets a bundler usually selects * `index.web.ts` instead — but bare server-side rendering (e.g. Next.js SSR, * which ignores the `browser` export condition on the server) can still load * this native module. Deferring instantiation means merely importing the * package never throws; the native binding is only touched when a value is * actually read. SSR component bodies can still read properties, so keep * those reads in client-only code. Nitro caches the constructor, so the * singleton is still created exactly once on first use. */ let nativeInstance: DeviceInfo | undefined; function getNativeInstance(): DeviceInfo { return (nativeInstance ??= createDeviceInfo()); } /** * Lazy singleton instance for convenience * * Use this if you don't need to create multiple instances. * * @example * ```typescript * import { DeviceInfoModule } from 'react-native-nitro-device-info' * * console.log(DeviceInfoModule.deviceId) * const uniqueId = DeviceInfoModule.uniqueId * ``` */ export const DeviceInfoModule: DeviceInfo = new Proxy({} as DeviceInfo, { get(_target, prop) { // Read from the native HybridObject WITHOUT forwarding `receiver`. Passing // the Proxy as `receiver` (e.g. `Reflect.get(instance, prop, receiver)`) // would run Nitro's native getters/methods with `this` bound to the Proxy, // whose target is an empty object with no NativeState — every access then // throws "`this` does not have a NativeState". Reading directly off the // instance keeps `this` bound to the real HybridObject. Methods are bound to // the instance for the same reason: an unbound function called later would // lose its `this`. const instance = getNativeInstance(); const value = instance[prop as keyof DeviceInfo]; return typeof value === 'function' ? value.bind(instance) : value; }, has(_target, prop) { return Reflect.has(getNativeInstance(), prop); }, }); export type { DeviceInfo, PowerState, BatteryState, NavigationMode }; // Re-export React hooks for runtime monitoring export { useBatteryLevel, useBatteryLevelIsLow, usePowerState, useIsHeadphonesConnected, useIsWiredHeadphonesConnected, useIsBluetoothHeadphonesConnected, useBrightness, } from './hooks';