From 92f49c7afeceff2b23ce1897cd276a55ff9bcff1 Mon Sep 17 00:00:00 2001 From: Giulio Canti Date: Sun, 17 Aug 2025 18:00:56 +0200 Subject: [PATCH] change fiberId type from Option to number | undefined (#412) --- packages/effect/src/Cause.ts | 6 ++---- packages/effect/src/internal/effect.ts | 15 +++++++-------- packages/effect/src/schema/Schema.ts | 13 ++----------- .../effect/src/unstable/http/HttpServerError.ts | 2 +- 4 files changed, 12 insertions(+), 24 deletions(-) diff --git a/packages/effect/src/Cause.ts b/packages/effect/src/Cause.ts index da832d2e24..b63f61e238 100644 --- a/packages/effect/src/Cause.ts +++ b/packages/effect/src/Cause.ts @@ -40,7 +40,6 @@ * @since 2.0.0 */ import type * as Filter from "./data/Filter.ts" -import type { Option } from "./data/Option.ts" import type * as Effect from "./Effect.ts" import type { Equal } from "./interfaces/Equal.ts" import type { Inspectable } from "./interfaces/Inspectable.ts" @@ -338,13 +337,12 @@ export interface Fail extends Cause.FailureProto<"Fail"> { * @example * ```ts * import { Cause } from "effect" -import * as Option from "effect/data/Option" * * const cause = Cause.interrupt(123) * const failure = cause.failures[0] * if (Cause.failureIsInterrupt(failure)) { * console.log(failure._tag) // "Interrupt" - * console.log(Option.isSome(failure.fiberId)) // true + * console.log(failure.fiberId !== undefined) // true * } * ``` * @@ -352,7 +350,7 @@ import * as Option from "effect/data/Option" * @category models */ export interface Interrupt extends Cause.FailureProto<"Interrupt"> { - readonly fiberId: Option // TODO: why not `number | undefined`? + readonly fiberId: number | undefined } /** diff --git a/packages/effect/src/internal/effect.ts b/packages/effect/src/internal/effect.ts index bcc4f597f2..bb964d85c3 100644 --- a/packages/effect/src/internal/effect.ts +++ b/packages/effect/src/internal/effect.ts @@ -79,9 +79,9 @@ import { version } from "./version.ts" /** @internal */ export class Interrupt extends FailureBase<"Interrupt"> implements Cause.Interrupt { - readonly fiberId: Option.Option + readonly fiberId: number | undefined constructor( - fiberId: Option.Option, + fiberId: number | undefined, annotations = new Map() ) { super("Interrupt", annotations, "Interrupted") @@ -120,13 +120,12 @@ export class Interrupt extends FailureBase<"Interrupt"> implements Cause.Interru } /** @internal */ -export const failureInterrupt = (fiberId?: number | undefined): Cause.Interrupt => - new Interrupt(Option.fromNullable(fiberId)) +export const failureInterrupt = (fiberId?: number | undefined): Cause.Interrupt => new Interrupt(fiberId) /** @internal */ export const causeInterrupt = ( fiberId?: number | undefined -): Cause.Cause => new CauseImpl([new Interrupt(Option.fromNullable(fiberId))]) +): Cause.Cause => new CauseImpl([new Interrupt(fiberId)]) /** @internal */ export const causeHasFail = (self: Cause.Cause): boolean => self.failures.some(failureIsFail) @@ -174,8 +173,8 @@ export const causeFilterInterruptors = (self: Cause.Cause): Set | const f = self.failures[i] if (f._tag !== "Interrupt") continue interruptors ??= new Set() - if (f.fiberId._tag === "Some") { - interruptors.add(f.fiberId.value) + if (f.fiberId !== undefined) { + interruptors.add(f.fiberId) } } return interruptors ? interruptors : Filter.fail(self) @@ -370,7 +369,7 @@ const cleanErrorStack = ( const interruptCauseStack = (error: Error, interrupts: Array): string => { const out: Array = [`${error.name}: ${error.message}`] for (const current of interrupts) { - const fiberId = current.fiberId._tag === "Some" ? `#${current.fiberId.value}` : "unknown" + const fiberId = current.fiberId !== undefined ? `#${current.fiberId}` : "unknown" const span = current.annotations.get(InterruptorSpanKey.key) as Tracer.Span | undefined out.push(` at fiber (${fiberId})`) if (span) pushSpanStack(out, span) diff --git a/packages/effect/src/schema/Schema.ts b/packages/effect/src/schema/Schema.ts index d055d2e2fd..51e0dca7e7 100644 --- a/packages/effect/src/schema/Schema.ts +++ b/packages/effect/src/schema/Schema.ts @@ -3365,7 +3365,7 @@ export function CauseFailure(error: E, defect: D): Union([ TaggedStruct("Fail", { error }), TaggedStruct("Die", { defect }), - TaggedStruct("Interrupt", { fiberId: UndefinedOr(Finite) }) // TODO: UndefinedOr(Int)? + TaggedStruct("Interrupt", { fiberId: UndefinedOr(Finite) }) ]), Transformation.transform({ decode: (input) => { @@ -3378,16 +3378,7 @@ export function CauseFailure(error: E, defect: D): return Cause_.failureInterrupt(input.fiberId) } }, - encode: (failure) => { - switch (failure._tag) { - case "Fail": - return { _tag: "Fail", error: failure.error } as const - case "Die": - return { _tag: "Die", defect: failure.defect } as const - case "Interrupt": - return { _tag: "Interrupt", fiberId: O.getOrUndefined(failure.fiberId) } as const - } - } + encode: identity }) ), arbitrary: { diff --git a/packages/effect/src/unstable/http/HttpServerError.ts b/packages/effect/src/unstable/http/HttpServerError.ts index 443a72273e..6bfa2cc2f4 100644 --- a/packages/effect/src/unstable/http/HttpServerError.ts +++ b/packages/effect/src/unstable/http/HttpServerError.ts @@ -151,7 +151,7 @@ export const causeResponse = ( } case "Interrupt": { if (acc[1].length > 0) return acc - const response = f.fiberId._tag === "Some" && f.fiberId.value === clientAbortFiberId + const response = f.fiberId === clientAbortFiberId ? clientAbortError : serverAbortError return [Effect.succeed(response), [f]]