Skip to content

Commit c8dd6d1

Browse files
committed
refactor: tighten boot mapping accessor types
1 parent a0b8c88 commit c8dd6d1

1 file changed

Lines changed: 25 additions & 12 deletions

File tree

src/config/sessions/session-accessor.ts

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -559,10 +559,26 @@ type TemporarySessionMappingSnapshot =
559559
}
560560
| {
561561
canRestore: true;
562-
entry?: SessionEntry;
563-
hadEntry: boolean;
562+
hadEntry: false;
564563
sessionKey: string;
565564
storePath: string;
565+
}
566+
| {
567+
canRestore: true;
568+
entry: SessionEntry;
569+
hadEntry: true;
570+
sessionKey: string;
571+
storePath: string;
572+
};
573+
574+
type TemporarySessionMappingOperationResult<T> =
575+
| {
576+
ok: true;
577+
result: T;
578+
}
579+
| {
580+
error: unknown;
581+
ok: false;
566582
};
567583

568584
export type SessionEntryCreateWithTranscriptContext = {
@@ -1429,23 +1445,20 @@ export async function preserveTemporarySessionMapping<T>(
14291445
operation: () => Promise<T> | T,
14301446
): Promise<TemporarySessionMappingPreservationResult<T>> {
14311447
const snapshot = snapshotTemporarySessionMapping(scope);
1432-
let operationResult: T | undefined;
1433-
let operationError: unknown;
1434-
let operationThrew = false;
1448+
let operationResult: TemporarySessionMappingOperationResult<T>;
14351449
try {
1436-
operationResult = await operation();
1450+
operationResult = { ok: true, result: await operation() };
14371451
} catch (err) {
1438-
operationError = err;
1439-
operationThrew = true;
1452+
operationResult = { error: err, ok: false };
14401453
}
14411454

14421455
const restoreFailure = await restoreTemporarySessionMapping(snapshot);
1443-
if (operationThrew) {
1444-
throw operationError;
1456+
if (!operationResult.ok) {
1457+
throw operationResult.error;
14451458
}
14461459

14471460
return {
1448-
result: operationResult as T,
1461+
result: operationResult.result,
14491462
...(snapshot.canRestore ? {} : { snapshotFailure: snapshot.snapshotFailure }),
14501463
...(restoreFailure ? { restoreFailure } : {}),
14511464
};
@@ -2606,7 +2619,7 @@ async function restoreTemporarySessionMapping(
26062619
await updateSessionStore(
26072620
snapshot.storePath,
26082621
(store) => {
2609-
if (snapshot.hadEntry && snapshot.entry) {
2622+
if (snapshot.hadEntry) {
26102623
store[snapshot.sessionKey] = structuredClone(snapshot.entry);
26112624
return;
26122625
}

0 commit comments

Comments
 (0)