-
-
Notifications
You must be signed in to change notification settings - Fork 9.4k
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
Core: Add context as a property of the context (self-referencing) #28353
Conversation
☁️ Nx Cloud ReportCI is running/has finished running commands for commit ccabc98. As they complete they will appear below. Click to see the status, the terminal output, and the build insights. 📂 See all runs for this CI Pipeline Execution ✅ Successfully ran 1 targetSent with 💌 from NxCloud. |
// By this stage, it is possible that new args/globals have been received for this story | ||
// and we need to ensure we render it with the new values | ||
...this.storyContext(), | ||
const context: StoryContext<TRenderer> = { |
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.
Note that loaders, beforeEach, the step function, the renderer and the play function, all get the same context reference.
@@ -15,21 +26,42 @@ vi.mock('@storybook/global', async (importOriginal) => ({ | |||
const id = 'id'; | |||
const name = 'name'; | |||
const title = 'title'; | |||
const render = (args: any) => {}; | |||
const render = () => {}; |
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.
Changed this, as there are subtle differences between prepareMeta and prepareStory when the renderer has args, which were not relevant for those tests.
f4e9d53
to
1819cb4
Compare
1819cb4
to
054f661
Compare
code/lib/preview-api/src/modules/preview-web/render/StoryRender.ts
Outdated
Show resolved
Hide resolved
const loadResults = await Promise.all(loaders.map((loader) => loader(updatedContext))); | ||
const loaded: Record<string, any> = Object.assign({}, ...loadResults); | ||
updatedContext = { ...updatedContext, loaded: { ...updatedContext.loaded, ...loaded } }; | ||
if (context.abortSignal.aborted) return loaded; |
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.
aborting as early as possible inside of applyloaders
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.
How is this scenario possible? Is there an await
in between the StoryRendering
checking the abort signal and this line?
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.
Yes the line below this line:
const loadResults = await Promise.all(loaders.map((loader) => loader(context)));
And notice that this is a loop, calling the project loaders first, and then the component and then the story loaders.
Whenever the user navigates to a new story, or force remounts, the StoryRender file will call:
this.abortController?.abort();
Now, the loaders can be cancelled half way, before it would only cancel after all loader are loaded.
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.
Ahh OK, got it. Maybe a comment would be useful here (something like: "If an abort is received in between running each level of loaders, we want to avoid running the next level")
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.
Okay, will add that to the mount PR!
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.
Very well done! I am not sure though, which kind of implications a self-referencing context object might have. The instrumenter is covered, which is the most obvious one. I am curious, whether there are cases where user's are serializing the context object. This would be a breaking change in this case.
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.
I like this change!
code/lib/preview-api/src/modules/preview-web/render/StoryRender.ts
Outdated
Show resolved
Hide resolved
code/lib/preview-api/src/modules/preview-web/render/StoryRender.ts
Outdated
Show resolved
Hide resolved
// TODO(kasperpeulen) Consolidate this function with composeConfigs | ||
// As composeConfigs is the real normalizer, and always run before normalizeProjectAnnotations |
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.
I discussed this with someone recently (@shilman I think?). Alternatively we could get rid of composeConfigs
and just pass ProjectAnnotations[]
around -- and do the composing here.
That makes sense to me as it avoids the need for both WP + Vite to call composeConfigs
at the right time.
It definitely doesn't makes sense to have both composeConfigs
and normalizeProjectAnnotations
.
Also I don't think these two lines belong in there:
storybook/code/lib/preview-api/src/modules/store/csf/normalizeProjectAnnotations.ts
Lines 43 to 48 in 8bb1298
inferArgTypes, | |
// inferControls technically should only run if the user is using the controls addon, | |
// and so should be added by a preset there. However, as it seems some code relies on controls | |
// annotations (in particular the angular implementation's `cleanArgsDecorator`), for backwards | |
// compatibility reasons, we will leave this in the store until 7.0 | |
inferControls, |
Instead, they should get added to the ProjectAnnotations[]
somewhere.
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.
Right, I like that. It seems better to do as little as possible JS in the webpack/vite template files.
Those 2 lines, should move to the addon-controls preview file right?
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.
Those 2 lines, should move to the addon-controls preview file right?
Possibly, let's discuss with @shilman to make sure.
const loadResults = await Promise.all(loaders.map((loader) => loader(updatedContext))); | ||
const loaded: Record<string, any> = Object.assign({}, ...loadResults); | ||
updatedContext = { ...updatedContext, loaded: { ...updatedContext.loaded, ...loaded } }; | ||
if (context.abortSignal.aborted) return loaded; |
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.
How is this scenario possible? Is there an await
in between the StoryRendering
checking the abort signal and this line?
0de55aa
to
6a91865
Compare
What I did
This PR uses the CSF changes here, they are best reviewed together:
https://github.com/ComponentDriven/csf/pull/98/files
This PR adds a self referencing context property to the story context:
storybook/code/lib/test/template/stories/before-each.stories.ts
Lines 60 to 65 in 054f661
This is useful for a lot of cases, but will be necessary when we implement the mount context field in a subsequent PR for 8.2 that always need to be destructured (see #27389):
storybook/docs/_snippets/my-component-play-function-composition.md
Lines 35 to 44 in b765c98
I consolidated the loader, before each, render and play contexts, so that it is the same for every phase. For example canvasElement and step are now always available:
storybook/code/lib/test/template/stories/before-each.stories.ts
Lines 67 to 79 in fd11b1a
The context is now always the same reference, which also allows people to mutate the context and use the mutation in a different phase. Similar as in vitest:
https://vitest.dev/guide/test-context#beforeeach-and-aftereach
![image](https://private-user-images.githubusercontent.com/1035299/343131208-e39448af-7585-4ae8-a3be-85151d4e662e.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3MzkyMTc3NzMsIm5iZiI6MTczOTIxNzQ3MywicGF0aCI6Ii8xMDM1Mjk5LzM0MzEzMTIwOC1lMzk0NDhhZi03NTg1LTRhZTgtYTNiZS04NTE1MWQ0ZTY2MmUucG5nP1gtQW16LUFsZ29yaXRobT1BV1M0LUhNQUMtU0hBMjU2JlgtQW16LUNyZWRlbnRpYWw9QUtJQVZDT0RZTFNBNTNQUUs0WkElMkYyMDI1MDIxMCUyRnVzLWVhc3QtMSUyRnMzJTJGYXdzNF9yZXF1ZXN0JlgtQW16LURhdGU9MjAyNTAyMTBUMTk1NzUzWiZYLUFtei1FeHBpcmVzPTMwMCZYLUFtei1TaWduYXR1cmU9ZGJiNjA5MWMwMGY4NTM0NDY0NDNiY2I4NmVjMWM2MzE4MWEzNTFmNWViYzI1MGFhM2U3MjViMmUxY2FiNmVmZiZYLUFtei1TaWduZWRIZWFkZXJzPWhvc3QifQ.M8zG9BxC-PeBETZ-OKdPScWmHu6G07z55R79txcI1fI)
We will need this behavior as well for the mount implementation. We will add a
canvas
context property in a@storybook/test
loader.contex.canvas
which will be equal towithin(canvasElement)
and will the default return type of the mount property (await mount() === context.canvas
)Checklist for Contributors
Testing
The changes in this PR are covered in the following automated tests:
Manual testing
This section is mandatory for all contributions. If you believe no manual test is necessary, please state so explicitly. Thanks!
Documentation
MIGRATION.MD
Checklist for Maintainers
When this PR is ready for testing, make sure to add
ci:normal
,ci:merged
orci:daily
GH label to it to run a specific set of sandboxes. The particular set of sandboxes can be found incode/lib/cli/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 canary-release-pr.yml --field pr=<PR_NUMBER>