Effection Logo
@effection-contrib/task-bufferv1.0.1thefrontside/effection-contrib
JSR BadgeNPM Badge with published versionBundle size badgeDependency count badgeTree shaking support badge
import { } from "@effection-contrib/task-buffer"

Task Buffer

Manages concurrent task execution by enforcing a maximum limit on simultaneously active operations.

When this limit is reached, the TaskBuffer automatically queues additional spawn requests and processes them in order as capacity becomes available. This prevents resource overload while ensuring all tasks are eventually executed.

import { run, sleep } from "effection";
import { useTaskBuffer } from "@effection-contrib/task-buffer";

await run(function* () {
  // Create a task buffer with a maximum of 2 concurrent tasks
  const buffer = yield* useTaskBuffer(2);

  // These tasks will execute immediately since they're within the limit
  yield* buffer.spawn(() => sleep(10));
  yield* buffer.spawn(() => sleep(10));

  // This task will be queued until one of the running tasks completes
  yield* buffer.spawn(() => sleep(10));

  // Wait for this specific task to complete
  yield* yield* buffer.spawn(() => sleep(10));

  // Wait for all spawned tasks to complete
  yield* buffer;
});

API Reference

function useTaskBuffer(max: number): Operation<TaskBuffer>

Create a new TaskBuffer attached to the current scope. It will not allow its number of active tasks to exceed max.

import { run, sleep } from "effection";
import { useTaskBuffer } from "@effection-contrib/task-buffer";

await run(function*() {
 const buffer = yield* useTaskBuffer(2);

 yield* buffer.spawn(() => sleep(10));
 yield* buffer.spawn(() => sleep(10));
 // the next task won't execute until the above two tasks are completed
 yield* buffer.spawn(() => sleep(10));

 // will wait for all tasks to be complete
 yield* buffer;
});

Parameters

max: number

  • the maximum number of concurrent tasks.

Return Type

Operation<TaskBuffer>

the new task buffer.