import * as Context from "effect/Context"; import * as Deferred from "effect/Deferred"; import * as Effect from "effect/Effect"; import type * as Fiber from "effect/Fiber"; import type * as Scope from "effect/Scope"; export class ServerActivation extends Context.Reference | undefined>( "t3/serverActivation", { defaultValue: () => undefined }, ) {} /** Forks a long-running root before commit, returning it after it reaches the activation boundary. */ export const forkParkedFiber = ( effect: Effect.Effect, ): Effect.Effect, never, Scope.Scope | R> => Effect.gen(function* () { const activation = yield* ServerActivation; if (activation === undefined) { return yield* Effect.forkScoped(effect); } const parked = yield* Deferred.make(); const fiber = yield* Effect.forkScoped( Deferred.succeed(parked, undefined).pipe(Effect.andThen(activation), Effect.andThen(effect)), ); yield* Deferred.await(parked); return fiber; }); /** Forks a long-running root before commit and proves it is parked at the activation boundary. */ export const forkParked = ( effect: Effect.Effect, ): Effect.Effect => forkParkedFiber(effect).pipe(Effect.asVoid);