Hone logo
Hone
Problems

Creating a useWakeLock Hook in React for Background Task Management

Modern mobile applications often need to perform tasks even when the app is in the background. WakeLock APIs allow an app to prevent the device from going to sleep, ensuring background tasks continue executing. This challenge asks you to create a reusable React hook, useWakeLock, that simplifies acquiring and releasing a WakeLock in a React component, providing a clean and encapsulated way to manage device wakefulness.

Problem Description

You need to implement a useWakeLock hook in TypeScript for React. This hook should handle the lifecycle of a WakeLock, ensuring it's acquired when the component mounts and released when the component unmounts. The hook should provide a boolean state variable indicating whether the WakeLock is currently active. It should also provide a function to manually release the WakeLock before unmounting.

Key Requirements:

  • Acquire WakeLock on Mount: When the component using the hook mounts, the WakeLock should be acquired.
  • Release WakeLock on Unmount: When the component unmounts, the WakeLock should be released.
  • Manual Release Function: Provide a function that allows the component to manually release the WakeLock before unmounting, useful for scenarios where the task is completed early.
  • State Management: Maintain a boolean state variable (isWakeLockActive) that reflects the current status of the WakeLock.
  • Error Handling: Gracefully handle potential errors during WakeLock acquisition and release. Log errors to the console.
  • Browser Compatibility: The hook should attempt to use the navigator.wakeLock.request() API. If the API is not supported, it should log a warning and not attempt to acquire a WakeLock.

Expected Behavior:

  • The hook should return an array containing:
    • A boolean value (isWakeLockActive) indicating whether the WakeLock is active.
    • A function (releaseWakeLock) to manually release the WakeLock.
  • When the component mounts, isWakeLockActive should become true (assuming WakeLock acquisition is successful).
  • When the component unmounts, isWakeLockActive should become false and the WakeLock should be released.
  • Calling releaseWakeLock should set isWakeLockActive to false and release the WakeLock.
  • If navigator.wakeLock.request() is not available, isWakeLockActive should remain false and a warning should be logged.

Edge Cases to Consider:

  • Browser/Environment Support: The WakeLock API is not universally supported.
  • User Permissions: The user might deny the app permission to keep the device awake.
  • Race Conditions: Ensure proper synchronization when acquiring and releasing the WakeLock.
  • Multiple Instances: Consider how the hook behaves if used in multiple components simultaneously. (For this challenge, assume each component instance manages its own WakeLock.)

Examples

Example 1:

Input: A React component using the useWakeLock hook.
Output: The WakeLock is acquired when the component mounts and released when it unmounts. The isWakeLockActive state reflects the WakeLock status.
Explanation: The hook handles the lifecycle of the WakeLock, ensuring it's properly managed.

Example 2:

Input: A React component using the useWakeLock hook and manually calling releaseWakeLock.
Output: The WakeLock is released immediately when releaseWakeLock is called, and isWakeLockActive becomes false.
Explanation: The manual release function allows for early termination of the background task.

Example 3: (Edge Case - Unsupported Browser)

Input: A React component using the useWakeLock hook in a browser that doesn't support the WakeLock API.
Output: isWakeLockActive remains false, and a warning message is logged to the console. The WakeLock is not attempted to be acquired.
Explanation: The hook gracefully handles the case where the WakeLock API is not available.

Constraints

  • The hook must be written in TypeScript.
  • The hook must use the navigator.wakeLock.request() API.
  • The hook must handle potential errors during WakeLock acquisition and release.
  • The hook must be compatible with React function components.
  • The hook should not introduce any unnecessary dependencies.

Notes

  • Consider using useEffect to manage the WakeLock lifecycle.
  • Think about how to handle potential errors during WakeLock acquisition and release.
  • The navigator.wakeLock.request() API requires user interaction to be initiated before requesting a WakeLock. For the purpose of this exercise, assume this requirement is already met (e.g., the component is triggered by a user action). You do not need to implement the user interaction part.
  • Focus on the core functionality of acquiring and releasing the WakeLock and managing the state. Advanced features like different WakeLock types are beyond the scope of this challenge.
Loading editor...
typescript