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