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:
- Accept a function (
task) and an interval (interval) as props. Thetaskprop should be a function that takes no arguments. Theintervalprop should be a number representing the interval in milliseconds. - Start a timer when the component is mounted. The timer should execute the provided
taskfunction at the specifiedinterval. - Stop the timer when the component is unmounted. This prevents memory leaks.
- Allow the interval to be dynamically changed. When the
intervalprop changes, the existing timer should be cleared, and a new timer should be started with the new interval. - Provide a method to manually stop the timer. This allows the parent component to control the timer's lifecycle.
- Handle edge cases: Ensure the component gracefully handles invalid input (e.g.,
intervalbeing less than or equal to 0,tasknot being a function). If thetaskis not a function, do not start the timer. If theintervalis invalid, default to a reasonable value (e.g., 1000ms).
Expected Behavior:
- When the component is mounted, the
taskfunction should be executed for the first time immediately. - After the initial execution, the
taskfunction should be executed repeatedly at the specifiedinterval. - When the
intervalprop 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
taskprop is not a function or theintervalprop 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
intervalprop must be a number greater than 0. If it's not, default to 1000ms. - The
taskprop 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
setIntervalandclearIntervalfor timer management. - Use Vue's reactivity system to ensure that changes to the
intervalprop are reflected in the timer. - Think about how to handle potential errors within the
taskfunction. (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
onMountedandonUnmountedlifecycle hooks for managing the timer.