Hyperlinkv0.8.0-beta.28

RequestResolver

RequestResolver.RequestResolverinterfaceeffect/RequestResolver.ts:77
RequestResolver<A>

A resolver that executes and completes batched Request entries.

Details

A resolver controls how requests are grouped, delayed, optionally pre-checked, and finally run. Its runAll method receives a non-empty batch of Request.Entry values for a single batch key and must complete every received entry, usually by calling completeUnsafe or one of the Request completion helpers.

Gotchas

If a resolver finishes without completing an entry, the waiting request fails because the resolver did not supply a result.

Example (Defining a request resolver)

import { Effect, Exit, RequestResolver } from "effect"
import type { Request } from "effect"

interface GetUserRequest extends Request.Request<string, Error> {
  readonly _tag: "GetUserRequest"
  readonly id: number
}

// In practice, you would typically use RequestResolver.make() instead
const resolver = RequestResolver.make<GetUserRequest>((entries) =>
  Effect.sync(() => {
    for (const entry of entries) {
      entry.completeUnsafe(Exit.succeed(`User ${entry.request.id}`))
    }
  })
)
models
export interface RequestResolver<in A extends Request.Any> extends RequestResolver.Variance<A>, Pipeable {
  readonly delay: Effect.Effect<void>

  /**
   * Get a batch key for the given request.
   */
  batchKey(entry: Request.Entry<A>): unknown

  /**
   * An optional pre-check function that can be used to filter requests before
   * they are added to a batch. If the function returns `false`, the request
   * will not be processed.
   */
  readonly preCheck: ((entry: Request.Entry<A>) => boolean) | undefined

  /**
   * Should the resolver continue collecting requests? Otherwise, it will
   * immediately execute the collected requests cutting the delay short.
   */
  collectWhile(entries: ReadonlySet<Request.Entry<A>>): boolean

  /**
   * Execute a collection of requests.
   */
  runAll(entries: NonEmptyArray<Request.Entry<A>>, key: unknown): Effect.Effect<void, Request.Error<A>>
}

/**
 * Namespace containing type-level helpers associated with `RequestResolver`.
 *
 * @since 2.0.0
 */
export declare namespace RequestResolver {
  /**
   * Variance marker carried by every `RequestResolver`.
   *
   * **Details**
   *
   * This marker preserves the request type accepted by the resolver for
   * Effect's type-level machinery. Users normally do not implement it directly.
   *
   * @category models
   * @since 2.0.0
   */
  export interface Variance<in A> {
    readonly [TypeId]: {
      readonly _A: Types.Contravariant<A>
    }
  }
}
Referenced by 21 symbols