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

Running functions conditionally based on the story displayed. #290

Closed
joscha opened this issue Jul 1, 2016 · 6 comments
Closed

Running functions conditionally based on the story displayed. #290

joscha opened this issue Jul 1, 2016 · 6 comments

Comments

@joscha
Copy link
Member

joscha commented Jul 1, 2016

I am currently trying to integrate https://github.com/mrdoob/stats.js in order to add a addMonitored method that would allow specifying a performance monitored story like this:

storiesOf('something', module)
  .addMonitored('a component with monitored performance', () => (
    <Component />
  ), () => Math.random());

I tried like this:

setAddon({
  addMonitored(storyName, storyFn, rafFn) {
    this.add(storyName, (context) => {
      const stats = new Stats();
      stats.showPanel( 0 ); // 0: fps, 1: ms, 2: mb, 3+: custom
      document.getElementById('root').appendChild(stats.dom);
      function animate() {
        stats.begin();
        rafFn();
        stats.end();
        requestAnimationFrame(animate);
      }
      requestAnimationFrame(animate);
      return storyFn(context);
    });
  }
});

It kinda works:
react_storybook

however there are some flaws in that - the function passed to this.add gets called immediately and not only when that particular story is shown, hence the stats window is always visible. Also, the requestAnimationFrame is kicked off immediately. Potentially this could be done by wrapping storyFn(context) with a component that starts the stats on mount, but I am not sure how to achieve this. Any pointers?

@arunoda
Copy link
Member

arunoda commented Jul 2, 2016

Yes you are correct, this.add called immediately. Here's you need to provide a React element. Inside that, render the perfTool, when the component mounted.

@arunoda
Copy link
Member

arunoda commented Jul 2, 2016

@arunoda arunoda closed this as completed Jul 2, 2016
@arunoda arunoda reopened this Jul 2, 2016
@joscha joscha changed the title Runnin functions conditionally based on the story displayed. Running functions conditionally based on the story displayed. Jul 3, 2016
@joscha
Copy link
Member Author

joscha commented Jul 4, 2016

Thank you for your help @arunoda - I successfully created an example here: https://gist.github.com/joscha/9e1d4eea6fddd50df25012d704180aa1

@arunoda
Copy link
Member

arunoda commented Jul 4, 2016

Okay. We are good here right :)
Publish this into NPM and send me a PR to here - https://github.com/kadirahq/react-storybook/blob/master/docs/extensions.md#available-extensions

@ndelangen
Copy link
Member

Did this ever make it into Storybook @joscha ?
Doesn't look like an open issue to me, if it's been open for over a year.

@joscha
Copy link
Member Author

joscha commented Jul 7, 2017

@ndelangen the linked example was sufficient, so closing it is fine! 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants