Skip to content

Commit

Permalink
long live setInitialState
Browse files Browse the repository at this point in the history
  • Loading branch information
w33ble committed Aug 25, 2016
1 parent 0f9bb2f commit 2c1ea3e
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 23 deletions.
2 changes: 1 addition & 1 deletion src/plugins/kibana/public/dashboard/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 + '"');
Expand Down
2 changes: 1 addition & 1 deletion src/plugins/kibana/public/discover/controllers/discover.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion src/plugins/kibana/public/visualize/editor/editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down Expand Up @@ -135,7 +135,7 @@ describe('stateMonitor', function () {
});
});

describe('setDefaultState', function () {
describe('setInitialState', function () {
let changeStub;

beforeEach(() => {
Expand All @@ -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];
Expand All @@ -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);
});
});
Expand Down
26 changes: 13 additions & 13 deletions src/ui/public/state_management/state_monitor_factory.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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,
Expand Down Expand Up @@ -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;
},

Expand Down

0 comments on commit 2c1ea3e

Please sign in to comment.