@dmop/puru 0.1.11 → 0.1.13

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.d.cts CHANGED
@@ -9,9 +9,9 @@ type StructuredCloneValue = void | null | undefined | string | number | boolean
9
9
  type ChannelValue = Exclude<StructuredCloneValue, null>;
10
10
  interface PuruConfig {
11
11
  maxThreads: number;
12
- strategy: 'fifo' | 'work-stealing';
12
+ strategy: "fifo" | "work-stealing";
13
13
  idleTimeout: number;
14
- adapter: 'auto' | 'node' | 'bun' | 'inline';
14
+ adapter: "auto" | "node" | "bun" | "inline";
15
15
  concurrency: number;
16
16
  }
17
17
  interface SpawnResult<T> {
@@ -213,7 +213,7 @@ declare function withValue<T = unknown>(parent: Context, key: symbol | string, v
213
213
  * }, { channels: { ch } })
214
214
  */
215
215
  declare function spawn<T extends StructuredCloneValue, TChannels extends Record<string, Channel<ChannelValue>> = Record<never, never>>(fn: (() => T | Promise<T>) | ((channels: TChannels) => T | Promise<T>), opts?: {
216
- priority?: 'low' | 'normal' | 'high';
216
+ priority?: "low" | "normal" | "high";
217
217
  concurrent?: boolean;
218
218
  channels?: TChannels;
219
219
  ctx?: Context;
@@ -418,6 +418,77 @@ declare class RWMutex {
418
418
  private wakeWriter;
419
419
  }
420
420
 
421
+ /**
422
+ * Weighted counting semaphore. Limits concurrent access to a resource where
423
+ * each acquisition can have a different cost. Modeled after Go's
424
+ * `golang.org/x/sync/semaphore.Weighted`.
425
+ *
426
+ * A `Mutex` is equivalent to `new Semaphore(1)` — one holder at a time.
427
+ * A `Semaphore` generalizes this to N units, with variable-weight acquisitions.
428
+ *
429
+ * Like `Mutex`, this operates on the main thread only. For cross-thread
430
+ * coordination, use channels instead.
431
+ *
432
+ * @example
433
+ * // Limit to 10 concurrent DB connections
434
+ * const sem = new Semaphore(10)
435
+ *
436
+ * await sem.acquire() // take 1 slot
437
+ * try {
438
+ * await db.query(...)
439
+ * } finally {
440
+ * sem.release()
441
+ * }
442
+ *
443
+ * @example
444
+ * // Weighted: heavy queries cost more
445
+ * const sem = new Semaphore(10)
446
+ *
447
+ * await sem.acquire(5) // heavy query takes 5 slots
448
+ * sem.release(5)
449
+ *
450
+ * @example
451
+ * // withAcquire — recommended (auto-releases on error)
452
+ * const result = await sem.withAcquire(async () => {
453
+ * return await fetch(url)
454
+ * })
455
+ *
456
+ * @example
457
+ * // Non-blocking check
458
+ * if (sem.tryAcquire(3)) {
459
+ * try { ... } finally { sem.release(3) }
460
+ * }
461
+ */
462
+ declare class Semaphore {
463
+ private max;
464
+ private cur;
465
+ private queue;
466
+ constructor(n: number);
467
+ /**
468
+ * Acquire `n` permits, waiting if necessary until they are available.
469
+ * Acquires are served in FIFO order.
470
+ */
471
+ acquire(n?: number): Promise<void>;
472
+ /**
473
+ * Try to acquire `n` permits without blocking.
474
+ * Returns `true` if successful, `false` if not enough permits are available.
475
+ */
476
+ tryAcquire(n?: number): boolean;
477
+ /**
478
+ * Release `n` permits, potentially waking queued acquirers.
479
+ */
480
+ release(n?: number): void;
481
+ /**
482
+ * Acquire `n` permits, run `fn`, then release automatically — even if `fn` throws.
483
+ */
484
+ withAcquire<T>(fn: () => T | Promise<T>, n?: number): Promise<T>;
485
+ /** Number of permits currently available. */
486
+ get available(): number;
487
+ /** Total capacity of the semaphore. */
488
+ get capacity(): number;
489
+ private wake;
490
+ }
491
+
421
492
  /**
422
493
  * Condition variable for async coordination. Modeled after Go's `sync.Cond`.
423
494
  *
@@ -763,9 +834,9 @@ declare function resize(maxThreads: number): void;
763
834
  */
764
835
  declare function shutdown(): Promise<void>;
765
836
 
766
- type Runtime = 'node' | 'deno' | 'bun' | 'browser';
767
- type Capability = 'full-threads' | 'single-thread';
837
+ type Runtime = "node" | "deno" | "bun" | "browser";
838
+ type Capability = "full-threads" | "single-thread";
768
839
  declare function detectRuntime(): Runtime;
769
840
  declare function detectCapability(): Capability;
770
841
 
771
- export { type CancelFunc, CancelledError, type Capability, type Channel, Cond, type Context, type ContextError, DeadlineExceededError, ErrGroup, Mutex, Once, type PoolStats, type PuruConfig, RWMutex, type RecvOnly, type Runtime, type SelectOptions, type SendOnly, type SpawnResult, Ticker, Timer, WaitGroup, after, background, chan, configure, detectCapability, detectRuntime, resize, select, shutdown, spawn, stats, task, ticker, withCancel, withDeadline, withTimeout, withValue };
842
+ export { type CancelFunc, CancelledError, type Capability, type Channel, Cond, type Context, type ContextError, DeadlineExceededError, ErrGroup, Mutex, Once, type PoolStats, type PuruConfig, RWMutex, type RecvOnly, type Runtime, type SelectOptions, Semaphore, type SendOnly, type SpawnResult, Ticker, Timer, WaitGroup, after, background, chan, configure, detectCapability, detectRuntime, resize, select, shutdown, spawn, stats, task, ticker, withCancel, withDeadline, withTimeout, withValue };
package/dist/index.d.ts CHANGED
@@ -9,9 +9,9 @@ type StructuredCloneValue = void | null | undefined | string | number | boolean
9
9
  type ChannelValue = Exclude<StructuredCloneValue, null>;
10
10
  interface PuruConfig {
11
11
  maxThreads: number;
12
- strategy: 'fifo' | 'work-stealing';
12
+ strategy: "fifo" | "work-stealing";
13
13
  idleTimeout: number;
14
- adapter: 'auto' | 'node' | 'bun' | 'inline';
14
+ adapter: "auto" | "node" | "bun" | "inline";
15
15
  concurrency: number;
16
16
  }
17
17
  interface SpawnResult<T> {
@@ -213,7 +213,7 @@ declare function withValue<T = unknown>(parent: Context, key: symbol | string, v
213
213
  * }, { channels: { ch } })
214
214
  */
215
215
  declare function spawn<T extends StructuredCloneValue, TChannels extends Record<string, Channel<ChannelValue>> = Record<never, never>>(fn: (() => T | Promise<T>) | ((channels: TChannels) => T | Promise<T>), opts?: {
216
- priority?: 'low' | 'normal' | 'high';
216
+ priority?: "low" | "normal" | "high";
217
217
  concurrent?: boolean;
218
218
  channels?: TChannels;
219
219
  ctx?: Context;
@@ -418,6 +418,77 @@ declare class RWMutex {
418
418
  private wakeWriter;
419
419
  }
420
420
 
421
+ /**
422
+ * Weighted counting semaphore. Limits concurrent access to a resource where
423
+ * each acquisition can have a different cost. Modeled after Go's
424
+ * `golang.org/x/sync/semaphore.Weighted`.
425
+ *
426
+ * A `Mutex` is equivalent to `new Semaphore(1)` — one holder at a time.
427
+ * A `Semaphore` generalizes this to N units, with variable-weight acquisitions.
428
+ *
429
+ * Like `Mutex`, this operates on the main thread only. For cross-thread
430
+ * coordination, use channels instead.
431
+ *
432
+ * @example
433
+ * // Limit to 10 concurrent DB connections
434
+ * const sem = new Semaphore(10)
435
+ *
436
+ * await sem.acquire() // take 1 slot
437
+ * try {
438
+ * await db.query(...)
439
+ * } finally {
440
+ * sem.release()
441
+ * }
442
+ *
443
+ * @example
444
+ * // Weighted: heavy queries cost more
445
+ * const sem = new Semaphore(10)
446
+ *
447
+ * await sem.acquire(5) // heavy query takes 5 slots
448
+ * sem.release(5)
449
+ *
450
+ * @example
451
+ * // withAcquire — recommended (auto-releases on error)
452
+ * const result = await sem.withAcquire(async () => {
453
+ * return await fetch(url)
454
+ * })
455
+ *
456
+ * @example
457
+ * // Non-blocking check
458
+ * if (sem.tryAcquire(3)) {
459
+ * try { ... } finally { sem.release(3) }
460
+ * }
461
+ */
462
+ declare class Semaphore {
463
+ private max;
464
+ private cur;
465
+ private queue;
466
+ constructor(n: number);
467
+ /**
468
+ * Acquire `n` permits, waiting if necessary until they are available.
469
+ * Acquires are served in FIFO order.
470
+ */
471
+ acquire(n?: number): Promise<void>;
472
+ /**
473
+ * Try to acquire `n` permits without blocking.
474
+ * Returns `true` if successful, `false` if not enough permits are available.
475
+ */
476
+ tryAcquire(n?: number): boolean;
477
+ /**
478
+ * Release `n` permits, potentially waking queued acquirers.
479
+ */
480
+ release(n?: number): void;
481
+ /**
482
+ * Acquire `n` permits, run `fn`, then release automatically — even if `fn` throws.
483
+ */
484
+ withAcquire<T>(fn: () => T | Promise<T>, n?: number): Promise<T>;
485
+ /** Number of permits currently available. */
486
+ get available(): number;
487
+ /** Total capacity of the semaphore. */
488
+ get capacity(): number;
489
+ private wake;
490
+ }
491
+
421
492
  /**
422
493
  * Condition variable for async coordination. Modeled after Go's `sync.Cond`.
423
494
  *
@@ -763,9 +834,9 @@ declare function resize(maxThreads: number): void;
763
834
  */
764
835
  declare function shutdown(): Promise<void>;
765
836
 
766
- type Runtime = 'node' | 'deno' | 'bun' | 'browser';
767
- type Capability = 'full-threads' | 'single-thread';
837
+ type Runtime = "node" | "deno" | "bun" | "browser";
838
+ type Capability = "full-threads" | "single-thread";
768
839
  declare function detectRuntime(): Runtime;
769
840
  declare function detectCapability(): Capability;
770
841
 
771
- export { type CancelFunc, CancelledError, type Capability, type Channel, Cond, type Context, type ContextError, DeadlineExceededError, ErrGroup, Mutex, Once, type PoolStats, type PuruConfig, RWMutex, type RecvOnly, type Runtime, type SelectOptions, type SendOnly, type SpawnResult, Ticker, Timer, WaitGroup, after, background, chan, configure, detectCapability, detectRuntime, resize, select, shutdown, spawn, stats, task, ticker, withCancel, withDeadline, withTimeout, withValue };
842
+ export { type CancelFunc, CancelledError, type Capability, type Channel, Cond, type Context, type ContextError, DeadlineExceededError, ErrGroup, Mutex, Once, type PoolStats, type PuruConfig, RWMutex, type RecvOnly, type Runtime, type SelectOptions, Semaphore, type SendOnly, type SpawnResult, Ticker, Timer, WaitGroup, after, background, chan, configure, detectCapability, detectRuntime, resize, select, shutdown, spawn, stats, task, ticker, withCancel, withDeadline, withTimeout, withValue };