Add STORY_STATE_CHANGED event

This commit is contained in:
Tom Coleman 2020-02-11 17:44:16 +11:00
parent 90adc9e373
commit 8b08dc2080
3 changed files with 20 additions and 0 deletions

View File

@ -93,6 +93,21 @@ describe('preview.story_store', () => {
store.setStoryState('a--1', { baz: 'bing' }); store.setStoryState('a--1', { baz: 'bing' });
expect(store.getRawStory('a', '1').state).toEqual({ foo: 'bar', baz: 'bing' }); expect(store.getRawStory('a', '1').state).toEqual({ foo: 'bar', baz: 'bing' });
}); });
it('synchronously emits STORY_STATE_CHANGED if different', () => {
const onStateChanged = jest.fn();
const testChannel = createChannel({ page: 'preview' });
testChannel.on(Events.STORY_STATE_CHANGED, onStateChanged);
const store = new StoryStore({ channel: testChannel });
addStoryToStore(store, 'a', '1', () => 0);
store.setStoryState('a--1', { foo: 'bar' });
expect(onStateChanged).toHaveBeenCalledWith('a--1', { foo: 'bar' });
store.setStoryState('a--1', { baz: 'bing' });
expect(onStateChanged).toHaveBeenCalledWith('a--1', { foo: 'bar', baz: 'bing' });
});
}); });
describe('storySort', () => { describe('storySort', () => {

View File

@ -349,8 +349,11 @@ export default class StoryStore extends EventEmitter {
} }
setStoryState(id: string, newState: StoryState) { setStoryState(id: string, newState: StoryState) {
if (!this._stories[id]) throw new Error(`No story for id ${id}`);
const { state } = this._stories[id]; const { state } = this._stories[id];
this._stories[id].state = { ...state, ...newState }; this._stories[id].state = { ...state, ...newState };
this._channel.emit(Events.STORY_STATE_CHANGED, id, this._stories[id].state);
} }
// This API is a reimplementation of Storybook's original getStorybook() API. // This API is a reimplementation of Storybook's original getStorybook() API.

View File

@ -10,6 +10,7 @@ enum events {
STORY_ADDED = 'storyAdded', STORY_ADDED = 'storyAdded',
STORY_CHANGED = 'storyChanged', STORY_CHANGED = 'storyChanged',
STORY_UNCHANGED = 'storyUnchanged', STORY_UNCHANGED = 'storyUnchanged',
STORY_STATE_CHANGED = 'storyStateChanged',
FORCE_RE_RENDER = 'forceReRender', FORCE_RE_RENDER = 'forceReRender',
REGISTER_SUBSCRIPTION = 'registerSubscription', REGISTER_SUBSCRIPTION = 'registerSubscription',
STORY_INIT = 'storyInit', STORY_INIT = 'storyInit',
@ -44,6 +45,7 @@ export const {
REGISTER_SUBSCRIPTION, REGISTER_SUBSCRIPTION,
STORY_INIT, STORY_INIT,
STORY_ADDED, STORY_ADDED,
STORY_STATE_CHANGED,
STORY_RENDER, STORY_RENDER,
STORY_RENDERED, STORY_RENDERED,
STORY_MISSING, STORY_MISSING,