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.
Exports
Click an export to jump to it's documentation.
import { } from "@effection-contrib/task-buffer"
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;
});