Hyperlinkv0.8.0-beta.28

PartitionedSemaphore

PartitionedSemaphore.withPermitsIfAvailableconsteffect/PartitionedSemaphore.ts:544
<K>(self: PartitionedSemaphore<K>, permits: number): <A, E, R>(
  effect: Effect.Effect<A, E, R>
) => Effect.Effect<Option.Option<A>, E, R>
<K, A, E, R>(
  self: PartitionedSemaphore<K>,
  permits: number,
  effect: Effect.Effect<A, E, R>
): Effect.Effect<Option.Option<A>, E, R>

Runs an effect only when the requested permits can be acquired immediately, returning the result in Some.

When to use

Use when guarded work should run only if the shared permit pool can provide the requested permits immediately.

Details

If the permits are not available, the effect is not run and the result is None. When permits are acquired, they are released after the wrapped effect completes, fails, or is interrupted. Requests for zero or a negative number of permits run the effect and return Some.

combinatorswithPermits
export const withPermitsIfAvailable: {
  <K>(
    self: PartitionedSemaphore<K>,
    permits: number
  ): <A, E, R>(effect: Effect.Effect<A, E, R>) => Effect.Effect<Option.Option<A>, E, R>
  <K, A, E, R>(
    self: PartitionedSemaphore<K>,
    permits: number,
    effect: Effect.Effect<A, E, R>
  ): Effect.Effect<Option.Option<A>, E, R>
} = ((...args: Array<any>) => {
  if (args.length === 2) {
    const [self, permits] = args
    return (effect: Effect.Effect<any, any, any>) => self.withPermitsIfAvailable(permits)(effect)
  }
  const [self, permits, effect] = args
  return self.withPermitsIfAvailable(permits)(effect)
}) as any