Constructor
new TaskLimiter(running_limitopt, queued_limitopt)
Create a new task limiter
Name | Type | Attributes | Default | Description |
---|---|---|---|---|
running_limit | number | <optional> | 10 | Initial value for TaskLimiter#running_limit property |
queued_limit | number | <optional> | Infinity | Initial value for TaskLimiter#queued_limit property |
- Source
Classes
Members
(readonly) blocked :number
Number of blocked tasks that have yet to be queued
- number
- Source
(readonly) queued :number
Number of queued tasks
- number
- Source
queued_limit :number
Limits the number of tasks that can be queued. TaskLimiter#add will block until the queue opens up more slots. If you dynamically change this value, the queued task count could rise above this limit temporarily.
Set this to Infinity
for unlimited queue, and no blocked tasks. If not Infinity
, make sure you await the TaskLimiter#add result, otherwise an internal tasks_blocked
list will be filled. This can be zero to have no queue, blocking until a task can be run.
- number
- Source
(readonly) running :number
Number of running tasks
- number
- Source
running_limit :number
Limits the number of async tasks that can be running at the same time. If you
- add a Promise directly to
add
method - dynamically change this value
then the running task count could rise above this limit temporarily.
- number
- Source
(readonly) tasks :number
Number of tasks, either running and queued
- number
- Source
Methods
add(task, nonblocking) → {Promise}
Add a task to be queued
Name | Type | Default | Description |
---|---|---|---|
task | Promise | | This can be a promise, in which case it is considered running already. Otherwise it should be a function representing a task. If the function returns a promise, it is considered an async task and will handled accordingly; if it returns any other type, it is assumed to be a synchronous task. | |
nonblocking | boolean | false | If true, for a task that is not yet running this will send it directly to the queue, disregarding any TaskLimiter#queued_limit. You might use this if you add tasks in a tail recursive manner, knowing that a running slot will open up and bring the queued length back to queued_limit. |
- Source
resolves when the task is queued or run immediately
- Type:
- Promise
blockedBelow(limitopt, nullable) → {Promise}
Returns promise that resolves when TaskLimiter#blocked falls below limit
Name | Type | Attributes | Default | Description |
---|---|---|---|---|
limit | number | <optional> <nullable> | null | null checks if it is below one (e.g. no blocked tasks) |
- Source
- Type:
- Promise
blockedEmpty() → {Promise}
Returns promise that resolves when there are no blocked tasks
- Source
- Type:
- Promise
canHandle() → {Promise}
Returns a promise which resolves when a task can be queued or run. See TaskLimiter#canRun and TaskLimiter#canQueue methods. This is an alias for tasksBelow(null)
call.
- Source
- Type:
- Promise
canQueue() → {Promise}
Returns a promise which resolves when a task can be queued. This is an alias for queuedBelow(null)
call.
- Source
- Type:
- Promise
canRun() → {Promise}
Returns a promise which resolves when a task can be run. This is an alias for runningBelow(null)
call.
- Source
- Type:
- Promise
queuedBelow(limitopt, nullable) → {Promise}
Returns promise that resolves when TaskLimiter#queued falls below limit
Name | Type | Attributes | Default | Description |
---|---|---|---|---|
limit | number | <optional> <nullable> | null | null checks if it is below TaskLimiter#queued_limit |
- Source
- Type:
- Promise
queuedEmpty() → {Promise}
Returns promise that resolves when there are no queued tasks
- Source
- Type:
- Promise
runningBelow(limitopt, nullable) → {Promise}
Returns promise that resolves when TaskLimiter#running falls below limit
Name | Type | Attributes | Default | Description |
---|---|---|---|---|
limit | number | <optional> <nullable> | null | null checks if it is below TaskLimiter#running_limit |
- Source
- Type:
- Promise
runningEmpty() → {Promise}
Returns promise that resolves when there are no running tasks
- Source
- Type:
- Promise
tasksBelow(limitopt, nullable) → {Promise}
Returns promise that resolves when TaskLimiter#tasks falls below limit
Name | Type | Attributes | Default | Description |
---|---|---|---|---|
limit | number | <optional> <nullable> | null | null checks if it is below the sum of TaskLimiter#running_limit and TaskLimiter#queued_limit |
- Source
- Type:
- Promise
tasksEmpty() → {Promise}
Returns promise that resolves when there are no running or queued tasks
- Source
- Type:
- Promise