From 2c1ea3ee593b051a4afc4e5be50ccc6bc6ab439d Mon Sep 17 00:00:00 2001 From: Joe Fleming Date: Thu, 25 Aug 2016 14:07:37 -0700 Subject: [PATCH] long live setInitialState --- src/plugins/kibana/public/dashboard/index.js | 2 +- .../public/discover/controllers/discover.js | 2 +- .../kibana/public/visualize/editor/editor.js | 2 +- .../__tests__/state_monitor_factory.js | 14 +++++----- .../state_management/state_monitor_factory.js | 26 +++++++++---------- 5 files changed, 23 insertions(+), 23 deletions(-) diff --git a/src/plugins/kibana/public/dashboard/index.js b/src/plugins/kibana/public/dashboard/index.js index 61b6c8d14a2e8..fa919fe9ffdfa 100644 --- a/src/plugins/kibana/public/dashboard/index.js +++ b/src/plugins/kibana/public/dashboard/index.js @@ -204,7 +204,7 @@ define(function (require) { dash.save() .then(function (id) { - stateMonitor.setDefaultState($state.toJSON()); + stateMonitor.setInitialState($state.toJSON()); $scope.configTemplate.close('save'); if (id) { notify.info('Saved Dashboard as "' + dash.title + '"'); diff --git a/src/plugins/kibana/public/discover/controllers/discover.js b/src/plugins/kibana/public/discover/controllers/discover.js index 9366d0a9eea6b..f5496917742db 100644 --- a/src/plugins/kibana/public/discover/controllers/discover.js +++ b/src/plugins/kibana/public/discover/controllers/discover.js @@ -293,7 +293,7 @@ define(function (require) { return savedSearch.save() .then(function (id) { - stateMonitor.setDefaultState($state.toJSON()); + stateMonitor.setInitialState($state.toJSON()); $scope.configTemplate.close('save'); if (id) { diff --git a/src/plugins/kibana/public/visualize/editor/editor.js b/src/plugins/kibana/public/visualize/editor/editor.js index 69b2b9f56646b..3a86c6046d5a9 100644 --- a/src/plugins/kibana/public/visualize/editor/editor.js +++ b/src/plugins/kibana/public/visualize/editor/editor.js @@ -243,7 +243,7 @@ define(function (require) { savedVis.save() .then(function (id) { - stateMonitor.setDefaultState($state.toJSON()); + stateMonitor.setInitialState($state.toJSON()); configTemplate.close('save'); if (id) { diff --git a/src/ui/public/state_management/__tests__/state_monitor_factory.js b/src/ui/public/state_management/__tests__/state_monitor_factory.js index 9565dba500449..d7663cf3c9045 100644 --- a/src/ui/public/state_management/__tests__/state_monitor_factory.js +++ b/src/ui/public/state_management/__tests__/state_monitor_factory.js @@ -4,7 +4,7 @@ import { cloneDeep } from 'lodash'; import stateMonitor from 'ui/state_management/state_monitor_factory'; import SimpleEmitter from 'ui/utils/SimpleEmitter'; -describe('stateMonitor', function () { +describe('stateMonitorFactory', function () { const noop = () => {}; const eventTypes = [ 'save_with_changes', @@ -135,7 +135,7 @@ describe('stateMonitor', function () { }); }); - describe('setDefaultState', function () { + describe('setInitialState', function () { let changeStub; beforeEach(() => { @@ -145,22 +145,22 @@ describe('stateMonitor', function () { }); it('should throw if no state is provided', function () { - const fn = () => monitor.setDefaultState(); + const fn = () => monitor.setInitialState(); expect(fn).to.throwException(/must be an object/); }); it('should throw if given the wrong type', function () { - const fn = () => monitor.setDefaultState([]); + const fn = () => monitor.setInitialState([]); expect(fn).to.throwException(/must be an object/); }); it('should trigger the onChange handler', function () { - monitor.setDefaultState({ new: 'state' }); + monitor.setInitialState({ new: 'state' }); sinon.assert.calledOnce(changeStub); }); it('should change the status with differing state', function () { - monitor.setDefaultState({ new: 'state' }); + monitor.setInitialState({ new: 'state' }); sinon.assert.calledOnce(changeStub); const status = changeStub.firstCall.args[0]; @@ -169,7 +169,7 @@ describe('stateMonitor', function () { }); it('should not trigger the onChange handler without state change', function () { - monitor.setDefaultState(cloneDeep(mockState.toJSON())); + monitor.setInitialState(cloneDeep(mockState.toJSON())); sinon.assert.notCalled(changeStub); }); }); diff --git a/src/ui/public/state_management/state_monitor_factory.js b/src/ui/public/state_management/state_monitor_factory.js index 4641ceb99ced5..74c85a22ef2af 100644 --- a/src/ui/public/state_management/state_monitor_factory.js +++ b/src/ui/public/state_management/state_monitor_factory.js @@ -8,13 +8,13 @@ function stateMonitor(state, customInitialState) { let destroyed = false; let ignoredProps = []; let changeHandlers = []; - let originalState; + let initialState; - setOriginalState(customInitialState); + setInitialState(customInitialState); - function setOriginalState(initialState) { + function setInitialState(customInitialState) { // state.toJSON returns a reference, clone so we can mutate it safely - originalState = cloneDeep(initialState) || cloneDeep(state.toJSON()); + initialState = cloneDeep(customInitialState) || cloneDeep(state.toJSON()); } function removeIgnoredProps(state) { @@ -27,7 +27,7 @@ function stateMonitor(state, customInitialState) { function getStatus() { // state.toJSON returns a reference, clone so we can mutate it safely const currentState = removeIgnoredProps(cloneDeep(state.toJSON())); - const isClean = isEqual(currentState, originalState); + const isClean = isEqual(currentState, initialState); return { clean: isClean, @@ -55,22 +55,22 @@ function stateMonitor(state, customInitialState) { }; return { - setDefaultState(customInitialState) { + setInitialState(customInitialState) { // check the current status - const currentStatus = getStatus(); + const previousStatus = getStatus(); - // update the originalState and apply ignoredProps + // update the initialState and apply ignoredProps if (!isPlainObject(customInitialState)) throw new TypeError('The default state must be an object'); - setOriginalState(customInitialState); - removeIgnoredProps(originalState); + setInitialState(customInitialState); + removeIgnoredProps(initialState); - // fire the change handler - if (!isEqual(currentStatus, getStatus())) dispatchChange(); + // fire the change handler if the status has changed + if (!isEqual(previousStatus, getStatus())) dispatchChange(); }, ignoreProps(props) { ignoredProps = ignoredProps.concat(props); - removeIgnoredProps(originalState); + removeIgnoredProps(initialState); return this; },