Skip to content
2 min read · 409 words

Interface: TurnGate<T>

A cooperative suspension gate that blocks a turn's middleware pipeline until resolved, rejected, aborted, or timed out.

Remarks

Created exclusively via ctx.waitFor() — middleware never constructs a gate directly. The gate emits turnGateOpen on the runner's observability bus at creation time and turnGateClosed when it settles.

Resolution is validated against an optional schema before the internal promise is settled. A validation failure throws @nhtio/adk!E_INVALID_TURN_GATE_RESOLUTION synchronously in the caller's context — the promise is NOT settled and the gate remains open.

Type Parameters

Type ParameterDefault typeDescription
TunknownThe expected type of the resolution value.

Properties

PropertyModifierType
createdAtreadonlyDateTime
idreadonlystring
isSettledreadonlyboolean
payloadreadonlyunknown
reasonreadonlystring
turnIdreadonlystring

Methods

_promise()

ts
_promise(): Promise<T>;

Internal

Returns the internal promise. Called by ctx.waitFor() to block the middleware pipeline.

Returns

Promise<T>


abort()

ts
abort(): void;

Aborts the gate by firing the internal AbortController, which rejects the promise with @nhtio/adk!E_TURN_GATE_ABORTED.

Returns

void

Remarks

No-ops if the gate is already settled. Distinct from the turn-level abort signal — this allows callers to cancel a specific gate without aborting the whole turn.


reject()

ts
reject(error: Error): void;

Rejects the gate with error, unblocking the awaiting middleware with a rejection.

Parameters

ParameterTypeDescription
errorErrorThe rejection reason.

Returns

void

Remarks

No-ops if the gate is already settled.


resolve()

ts
resolve(value: unknown): void;

Resolves the gate with value, unblocking the awaiting middleware.

Parameters

ParameterTypeDescription
valueunknownThe resolution value. Must satisfy the gate's schema when one was provided.

Returns

void

Remarks

If a schema was provided at construction, value is validated synchronously before the promise is settled. A validation failure throws @nhtio/adk!E_INVALID_TURN_GATE_RESOLUTION in the caller's context — the promise is NOT settled and the gate remains open.

No-ops if the gate is already settled.

Throws

@nhtio/adk!E_INVALID_TURN_GATE_RESOLUTION when value fails schema validation.