Hone logo
Hone
Problems

Reactive Time Slicing in Vue.js with TypeScript

Time slicing, or time-based scheduling, is a common requirement in applications that need to execute tasks at regular intervals. This challenge asks you to implement a reactive time slicing mechanism within a Vue.js component using TypeScript. This will allow you to schedule and execute functions at specified intervals, with the component automatically managing the timer and reacting to changes in the interval duration.

Problem Description

You need to create a Vue.js component that allows users to define a function to be executed and an interval (in milliseconds) at which it should be executed. The component should:

  1. Accept a function (task) and an interval (interval) as props. The task prop should be a function that takes no arguments. The interval prop should be a number representing the interval in milliseconds.
  2. Start a timer when the component is mounted. The timer should execute the provided task function at the specified interval.
  3. Stop the timer when the component is unmounted. This prevents memory leaks.
  4. Allow the interval to be dynamically changed. When the interval prop changes, the existing timer should be cleared, and a new timer should be started with the new interval.
  5. Provide a method to manually stop the timer. This allows the parent component to control the timer's lifecycle.
  6. Handle edge cases: Ensure the component gracefully handles invalid input (e.g., interval being less than or equal to 0, task not being a function). If the task is not a function, do not start the timer. If the interval is invalid, default to a reasonable value (e.g., 1000ms).

Expected Behavior:

  • When the component is mounted, the task function should be executed for the first time immediately.
  • After the initial execution, the task function should be executed repeatedly at the specified interval.
  • When the interval prop changes, the timer should be updated to reflect the new interval.
  • When the component is unmounted, the timer should be stopped.
  • The component should not throw errors if the task prop is not a function or the interval prop is invalid.

Examples

Example 1:

Input: task: () => console.log("Task executed"), interval: 2000
Output: "Task executed" is logged to the console every 2 seconds.
Explanation: The component starts a timer that executes the provided function every 2000 milliseconds.

Example 2:

Input: task: () => console.log("Task executed"), interval: 1000, then interval changes to 5000
Output: "Task executed" is logged to the console every 1 second initially, then every 5 seconds.
Explanation: The component dynamically updates the timer when the interval prop changes.

Example 3: (Edge Case)

Input: task: "not a function", interval: 1000
Output: No timer is started, and no function is executed.
Explanation: The component handles the case where the task prop is not a function.

Constraints

  • The component should be written in Vue.js 3 with TypeScript.
  • The interval prop must be a number greater than 0. If it's not, default to 1000ms.
  • The task prop must be a function. If it's not, the timer should not be started.
  • The component should be performant and avoid unnecessary re-renders.
  • The component should be well-structured and easy to understand.

Notes

  • Consider using setInterval and clearInterval for timer management.
  • Use Vue's reactivity system to ensure that changes to the interval prop are reflected in the timer.
  • Think about how to handle potential errors within the task function. (While not required to implement error handling within the task, ensure your component doesn't crash if the task throws an error).
  • Focus on creating a reusable and robust component that can be easily integrated into other Vue.js applications.
  • Consider using onMounted and onUnmounted lifecycle hooks for managing the timer.
Loading editor...
typescript