BVB Source Codes

storybook Show client_api.js Source code

Return Download storybook: download client_api.js Source code - Download storybook Source code - Type:.js
  1. export default class ClientApi {
  2.   constructor({ channel, storyStore }) {
  3.     // channel can be null when running in node
  4.     // always check whether channel is available
  5.     this._channel = channel;
  6.     this._storyStore = storyStore;
  7.     this._addons = {};
  8.     this._globalDecorators = [];
  9.   }
  10.  
  11.   setAddon(addon) {
  12.     this._addons = {
  13.       ...this._addons,
  14.       ...addon,
  15.     };
  16.   }
  17.  
  18.   addDecorator(decorator) {
  19.     this._globalDecorators.push(decorator);
  20.   }
  21.  
  22.   clearDecorators() {
  23.     this._globalDecorators = [];
  24.   }
  25.  
  26.   storiesOf(kind, m) {
  27.     if (!kind && typeof kind !== 'string') {
  28.       throw new Error('Invalid or missing kind provided for stories, should be a string');
  29.     }
  30.  
  31.     if (m && m.hot) {
  32.       m.hot.dispose(() => {
  33.         this._storyStore.removeStoryKind(kind);
  34.       });
  35.     }
  36.  
  37.     const localDecorators = [];
  38.     const api = {
  39.       kind,
  40.     };
  41.  
  42.     // apply addons
  43.     Object.keys(this._addons).forEach(name => {
  44.       const addon = this._addons[name];
  45.       api[name] = (...args) => {
  46.         addon.apply(api, args);
  47.         return api;
  48.       };
  49.     });
  50.  
  51.     api.add = (storyName, getStory) => {
  52.       if (typeof storyName !== 'string') {
  53.         throw new Error(`Invalid or missing storyName provided for a "${kind}" story.`);
  54.       }
  55.  
  56.       if (this._storyStore.hasStory(kind, storyName)) {
  57.         throw new Error(`Story of "${kind}" named "${storyName}" already exists`);
  58.       }
  59.  
  60.       // Wrap the getStory function with each decorator. The first
  61.       // decorator will wrap the story function. The second will
  62.       // wrap the first decorator and so on.
  63.       const decorators = [...localDecorators, ...this._globalDecorators];
  64.  
  65.       const fn = decorators.reduce(
  66.         (decorated, decorator) => context => decorator(() => decorated(context), context),
  67.         getStory
  68.       );
  69.  
  70.       // Add the fully decorated getStory function.
  71.       this._storyStore.addStory(kind, storyName, fn);
  72.       return api;
  73.     };
  74.  
  75.     api.addDecorator = decorator => {
  76.       localDecorators.push(decorator);
  77.       return api;
  78.     };
  79.  
  80.     return api;
  81.   }
  82.  
  83.   getStorybook() {
  84.     return this._storyStore.getStoryKinds().map(kind => {
  85.       const stories = this._storyStore.getStories(kind).map(name => {
  86.         const render = this._storyStore.getStory(kind, name);
  87.         return { name, render };
  88.       });
  89.       return { kind, stories };
  90.     });
  91.   }
  92. }
  93.  
downloadclient_api.js Source code - Download storybook Source code
Related Source Codes/Software:
ionicons - The premium icon font for Ionic ... 2017-06-07
AsyncDisplayKit - Smooth asynchronous user interfaces for iOS apps. ... 2017-06-07
lottie-android - Render After Effects animations natively on Androi... 2017-06-07
parse-server - Parse-compatible API server module for Node/Expres... 2017-06-07
prettier - Prettier is an opinionated JavaScript formatter. ... 2017-06-08
inferno - An extremely fast, React-like JavaScript library f... 2017-06-08
guetzli - Perceptual JPEG encoder 2017-06-08
cs-video-courses - List of Computer Science courses with video lectur... 2017-06-08
interviews - Everything you need to know to get the job. 2017-06-08
CRYENGINE - CRYENGINE is a powerful real-time game development... 2017-06-11
postal - 2017-06-11
reactide - Reactide is the first dedicated IDE for React web ... 2017-06-11
rkt - rkt is a pod-native container engine for Linux. It... 2017-06-11
uWebSockets - Tiny WebSockets https://for... 2017-06-11
realworld - TodoMVC for the RealWorld - Exemplary fullstack Me... 2017-06-11
goreplay - GoReplay is an open-source tool for capturing and ... 2017-06-10
pyenv - Simple Python version management 2017-06-10
redux-saga - An alternative side effect model for Redux apps ... 2017-06-10
angular-starter - 2017-06-10

 Back to top