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

detachStackFrame.ts

22 lines887 Bytes

1import * as Effect from "effect/Effect";
2import * as References from "effect/References";
3​
4const snapshotStack = (
5 frame: References.StackFrame | undefined,
6): References.StackFrame | undefined => {
7 if (frame === undefined) return undefined;
8 const stack = frame.stack();
9 return { name: frame.name, stack: () => stack, parent: snapshotStack(frame.parent) };
10};
11​
12/**
13 * Wrap a `Cache.get` so its lookup fiber inherits a materialized stack. The lazy
14 * caller frame would otherwise retain the caller's request snapshot for the TTL.
15 * Stay untraced so this helper does not introduce another lazy frame to retain.
16 */
17export const detachStackFrame = Effect.fnUntraced(function* <A, E, R>(
18 effect: Effect.Effect<A, E, R>,
19) {
20 const frame = snapshotStack(yield* References.CurrentStackFrame);
21 return yield* Effect.provideService(effect, References.CurrentStackFrame, frame);
22});