From bafe09d820303781d2d507be67983c2747d03ab4 Mon Sep 17 00:00:00 2001 From: Aruna Herath Date: Tue, 23 Aug 2016 22:05:00 +0530 Subject: [PATCH] Call 'onStory' only when a story is updated --- src/modules/api/configs/__tests__/init_api.js | 12 ++++++++++-- src/modules/api/configs/init_api.js | 9 +++++++++ 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/src/modules/api/configs/__tests__/init_api.js b/src/modules/api/configs/__tests__/init_api.js index 550977a0985..607fe0755da 100644 --- a/src/modules/api/configs/__tests__/init_api.js +++ b/src/modules/api/configs/__tests__/init_api.js @@ -63,10 +63,14 @@ describe('manager.api.config.initApi', () => { it('should support to add multiple onStory callback', (done) => { const actions = { api: {}, shortcuts: {} }; + const selectedKind = 'XXdd'; + const selectedStory = 'u8sd'; const reduxStore = { subscribe: sinon.stub(), - getState: () => ({ api: {} }), + getState: () => ({ + api: { selectedKind, selectedStory }, + }), }; const provider = { @@ -91,10 +95,14 @@ describe('manager.api.config.initApi', () => { it('should support a way to remove onStory callback', (done) => { const actions = { api: {}, shortcuts: {} }; + const selectedKind = 'XXdd'; + const selectedStory = 'u8sd'; const reduxStore = { subscribe: sinon.stub(), - getState: () => ({ api: {} }), + getState: () => ({ + api: { selectedKind, selectedStory }, + }), }; const provider = { diff --git a/src/modules/api/configs/init_api.js b/src/modules/api/configs/init_api.js index 561de344ca3..152bef6542c 100644 --- a/src/modules/api/configs/init_api.js +++ b/src/modules/api/configs/init_api.js @@ -29,10 +29,19 @@ export default function (provider, reduxStore, actions) { provider.handleAPI(providerApi); // subscribe to redux store and trigger onStory's callback + let currentKind; + let currentStory; reduxStore.subscribe(function () { const { api } = reduxStore.getState(); if (!api) return; + if (api.selectedKind === currentKind && api.selectedStory === currentStory) { + // No change in the selected story so avoid emitting 'story' + return; + } + + currentKind = api.selectedKind; + currentStory = api.selectedStory; callbacks.emit('story', api.selectedKind, api.selectedStory); // providerApi._onStoryCallback(api.selectedKind, api.selectedStory); });