-
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathprefetch.ts
More file actions
64 lines (58 loc) · 2.22 KB
/
prefetch.ts
File metadata and controls
64 lines (58 loc) · 2.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
/**
* Background Org Detection Prefetch
*
* Provides a warm/consume pattern for org resolution during `sentry init`.
* Call {@link warmOrgDetection} early (before the preamble) to start DSN
* scanning in the background. Later, call {@link resolveOrgPrefetched} —
* it returns the cached result instantly if the background work has
* finished, or falls back to a live call if it hasn't been warmed.
*
* `listOrganizations()` does NOT need prefetching because it has its own
* SQLite cache layer (PR #446). After `sentry login`, the org cache is
* pre-populated (PR #490), so subsequent calls return from cache instantly
* without any HTTP requests. Only `resolveOrg()` (DSN scanning) benefits
* from background prefetching since it performs filesystem I/O.
*
* This keeps the hot path (inside the wizard's `createSentryProject`)
* free of explicit promise-threading — callers just swap in the
* prefetch-aware functions.
*/
import type { ResolvedOrg } from "../resolve-target.js";
import { resolveOrg } from "../resolve-target.js";
type OrgResult = ResolvedOrg | null;
let orgPromise: Promise<OrgResult> | undefined;
let warmedCwd: string | undefined;
/**
* Kick off background DSN scanning + env var / config checks.
*
* Safe to call multiple times — subsequent calls are no-ops.
* Errors are silently swallowed so the foreground path can retry.
*/
export function warmOrgDetection(cwd: string): void {
if (!orgPromise) {
warmedCwd = cwd;
orgPromise = resolveOrg({ cwd }).catch(() => null);
}
}
/**
* Resolve the org, using the prefetched result if available.
*
* Returns the cached background result only when `cwd` matches the
* directory that was passed to {@link warmOrgDetection}. If `cwd`
* differs (or warming was never called), falls back to a live
* `resolveOrg()` call so callers always get results for the correct
* working directory.
*/
export function resolveOrgPrefetched(cwd: string): Promise<OrgResult> {
if (orgPromise && cwd === warmedCwd) {
return orgPromise;
}
return resolveOrg({ cwd }).catch(() => null);
}
/**
* Reset prefetch state. Used by tests to prevent cross-test leakage.
*/
export function resetPrefetch(): void {
orgPromise = undefined;
warmedCwd = undefined;
}