import type * as Cause from "../Cause.ts"; import * as Context from "../Context.ts"; import * as Data from "../Data.ts"; import type * as Duration from "../Duration.ts"; import * as Effect from "../Effect.ts"; import * as Equal from "../Equal.ts"; import * as Exit from "../Exit.ts"; import type * as Latch from "../Latch.ts"; import * as Layer from "../Layer.ts"; import * as Option from "../Option.ts"; import * as Queue from "../Queue.ts"; import * as Rpc from "../rpc/Rpc.ts"; import * as RpcClient from "../rpc/RpcClient.ts"; import * as RpcGroup from "../rpc/RpcGroup.ts"; import type * as Schedule from "../Schedule.ts"; import { Scope } from "../Scope.ts"; import * as Stream from "../Stream.ts"; import type { AlreadyProcessingMessage, EntityNotAssignedToRunner, MailboxFull, PersistenceError } from "./ClusterError.ts"; import { EntityAddress } from "./EntityAddress.ts"; import type { EntityId } from "./EntityId.ts"; import { EntityType } from "./EntityType.ts"; import * as Envelope from "./Envelope.ts"; import type * as Reply from "./Reply.ts"; import { RunnerAddress } from "./RunnerAddress.ts"; import * as ShardId from "./ShardId.ts"; import type { Sharding } from "./Sharding.ts"; import { ShardingConfig } from "./ShardingConfig.ts"; declare const TypeId = "~effect/cluster/Entity"; /** * Represents a cluster entity type and the RPC protocol it can handle. * * **Details** * * An entity defines how ids map to shard groups, exposes a sharded client, and * can be registered as a layer using RPC handlers or a mailbox queue. * * @stability unstable * @category models * @since 4.0.0 */ export interface Entity extends Equal.Equal { readonly [TypeId]: typeof TypeId; /** * The name of the entity type. */ readonly type: EntityType; /** * A RpcGroup definition for messages which represents the messaging protocol * that the entity is capable of processing. */ readonly protocol: RpcGroup.RpcGroup; /** * Get the shard group for the given EntityId. */ getShardGroup(entityId: EntityId): string; /** * Get the ShardId for the given EntityId. */ getShardId(entityId: EntityId): Effect.Effect; /** * Annotate the entity with a value. */ annotate(key: Context.Key, value: S): Entity; /** * Annotate the Rpc's above this point with a value. */ annotateRpcs(key: Context.Key, value: S): Entity; /** * Annotate the entity with the given annotations. */ annotateMerge(annotation: Context.Context): Entity; /** * Annotate the Rpc's above this point with a context object. */ annotateRpcsMerge(context: Context.Context): Entity; /** * Create a client for this entity. */ readonly client: Effect.Effect<(entityId: string) => RpcClient.RpcClient.From, never, Sharding>; /** * Create a Layer from an Entity. * * **Details** * * It will register the entity with the Sharding service. */ toLayer, RX = never>(build: Handlers | Effect.Effect, options?: { readonly maxIdleTime?: Duration.Input | undefined; readonly concurrency?: number | "unbounded" | undefined; readonly mailboxCapacity?: number | "unbounded" | undefined; readonly mailboxMetrics?: boolean | undefined; readonly disableFatalDefects?: boolean | undefined; readonly defectRetryPolicy?: Schedule.Schedule | undefined; readonly spanAttributes?: Record | undefined; }): Layer.Layer | RpcGroup.HandlersServices | Rpc.ServicesClient | Rpc.ServicesServer | Rpc.Middleware | Sharding>; of>(handlers: Handlers): Handlers; /** * Create a Layer from an Entity. * * **Details** * * It will register the entity with the Sharding service. */ toLayerQueue(build: ((queue: Queue.Dequeue>, replier: Replier) => Effect.Effect) | Effect.Effect<(queue: Queue.Dequeue>, replier: Replier) => Effect.Effect, never, RX>, options?: { readonly maxIdleTime?: Duration.Input | undefined; readonly mailboxCapacity?: number | "unbounded" | undefined; readonly mailboxMetrics?: boolean | undefined; readonly disableFatalDefects?: boolean | undefined; readonly defectRetryPolicy?: Schedule.Schedule | undefined; readonly spanAttributes?: Record | undefined; }): Layer.Layer | R | Rpc.ServicesClient | Rpc.ServicesServer | Rpc.Middleware | Sharding>; } /** * Type alias for any cluster `Entity`, regardless of entity type or RPC * protocol. * * @stability unstable * @category models * @since 4.0.0 */ export type Any = Entity; /** * Maps each RPC in an entity protocol to the handler function expected by * `Entity.toLayer`. * * **Details** * * Each handler receives the entity request envelope for that RPC and returns the * RPC result or a supported RPC wrapper. * * @stability unstable * @category utility types * @since 4.0.0 */ export type HandlersFrom = { readonly [Current in Rpc as Current["_tag"]]: (envelope: Request) => Rpc.WrapperOr>; }; /** * Returns `true` when the supplied value is a cluster `Entity`. * * **Details** * * The check is based on the internal entity type identifier. * * @stability unstable * @category guards * @since 4.0.0 */ export declare const isEntity: (u: unknown) => u is Any; /** * Creates a new `Entity` of the specified `type` which will accept messages * that adhere to the provided `RpcGroup`. * * @stability unstable * @category constructors * @since 4.0.0 */ export declare const fromRpcGroup: ( /** * The entity type name. */ type: Type, /** * The schema definition for messages that the entity is capable of * processing. */ protocol: RpcGroup.RpcGroup) => Entity; /** * Creates a new `Entity` of the specified `type` which will accept messages * that adhere to the provided schemas. * * **When to use** * * Use to define a cluster entity from individual `Rpc` definitions, giving the * cluster runtime a typed protocol for handlers and per-entity clients. * * **Details** * * The `type` argument is stored as the entity `EntityType`, and the RPC array * is grouped into the entity's `protocol`. * * **Gotchas** * * RPC tags should be unique within the array. If multiple definitions use the * same tag, the resulting protocol keeps the later definition for that tag. * * @see {@link fromRpcGroup} for creating an entity from an existing `RpcGroup` * * @stability unstable * @category constructors * @since 4.0.0 */ export declare const make: >( /** * The entity type name. */ type: Type, /** * The schema definition for messages that the entity is capable of * processing. */ protocol: Rpcs) => Entity; declare const CurrentAddress_base: Context.ServiceClass; /** * Service tag for the entity address currently being processed. * * **When to use** * * Use to read the current entity identity and shard address from entity * handlers and keep-alive logic. * * @stability unstable * @category services * @since 4.0.0 */ export declare class CurrentAddress extends CurrentAddress_base { } declare const CurrentRunnerAddress_base: Context.ServiceClass; /** * Service tag for the runner address currently registering entity handlers. * * **When to use** * * Use to read the runner address associated with the current entity handler * registration. * * @stability unstable * @category services * @since 4.0.0 */ export declare class CurrentRunnerAddress extends CurrentRunnerAddress_base { } /** * Reply API passed to queue-based entity handlers. * * **When to use** * * Use when you use it to complete an entity request by succeeding, failing, failing with a * cause, or supplying an explicit `Exit`. * * @stability unstable * @category models * @since 4.0.0 */ export interface Replier { readonly succeed: (request: Envelope.Request, value: Replier.Success) => Effect.Effect; readonly fail: (request: Envelope.Request, error: Rpc.Error) => Effect.Effect; readonly failCause: (request: Envelope.Request, cause: Cause.Cause>) => Effect.Effect; readonly complete: (request: Envelope.Request, exit: Exit.Exit, Rpc.Error>) => Effect.Effect; } /** * Helper types used by the `Replier` API. * * @stability unstable * @since 4.0.0 */ export declare namespace Replier { /** * Success value accepted by a `Replier` for a single RPC. * * **Details** * * For streaming RPCs this may be either a stream of success chunks or a dequeue * of success chunks. For non-streaming RPCs it is the RPC success value. * * @stability unstable * @category utility types * @since 4.0.0 */ type Success = Rpc.Success extends Stream.Stream ? Stream.Stream<_A, _E | Rpc.Error, _R> | Queue.Dequeue<_A, _E | Rpc.Error | Cause.Done> : Rpc.Success; } /** * Represents an entity request envelope delivered to entity handlers. * * **Details** * * It includes the underlying request envelope plus the last stream reply chunk * that was sent, allowing handlers to resume chunk sequencing after a restart. * * @stability unstable * @category models * @since 4.0.0 */ export declare class Request extends Data.Class & { readonly lastSentChunk: Option.Option>; }> { /** * Most recent success chunk value sent by the entity, when one exists. * * @stability unstable * @since 4.0.0 */ get lastSentChunkValue(): Option.Option>; /** * Sequence number to use for the entity's next outgoing success chunk. * * @stability unstable * @since 4.0.0 */ get nextSequence(): number; } /** * Builds an in-memory test client for an entity layer. * * **Details** * * The returned function creates a no-serialization RPC client for each entity ID, * using a test sharding service instead of the cluster transport. * * @stability unstable * @category testing * @since 4.0.0 */ export declare const makeTestClient: (entity: Entity, layer: Layer.Layer) => Effect.Effect<(entityId: string) => Effect.Effect>, LE, Scope | ShardingConfig | Exclude | Rpc.MiddlewareClient>; /** * Enables or disables keep-alive for the current entity. * * **Details** * * When enabled it sends the internal keep-alive RPC for the current address; when * disabled it releases the keep-alive latch if one is present. * * @stability unstable * @category keep alive * @since 4.0.0 */ export declare const keepAlive: (enabled: boolean) => Effect.Effect; /** * RPC used internally to keep an entity active while a resource is held. * * **Details** * * The RPC is marked as persisted and uninterruptible so the keep-alive signal * survives normal entity restarts. * * @stability unstable * @category keep alive * @since 4.0.0 */ export declare const KeepAliveRpc: Rpc.Rpc<"Cluster/Entity/keepAlive", import("../Schema.ts").Void, import("../Schema.ts").Void, import("../Schema.ts").Never, never, never>; declare const KeepAliveLatch_base: Context.ServiceClass; /** * Service tag for the latch that coordinates entity keep-alive state. * * **Details** * * `keepAlive` closes the latch when keep-alive is active and opens it again when * the resource no longer needs to keep the entity alive. * * @stability unstable * @category services * @since 4.0.0 */ export declare class KeepAliveLatch extends KeepAliveLatch_base { } export {}; //# sourceMappingURL=Entity.d.ts.map