Skip to content

Commit cb52098

Browse files
committed
refactor: drop listOrgsPrefetched — listOrganizations() is SQLite-cached
Since PR #446 (listOrganizations SQLite caching) and PR #490 (post-login cache warming), listOrganizations() returns from cache instantly on warm starts. The separate listOrgsPrefetched() wrapper no longer adds value. Keep only the resolveOrg prefetch (DSN scanning, 2-5s on cold starts), which is the actual latency win of this PR.
1 parent cc9b33f commit cb52098

File tree

2 files changed

+26
-31
lines changed

2 files changed

+26
-31
lines changed

src/lib/init/local-ops.ts

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,12 @@ import { spawn } from "node:child_process";
99
import fs from "node:fs";
1010
import path from "node:path";
1111
import { isCancel, select } from "@clack/prompts";
12-
import { createProject, getProject, tryGetPrimaryDsn } from "../api-client.js";
12+
import {
13+
createProject,
14+
getProject,
15+
listOrganizations,
16+
tryGetPrimaryDsn,
17+
} from "../api-client.js";
1318
import { ApiError } from "../errors.js";
1419
import { resolveOrCreateTeam } from "../resolve-team.js";
1520
import { buildProjectUrl } from "../sentry-urls.js";
@@ -19,7 +24,7 @@ import {
1924
MAX_FILE_BYTES,
2025
MAX_OUTPUT_BYTES,
2126
} from "./constants.js";
22-
import { listOrgsPrefetched, resolveOrgPrefetched } from "./prefetch.js";
27+
import { resolveOrgPrefetched } from "./prefetch.js";
2328
import type {
2429
ApplyPatchsetPayload,
2530
CreateSentryProjectPayload,
@@ -654,10 +659,13 @@ function applyPatchset(
654659
* Resolve the org slug from local config, env vars, or by listing the user's
655660
* organizations from the API as a fallback.
656661
*
657-
* Uses the prefetch-aware helpers from `./prefetch.ts` — if
658-
* {@link warmOrgDetection} was called earlier (by `init.ts`), the results are
659-
* already cached and this function returns near-instantly. Otherwise it falls
660-
* back to live calls transparently.
662+
* DSN scanning uses the prefetch-aware helper from `./prefetch.ts` — if
663+
* {@link warmOrgDetection} was called earlier (by `init.ts`), the result is
664+
* already cached and returns near-instantly.
665+
*
666+
* `listOrganizations()` uses SQLite caching for near-instant warm lookups
667+
* (populated after `sentry login` or the first API call), so it does not
668+
* need background prefetching.
661669
*
662670
* @returns The org slug on success, or a {@link LocalOpResult} error to return early.
663671
*/
@@ -670,8 +678,8 @@ async function resolveOrgSlug(
670678
return resolved.org;
671679
}
672680

673-
// Fallback: list user's organizations from API (prefetch-aware)
674-
const orgs = await listOrgsPrefetched();
681+
// Fallback: list user's organizations (SQLite-cached after login/first call)
682+
const orgs = await listOrganizations();
675683
if (orgs.length === 0) {
676684
return {
677685
ok: false,

src/lib/init/prefetch.ts

Lines changed: 10 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -3,28 +3,30 @@
33
*
44
* Provides a warm/consume pattern for org resolution during `sentry init`.
55
* Call {@link warmOrgDetection} early (before the preamble) to start DSN
6-
* scanning and org-list fetching in the background. Later, call
7-
* {@link resolveOrgPrefetched} or {@link listOrgsPrefetched} — they return
8-
* the cached result instantly if the background work has finished, or
9-
* fall back to a live call if it hasn't been warmed.
6+
* scanning in the background. Later, call {@link resolveOrgPrefetched} —
7+
* it returns the cached result instantly if the background work has
8+
* finished, or falls back to a live call if it hasn't been warmed.
9+
*
10+
* `listOrganizations()` does NOT need prefetching because it has its own
11+
* SQLite cache layer (PR #446). After `sentry login`, the org cache is
12+
* pre-populated (PR #490), so subsequent calls return from cache instantly
13+
* without any HTTP requests. Only `resolveOrg()` (DSN scanning) benefits
14+
* from background prefetching since it performs filesystem I/O.
1015
*
1116
* This keeps the hot path (inside the wizard's `createSentryProject`)
1217
* free of explicit promise-threading — callers just swap in the
1318
* prefetch-aware functions.
1419
*/
1520

16-
import { listOrganizations } from "../api-client.js";
1721
import type { ResolvedOrg } from "../resolve-target.js";
1822
import { resolveOrg } from "../resolve-target.js";
1923

2024
type OrgResult = ResolvedOrg | null;
21-
type OrgListResult = Array<{ id: string; slug: string; name: string }>;
2225

2326
let orgPromise: Promise<OrgResult> | undefined;
24-
let orgListPromise: Promise<OrgListResult> | undefined;
2527

2628
/**
27-
* Kick off background org detection and org-list fetching.
29+
* Kick off background DSN scanning + env var / config checks.
2830
*
2931
* Safe to call multiple times — subsequent calls are no-ops.
3032
* Errors are silently swallowed so the foreground path can retry.
@@ -33,9 +35,6 @@ export function warmOrgDetection(cwd: string): void {
3335
if (!orgPromise) {
3436
orgPromise = resolveOrg({ cwd }).catch(() => null);
3537
}
36-
if (!orgListPromise) {
37-
orgListPromise = listOrganizations().catch(() => []);
38-
}
3938
}
4039

4140
/**
@@ -49,21 +48,9 @@ export function resolveOrgPrefetched(cwd: string): Promise<OrgResult> {
4948
return resolveOrg({ cwd }).catch(() => null);
5049
}
5150

52-
/**
53-
* List organizations, using the prefetched result if available.
54-
* Falls back to a live call when {@link warmOrgDetection} was not called.
55-
*/
56-
export function listOrgsPrefetched(): Promise<OrgListResult> {
57-
if (orgListPromise) {
58-
return orgListPromise;
59-
}
60-
return listOrganizations().catch(() => []);
61-
}
62-
6351
/**
6452
* Reset prefetch state. Used by tests to prevent cross-test leakage.
6553
*/
6654
export function resetPrefetch(): void {
6755
orgPromise = undefined;
68-
orgListPromise = undefined;
6956
}

0 commit comments

Comments
 (0)