Skip to content

Manager-API: Update refs sequentially in experimental_setFilter#33958

Merged
valentinpalkovic merged 1 commit into
storybookjs:nextfrom
ia319:bug/33800-update-refs-sequentially
Mar 2, 2026
Merged

Manager-API: Update refs sequentially in experimental_setFilter#33958
valentinpalkovic merged 1 commit into
storybookjs:nextfrom
ia319:bug/33800-update-refs-sequentially

Conversation

@ia319
Copy link
Copy Markdown
Member

@ia319 ia319 commented Feb 28, 2026

Closes #33800

What I did

Change the concurrent forEach updates over refs in experimental_setFilter to a serial await loop to avoid later writes overwriting earlier ones

Checklist for Contributors

Testing

The changes in this PR are covered in the following automated tests:

  • stories
  • unit tests
  • integration tests
  • end-to-end tests

Manual testing

  1. In code/.storybook/main.ts, copy three icons entries into refs (pick ones that have docs so they’re easy to filter)
  2. In code, run yarn storybook:ui
  3. Click the filter and select docs
  4. Observe the four icons groups — they should all contain docs only

Documentation

  • Add or update documentation reflecting your changes
  • If you are deprecating/removing a feature, make sure to update
    MIGRATION.MD

Checklist for Maintainers

  • When this PR is ready for testing, make sure to add ci:normal, ci:merged or ci:daily GH label to it to run a specific set of sandboxes. The particular set of sandboxes can be found in code/lib/cli-storybook/src/sandbox-templates.ts

  • Make sure this PR contains one of the labels below:

    Available labels
    • bug: Internal changes that fixes incorrect behavior.
    • maintenance: User-facing maintenance tasks.
    • dependencies: Upgrading (sometimes downgrading) dependencies.
    • build: Internal-facing build tooling & test updates. Will not show up in release changelog.
    • cleanup: Minor cleanup style change. Will not show up in release changelog.
    • documentation: Documentation only changes. Will not show up in release changelog.
    • feature request: Introducing a new feature.
    • BREAKING CHANGE: Changes that break compatibility in some way with current major version.
    • other: Changes that don't fit in the above categories.

🦋 Canary release

This PR does not have a canary release associated. You can request a canary release of this pull request by mentioning the @storybookjs/core team here.

core team members can create a canary release here or locally with gh workflow run --repo storybookjs/storybook publish.yml --field pr=<PR_NUMBER>

Summary by CodeRabbit

  • Bug Fixes
    • Improved the reliability and consistency of filter operations by ensuring reference updates are properly sequenced and fully completed before proceeding.

@ia319 ia319 changed the title Manager-api: Update refs sequentially in experimental_setFilter Manager api: Update refs sequentially in experimental_setFilter Feb 28, 2026
@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented Feb 28, 2026

📝 Walkthrough

Walkthrough

Modified the experimental_setFilter function to iterate through refs sequentially using a for...of loop with awaited fullAPI.setRef calls, replacing a parallel forEach pattern. This ensures ref updates complete before emitting the SET_FILTER event.

Changes

Cohort / File(s) Summary
Async iteration refactoring
code/core/src/manager-api/modules/stories.ts
Changed refs iteration from Object.entries().forEach with non-awaited calls to for...of loop with awaited fullAPI.setRef invocations, making updates sequential and blocking before filter emission.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes


Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🧹 Nitpick comments (1)
code/core/src/manager-api/modules/stories.ts (1)

708-710: Apply the same sequential ref-update strategy in the status-change reapply path.

This loop is correctly serialized, but the status-change path still does concurrent forEach ref updates (Line 933-Line 935). That can reintroduce last-write-wins behavior when filters are reapplied after status changes. Consider extracting a shared awaited helper and reusing it in both places.

Proposed refactor
+  const updateRefsWithCurrentFilters = async () => {
+    const refs = await fullAPI.getRefs();
+    for (const [refId, { internal_index, ...ref }] of Object.entries(refs)) {
+      await fullAPI.setRef(refId, { ...ref, storyIndex: internal_index }, true);
+    }
+  };
...
-      const refs = await fullAPI.getRefs();
-      for (const [refId, { internal_index, ...ref }] of Object.entries(refs)) {
-        await fullAPI.setRef(refId, { ...ref, storyIndex: internal_index }, true);
-      }
+      await updateRefsWithCurrentFilters();
...
-    const refs = await fullAPI.getRefs();
-    Object.entries(refs).forEach(([refId, { internal_index, ...ref }]) => {
-      fullAPI.setRef(refId, { ...ref, storyIndex: internal_index }, true);
-    });
+    await updateRefsWithCurrentFilters();
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@code/core/src/manager-api/modules/stories.ts` around lines 708 - 710, The
status-change reapply path currently updates refs concurrently (forEach at the
reapply block) which can cause last-write-wins; extract a small awaited helper
(e.g., setRefsSequentially or applyRefsInOrder) that takes the refs
object/entries and calls fullAPI.setRef(refId, { ...ref, storyIndex:
internal_index }, true) in a for..of loop (as used in the serialized loop that
iterates Object.entries(refs)), then replace the concurrent forEach in the
status-change reapply path with a call to that helper so both code paths update
refs sequentially and await each setRef.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Nitpick comments:
In `@code/core/src/manager-api/modules/stories.ts`:
- Around line 708-710: The status-change reapply path currently updates refs
concurrently (forEach at the reapply block) which can cause last-write-wins;
extract a small awaited helper (e.g., setRefsSequentially or applyRefsInOrder)
that takes the refs object/entries and calls fullAPI.setRef(refId, { ...ref,
storyIndex: internal_index }, true) in a for..of loop (as used in the serialized
loop that iterates Object.entries(refs)), then replace the concurrent forEach in
the status-change reapply path with a call to that helper so both code paths
update refs sequentially and await each setRef.

ℹ️ Review info

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 2b8e827 and db26045.

📒 Files selected for processing (1)
  • code/core/src/manager-api/modules/stories.ts

@valentinpalkovic valentinpalkovic changed the title Manager api: Update refs sequentially in experimental_setFilter Manager-API: Update refs sequentially in experimental_setFilter Mar 2, 2026
@valentinpalkovic valentinpalkovic moved this to In Progress in Core Team Projects Mar 2, 2026
@valentinpalkovic valentinpalkovic self-assigned this Mar 2, 2026
@valentinpalkovic valentinpalkovic merged commit c4d647b into storybookjs:next Mar 2, 2026
130 of 135 checks passed
@github-project-automation github-project-automation Bot moved this from In Progress to Done in Core Team Projects Mar 2, 2026
@valentinpalkovic valentinpalkovic added the patch:yes Bugfix & documentation PR that need to be picked to main branch label Mar 2, 2026
yannbf pushed a commit that referenced this pull request Mar 4, 2026
Manager-API: Update refs sequentially in experimental_setFilter
(cherry picked from commit c4d647b)
@github-actions github-actions Bot added the patch:done Patch/release PRs already cherry-picked to main/release branch label Mar 4, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug ci:normal patch:done Patch/release PRs already cherry-picked to main/release branch patch:yes Bugfix & documentation PR that need to be picked to main branch

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

[Bug]: Tag filters apply to current storybook and only to the last external storybook in refs composition

2 participants