-
-
Notifications
You must be signed in to change notification settings - Fork 10.1k
Core: Implement component to stories lookup #34972
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| import type { ChangeDetectionService } from './ChangeDetectionService.ts'; | ||
|
|
||
| let activeService: ChangeDetectionService | undefined; | ||
|
|
||
| /** | ||
| * Records the change-detection service started by the current Storybook dev server | ||
| * so that in-process consumers (addon presets) can reach it without going through a | ||
| * preset hook. Dev server is single-instance, so only one service is ever active. | ||
| * | ||
| * @internal | ||
| */ | ||
| export function setActiveChangeDetectionService(service: ChangeDetectionService | undefined): void { | ||
| activeService = service; | ||
| } | ||
|
|
||
| /** | ||
| * Returns the change-detection service for the current dev-server process, or | ||
| * `undefined` when change detection is disabled / not yet started / disposed. | ||
| * | ||
| * Read at request time, not at preset load time — the service is constructed | ||
| * after presets register. | ||
| * | ||
| * @experimental | ||
| */ | ||
| export function getActiveChangeDetectionService(): ChangeDetectionService | undefined { | ||
| return activeService; | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -9,7 +9,10 @@ import polka from 'polka'; | |
|
|
||
| import { isTelemetryModuleEnabled, telemetry } from '../telemetry/index.ts'; | ||
| import type { ChangeDetectionAdapter } from './change-detection/index.ts'; | ||
| import { ChangeDetectionService } from './change-detection/index.ts'; | ||
| import { | ||
| ChangeDetectionService, | ||
| setActiveChangeDetectionService, | ||
| } from './change-detection/index.ts'; | ||
| import { getStatusStoreByTypeId } from './stores/status.ts'; | ||
| import type { StoryIndexGenerator } from './utils/StoryIndexGenerator.ts'; | ||
| import { doTelemetry } from './utils/doTelemetry.ts'; | ||
|
|
@@ -54,6 +57,9 @@ export async function storybookDevServer( | |
| workingDir, | ||
| presets: options.presets, | ||
| }); | ||
| // Expose to in-process consumers (addon presets) via the active-service registry. | ||
| // Dev server is single-instance, so only one service is ever active. | ||
| setActiveChangeDetectionService(changeDetectionService); | ||
|
|
||
|
Comment on lines
+60
to
63
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Register the active service only when change detection is actually active. The registry is set before any 💡 Proposed fix (gate registration by startability)- // Expose to in-process consumers (addon presets) via the active-service registry.
- // Dev server is single-instance, so only one service is ever active.
- setActiveChangeDetectionService(changeDetectionService); if (!options.ignorePreview) {
if (!features.changeDetection) {
+ setActiveChangeDetectionService(undefined);
changeDetectionService.start(undefined, false);
}
@@
if (features.changeDetection) {
let adapter: ChangeDetectionAdapter | undefined;
@@
- changeDetectionService.start(adapter, true);
+ if (adapter) {
+ setActiveChangeDetectionService(changeDetectionService);
+ } else {
+ setActiveChangeDetectionService(undefined);
+ }
+ changeDetectionService.start(adapter, true);
}
}🤖 Prompt for AI Agents |
||
| app.use(compression({ level: 1 })); | ||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Avoid returning the mutable internal cache map directly.
This method exposes cached
Map<string, Set<string>>references to callers. External mutation can corrupt internal state used later by change-detection scans.💡 Proposed fix
async getStoryIdsByFile(): Promise<Map<string, Set<string>>> { const generator = await this.options.storyIndexGeneratorPromise; const storyIndex = await generator.getIndex(); - return getStoryIdsByAbsolutePath(this.storyIdsByFileCache, storyIndex, this.workingDir); + const storyIdsByFile = getStoryIdsByAbsolutePath( + this.storyIdsByFileCache, + storyIndex, + this.workingDir + ); + return new Map( + Array.from(storyIdsByFile.entries(), ([filePath, storyIds]) => [filePath, new Set(storyIds)]) + ); }🤖 Prompt for AI Agents