API reference@evolu/commonTask › createSemaphoreByKey

Call Signature

function createSemaphoreByKey<K>(
  initialPermits: Int1To100OrPositiveInt,
  options?: CreateSemaphoreByKeyOptions<K, unknown>,
): SemaphoreByKey<K>;

Defined in: packages/common/src/Task.ts:6009

Creates a SemaphoreByKey.

Example

import {
  assertOk,
  assertTrue,
  createRun,
  createSemaphoreByKey,
  ok,
  type Task,
} from "@evolu/common";

// Each host gets an independent two-download limit.
const downloadsByHost = createSemaphoreByKey<string>(2);
const download = (host: string, file: string): Task<string> =>
  downloadsByHost.withPermit(host, () => ok(`${host}/${file}`));

await using run = createRun();
assertOk(
  await run(download("a.example", "index.json")),
  "a.example/index.json",
);
assertTrue(downloadsByHost.isIdle("a.example"));

Call Signature

function createSemaphoreByKey<K, L>(
  initialPermits: Int1To100OrPositiveInt,
  options: CreateSemaphoreByKeyOptions<K, L>,
): SemaphoreByKey<K>;

Defined in: packages/common/src/Task.ts:6015

Creates a SemaphoreByKey with custom logical key lookup.