Problem Statement
In high-performance IDEs and AI-assisted editors, managing background tasks is critical. You need to implement a Priority Task Queue with Concurrency Control.
Your task is to create a TaskScheduler class that can handle multiple tasks with different priorities (High, Medium, Low). The scheduler should limit the number of concurrently running tasks to avoid slowing down the editor's main thread or overwhelming the AI service APIs.
Requirements
- Concurrency Limit: The scheduler should accept a
maxConcurrencyvalue in its constructor. - Priority Levels: Tasks are added with a priority:
0(High),1(Medium), and2(Low). High-priority tasks should always run before lower-priority ones. - Task Execution: Tasks are asynchronous functions that return a Promise.
- Result Management: The scheduler should return a promise that resolves with the task's result when it completes.
Example
Constraints
- The number of active promises must never exceed
maxConcurrency. - If a High priority task enters the queue while the scheduler is full, it must be the next task to run once a slot opens up.