Skip to content
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: Normalize parameters in store/channel #10373

Merged
merged 29 commits into from
Apr 29, 2020
Merged

Conversation

tmeasday
Copy link
Member

@tmeasday tmeasday commented Apr 10, 2020

Issue: #10361

WIP:

  • Store parameters in the store normalized
    • Update add/tests
  • Emit stores over channel normalized
    • Figure out format
    • Update stories module to combine them into storiesHash
    • Update refs module to do the same, but back-compatible with old format.
  • Migration guide
    • Make sure combineParameters is available.

@github-actions
Copy link
Contributor

github-actions bot commented Apr 10, 2020

Fails
🚫 PR is marked with "BREAKING CHANGE" label.

Generated by 🚫 dangerJS against e2d54f6

@tmeasday tmeasday force-pushed the 10361-normalize-parameters branch from a415dc3 to 9b91f38 Compare April 22, 2020 13:21
@tmeasday
Copy link
Member Author

@shilman @ndelangen can you take a look? This is mostly done but there are a few things in here that may be controversial:

  1. New event instead of changing the data to SET_STORIES. It seems like this is less likely to cause subtle bugs. Plus it makes it easier to support legacy events in refs.

  2. A second implementation of combineParameters. I feel like we should have some raw "datatypes" package that we can depend on everywhere (that would include types[1]). Potentially that package is csf or something like that.

  3. Other stuff?

[1] There is heaps of inconsistent duplication of types in the code base it is a mess.

Comment on lines 801 to 810
const fullAPI = { on: jest.fn(), setStories: jest.fn(), setOptions: jest.fn() };
const navigate = jest.fn();
const store = createMockStore();

const { init } = initStories({ store, navigate, provider, fullAPI });
init();

const onSetStoryStoreData = fullAPI.on.mock.calls.find(
([event]) => event === SET_STORY_STORE_DATA
)[1];
Copy link
Member Author

@tmeasday tmeasday Apr 22, 2020

Choose a reason for hiding this comment

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

Is there a better way to do this for tests @ndelangen?

lib/client-api/src/story_store.ts Show resolved Hide resolved
@@ -401,6 +405,7 @@ export default class StoryStore {

return {
...data,
parameters: this.combineStoryParameters(data.parameters, data.kind),
Copy link
Member Author

Choose a reason for hiding this comment

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

It wasn't exactly clear which APIs we should produce denormed/combined parameters for on the story store. Right now it may be very confusing which ones we do it for.

Copy link
Member

@shilman shilman left a comment

Choose a reason for hiding this comment

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

I agree about the need for a separate package to dedupe a small set of core data structures and convenience functions

lib/client-api/src/story_store.ts Outdated Show resolved Hide resolved
lib/client-api/src/story_store.ts Show resolved Hide resolved
lib/client-api/src/story_store.ts Show resolved Hide resolved
@ndelangen
Copy link
Member

@tmeasday the set_stories event's data is currently something like { stories: {...} }.

I made it that way so we could add more data to it in the future!

I think we don't want an extra event, that would probably complicate things more? More async events, race-conditions and stuff?

@tmeasday
Copy link
Member Author

I think we don't want an extra event, that would probably complicate things more? More async events, race-conditions and stuff?

I wasn't going to add an extra event, just replace the existing SET_STORIES with SET_STORY_STORE_DATA.

I've discussed this with both you and @shilman and the conclusion we reached is:

  1. We should re-use SET_STORIES w/ a {v:2} to indicate it is a new format.

  2. We should also add {v:1} to all other core event data structures.

  3. We should throw or at least warn if we receive an event with a higher version number.

Note that 3. is very possible if someone is running a SB6.0 storybook with a ref to a SB7+ iframe. We should put this in place now! Will open a separate issue for this.

WDYT @ndelangen?

@ndelangen
Copy link
Member

I think the v number should be storybook's major version?

I like the solution @tmeasday

@tmeasday
Copy link
Member Author

tmeasday commented Apr 28, 2020

I created an issue for the version thing: #10576

let's discuss the numbering there, I will just do v:2 here for now.

@tmeasday tmeasday requested review from alterx and igor-dv as code owners April 28, 2020 11:39
@shilman
Copy link
Member

shilman commented Apr 29, 2020

Some before & after perf numbers from official-storybook page load:

Before: Denormalized

  • preview (generated-entry): 1.4s
    - pushTomanager: 1s
  • manager: 1.1s
    - telejson parse: 755ms
    - setStories: 355ms

After: Normalized

  • preview (generated-entry): 470ms
    - pushTomanager: 200ms
  • manager: 1s
    - telejson parse: 150ms
    - setStories: 900ms

Copy link
Member

@shilman shilman left a comment

Choose a reason for hiding this comment

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

I made a few fixes @tmeasday

@@ -350,7 +369,7 @@ export function useSharedState<S>(stateId: string, defaultState?: S) {
[`${SHARED_STATE_SET}-client-${stateId}`]: (s: S) => setState(s),
};
const stateInitializationHandlers = {
[SET_STORIES]: () => {
[SET_STORY_STORE_DATA]: () => {
Copy link
Member

Choose a reason for hiding this comment

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

I thought we were using SET_STORIES instead of SET_STORY_STORE_DATA? Looks like it only got partially reverted?

@shilman shilman changed the title Store parameters in the store in a normalized way Core: Store normalized parameters in the store Apr 29, 2020
@shilman shilman changed the title Core: Store normalized parameters in the store Core: Store normalized parameters Apr 29, 2020
@shilman shilman changed the title Core: Store normalized parameters Core: Make parameters normalized in store Apr 29, 2020
@shilman shilman changed the title Core: Make parameters normalized in store Core: Make parameters normalized in store/channel Apr 29, 2020
@shilman shilman changed the title Core: Make parameters normalized in store/channel Core: Normalize parameters in store/channel Apr 29, 2020
@tmeasday
Copy link
Member Author

tmeasday commented Apr 29, 2020

Thanks for taking those timings @shilman. We could definitely consider not denormalizing/combining parameters on stories in the manager until they are rendered although that could have a bunch of implications that we might not want.

We could also memoize the (globalParameters, kindParameters[kind]) part of the combination pretty easily..?

@shilman
Copy link
Member

shilman commented Apr 29, 2020

@tmeasday we can probably optimize further in a separate PR. before i merge this one, can you look at the chromatic changes? one appears to be related to the timing change and is probably OK (tho weird?!), but the other looks like it's possibly a bug, where argTypes is not getting passed through as a parameter. not obvious to me what's going on there.

@@ -333,7 +322,7 @@ export default class StoryStore {
...accumlatedParameters,
argTypes: enhancer({
...identification,
storyFn,
storyFn: original,
Copy link
Member Author

Choose a reason for hiding this comment

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

@shilman I think the original version of this was looking at the wrong storyFn?

Copy link
Member

Choose a reason for hiding this comment

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

No, the enhancer needs the original story function. Maybe we should be passing that as original instead of storyFn??

I think this "fix" actually introduces a bug. Because the enhancer can't look at whether the original story function accepts args.

Copy link
Member

Choose a reason for hiding this comment

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

OK, I misunderstood. You fixed the bug by reverting that part of the PR.

Copy link
Member Author

Choose a reason for hiding this comment

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

Ok so I just fixed a bug in my own code 👍

@shilman shilman merged commit 94c6e1d into next Apr 29, 2020
@tmeasday tmeasday deleted the 10361-normalize-parameters branch April 29, 2020 10:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants