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

Task Buffer

Spawn operations, but only allow a certain number to be active at a given time. Once the TaskBuffer becomes full, it will queue up spawn operations until room becomes available.

API

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;
});