Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

serverActivation.ts

26 lines951 Bytes

1import * as Context from "effect/Context";
2import * as Deferred from "effect/Deferred";
3import * as Effect from "effect/Effect";
4import type * as Scope from "effect/Scope";
5​
6export class ServerActivation extends Context.Reference<Effect.Effect<void> | undefined>(
7 "t3/serverActivation",
8 { defaultValue: () => undefined },
9) {}
10​
11/** Forks a long-running root before commit and proves it is parked at the activation boundary. */
12export const forkParked = <A, E, R>(
13 effect: Effect.Effect<A, E, R>,
14): Effect.Effect<void, never, Scope.Scope | R> =>
15 Effect.gen(function* () {
16 const activation = yield* ServerActivation;
17 if (activation === undefined) {
18 yield* Effect.forkScoped(effect);
19 return;
20 }
21 const parked = yield* Deferred.make<void>();
22 yield* Effect.forkScoped(
23 Deferred.succeed(parked, undefined).pipe(Effect.andThen(activation), Effect.andThen(effect)),
24 );
25 yield* Deferred.await(parked);
26 });