From add096419ba82afbbb9b0c608a0012cea1fcd969 Mon Sep 17 00:00:00 2001 From: Tom Coleman Date: Tue, 20 Feb 2018 15:22:29 +1100 Subject: [PATCH 1/3] ClientApi doesn't actually use the channel, so let's not require it --- lib/core/src/client/preview/client_api.js | 9 +-------- 1 file changed, 1 insertion(+), 8 deletions(-) diff --git a/lib/core/src/client/preview/client_api.js b/lib/core/src/client/preview/client_api.js index c159ee1ec511..9f268e783621 100644 --- a/lib/core/src/client/preview/client_api.js +++ b/lib/core/src/client/preview/client_api.js @@ -11,14 +11,7 @@ const defaultDecorateStory = (getStory, decorators) => ); export default class ClientApi { - constructor({ - channel, - storyStore = new StoryStore(), - decorateStory = defaultDecorateStory, - } = {}) { - // channel can be null when running in node - // always check whether channel is available - this._channel = channel; + constructor({ storyStore = new StoryStore(), decorateStory = defaultDecorateStory } = {}) { this._storyStore = storyStore; this._addons = {}; this._globalDecorators = []; From 7a70d5c650b4e4ced28d13effa9d6a38ac90a8c5 Mon Sep 17 00:00:00 2001 From: Tom Coleman Date: Tue, 20 Feb 2018 15:41:13 +1100 Subject: [PATCH 2/3] Use the full `ClientApi` from `@storybook/react-native` --- app/react-native/src/index.js | 1 + app/react-native/src/preview/index.js | 38 +++------------------- app/react-native/src/preview/story_kind.js | 32 ------------------ 3 files changed, 6 insertions(+), 65 deletions(-) delete mode 100644 app/react-native/src/preview/story_kind.js diff --git a/app/react-native/src/index.js b/app/react-native/src/index.js index f197d31b91b0..21298b6d5241 100644 --- a/app/react-native/src/index.js +++ b/app/react-native/src/index.js @@ -11,6 +11,7 @@ const preview = new Preview(); export const storiesOf = preview.storiesOf.bind(preview); export const setAddon = preview.setAddon.bind(preview); export const addDecorator = preview.addDecorator.bind(preview); +export const clearDecorators = preview.clearDecorators.bind(preview); export const configure = preview.configure.bind(preview); export const getStorybook = preview.getStorybook.bind(preview); export const getStorybookUI = preview.getStorybookUI.bind(preview); diff --git a/app/react-native/src/preview/index.js b/app/react-native/src/preview/index.js index fd4e157c616d..3ba9518d8939 100644 --- a/app/react-native/src/preview/index.js +++ b/app/react-native/src/preview/index.js @@ -6,8 +6,7 @@ import parse from 'url-parse'; import addons from '@storybook/addons'; import createChannel from '@storybook/channel-websocket'; import { EventEmitter } from 'events'; -import { StoryStore } from '@storybook/core/client'; -import StoryKindApi from './story_kind'; +import { StoryStore, ClientApi } from '@storybook/core/client'; import OnDeviceUI from './components/OnDeviceUI'; import StoryView from './components/StoryView'; @@ -15,26 +14,12 @@ export default class Preview { constructor() { this._addons = {}; this._decorators = []; - this._stories = new StoryStore(); this._events = new EventEmitter(); - } - - storiesOf(kind, module) { - if (module && module.hot) { - // TODO remove the kind on dispose - } + this._clientApi = new ClientApi({ storyStore: new StoryStore() }); - const fileName = module ? module.filename : null; - - return new StoryKindApi(this._stories, this._addons, this._decorators, kind, fileName); - } - - setAddon(addon) { - Object.assign(this._addons, addon); - } - - addDecorator(decorator) { - this._decorators.push(decorator); + ['storiesOf', 'setAddon', 'addDecorator', 'clearDecorators', 'getStorybook'].forEach(method => { + this[method] = this._clientApi[method].bind(this._clientApi); + }); } configure(loadStories, module) { @@ -45,19 +30,6 @@ export default class Preview { } } - getStorybook() { - return this._stories.getStoryKinds().map(kind => { - const fileName = this._stories.getStoryFileName(kind); - - const stories = this._stories.getStories(kind).map(name => { - const render = this._stories.getStory(kind, name); - return { name, render }; - }); - - return { kind, fileName, stories }; - }); - } - getStorybookUI(params = {}) { return () => { let webUrl = null; diff --git a/app/react-native/src/preview/story_kind.js b/app/react-native/src/preview/story_kind.js deleted file mode 100644 index 01024277cac4..000000000000 --- a/app/react-native/src/preview/story_kind.js +++ /dev/null @@ -1,32 +0,0 @@ -/* eslint no-underscore-dangle: 0 */ - -export default class StoryKindApi { - constructor(stories, addons, decorators, kind, fileName) { - this.kind = kind; - this._stories = stories; - this._decorators = decorators.slice(); - this._fileName = fileName; - Object.assign(this, addons); - } - - addDecorator(decorator) { - this._decorators.push(decorator); - return this; - } - - add(story, fn) { - const decorated = this._decorate(fn); - this._stories.addStory(this.kind, story, decorated, this._fileName); - return this; - } - - _decorate(fn) { - return this._decorators.reduce( - (decorated, decorator) => context => { - const _fn = () => decorated(context); - return decorator(_fn, context); - }, - fn - ); - } -} From e8ddefcc7155e999a61249fc548048c45c15cbe1 Mon Sep 17 00:00:00 2001 From: Tom Coleman Date: Tue, 20 Feb 2018 16:02:52 +1100 Subject: [PATCH 3/3] Missed how this was being used --- app/react-native/src/preview/index.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/app/react-native/src/preview/index.js b/app/react-native/src/preview/index.js index 3ba9518d8939..742945492120 100644 --- a/app/react-native/src/preview/index.js +++ b/app/react-native/src/preview/index.js @@ -15,7 +15,8 @@ export default class Preview { this._addons = {}; this._decorators = []; this._events = new EventEmitter(); - this._clientApi = new ClientApi({ storyStore: new StoryStore() }); + this._stories = new StoryStore(); + this._clientApi = new ClientApi({ storyStore: this._stories }); ['storiesOf', 'setAddon', 'addDecorator', 'clearDecorators', 'getStorybook'].forEach(method => { this[method] = this._clientApi[method].bind(this._clientApi);