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

addon-viewport: Use the new parameterized way of decorators #3610

Merged
merged 4 commits into from
May 20, 2018

Conversation

mshaaban088
Copy link
Contributor

What I did

I used the new parameterized way of decorators described in #3559

How to test

yarn test

Is this testable with Jest or Chromatic screenshots?
No

Does this need a new example in the kitchen sink apps?
No, I just updated the already existing one in official-storybook examples

Does this need an update to the documentation?
Yes and added

If your answer is yes to any of these, please make sure to include it in your PR.

For maintainers only: Please tag your pull request with at least one of the following:
["cleanup", "BREAKING CHANGE", "feature request", "bug", "documentation", "maintenance", "dependencies", "other"]


const subscription = () => {
const subscription = handler => () => {
Copy link
Contributor Author

@mshaaban088 mshaaban088 May 20, 2018

Choose a reason for hiding this comment

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

I noticed an issue in the previous implementation when you create 2 stories with subscription/onViewportChange, the 2nd one receives the event twice every time you change the viewport, which should not be

Copy link
Member

@Hypnosphi Hypnosphi May 20, 2018

Choose a reason for hiding this comment

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

Now it creates a new subscription on each rerender (e.g. knob change): subscription(handler) is a new function each time

Copy link
Member

@Hypnosphi Hypnosphi May 20, 2018

Choose a reason for hiding this comment

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

Can you please add a second example with onViewportChange so that everyoune could reproduce the issue. Just by looking at the change I can't figure out why it happened

Copy link
Member

@Hypnosphi Hypnosphi May 20, 2018

Choose a reason for hiding this comment

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

Oh I see, changing of handler variable prevents the subscription from removing. This should fix it without affecting performance:

const callHandler = (...args) => handler(...args)

const subscription = () => {
  const channel = addons.getChannel();
  channel.on(VIEWPORT_CHANGED_EVENT_ID, callHandler);
  return () => channel.removeListener(VIEWPORT_CHANGED_EVENT_ID, callHandler);
};

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yep, this should work.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@Hypnosphi do you still need the second example?

Copy link
Member

Choose a reason for hiding this comment

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

As you wish. Or maybe a testcase would be better, just to make sure it doesn't break in future

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I agree, test case is even better. Will add it

const viewportOptions =
typeof storyOptions === 'string' ? { name: storyOptions } : storyOptions;

applyViewportOptions(viewportOptions);
Copy link
Member

Choose a reason for hiding this comment

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

is it needed when there are neither options nor parameters?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

No, not needed in such case

Copy link
Member

Choose a reason for hiding this comment

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

Then you can pass skipIfNoParametersOrOptions: true to makeDecorator


export const Viewport = deprecate(({ children, ...options }) => {
setViewport(options);
applyViewportOptions(options);
return children;
}, `<Viewport> usage is deprecated, use .addDecorator(withViewport(viewport)) instead`);
Copy link
Member

@Hypnosphi Hypnosphi May 20, 2018

Choose a reason for hiding this comment

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

I think it can be now "use .addParameters({viewport}) instead"

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I updated it.
I wasn't aware of such method to be honest :)

@codecov
Copy link

codecov bot commented May 20, 2018

Codecov Report

Merging #3610 into master will increase coverage by 0.03%.
The diff coverage is 27.27%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #3610      +/-   ##
==========================================
+ Coverage   40.02%   40.05%   +0.03%     
==========================================
  Files         481      481              
  Lines        5254     5255       +1     
  Branches      862      864       +2     
==========================================
+ Hits         2103     2105       +2     
+ Misses       2634     2632       -2     
- Partials      517      518       +1
Impacted Files Coverage Δ
addons/viewport/src/preview/withViewport.js 28.57% <27.27%> (+8.57%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update c821484...3c13771. Read the comment docs.

@Hypnosphi Hypnosphi merged commit 55c6f7b into storybookjs:master May 20, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
addon: viewport maintenance User-facing maintenance tasks
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants