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

serverActivation.ts

30 lines1.1 KB

1import * as Context from "effect/Context";
2import * as Deferred from "effect/Deferred";
3import * as Effect from "effect/Effect";
4import type * as Fiber from "effect/Fiber";
5import type * as Scope from "effect/Scope";
6​
7export class ServerActivation extends Context.Reference<Effect.Effect<void> | undefined>(
8 "t3/serverActivation",
9 { defaultValue: () => undefined },
10) {}
11​
12export const forkParkedFiber = <A, E, R>(
13 effect: Effect.Effect<A, E, R>,
14): Effect.Effect<Fiber.Fiber<A, E>, never, Scope.Scope | R> =>
15 Effect.gen(function* () {
16 const activation = yield* ServerActivation;
17 if (activation === undefined) {
18 return yield* Effect.forkScoped(effect);
19 }
20 const parked = yield* Deferred.make<void>();
21 const fiber = yield* Effect.forkScoped(
22 Deferred.succeed(parked, undefined).pipe(Effect.andThen(activation), Effect.andThen(effect)),
23 );
24 yield* Deferred.await(parked);
25 return fiber;
26 });
27​
28export const forkParked = <A, E, R>(
29 effect: Effect.Effect<A, E, R>,
30): Effect.Effect<void, never, Scope.Scope | R> => forkParkedFiber(effect).pipe(Effect.asVoid);