Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[state] don't make extra $location.replace() calls #8179

Merged
merged 1 commit into from
Sep 8, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions src/ui/public/state_management/__tests__/state.js
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,23 @@ describe('State Management', function () {
stateObj = state.toObject();
expect(stateObj).to.eql({});
});

it('does not replace the state value on read', () => {
const { state } = setup();
sinon.stub($location, 'search', (newSearch) => {
if (newSearch) {
return $location;
} else {
return {
[state.getQueryParamName()]: '(a:1)'
};
}
});
const replaceStub = sinon.stub($location, 'replace').returns($location);

state.fetch();
sinon.assert.notCalled(replaceStub);
});
});

describe('Hashing', () => {
Expand Down
18 changes: 14 additions & 4 deletions src/ui/public/state_management/state.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,13 +80,19 @@ export default function StateProvider(Private, $rootScope, $location, config) {
$location.search(search).replace();
}

if (risonEncoded) {
if (!risonEncoded) {
return null;
}

if (this.isHashingEnabled()) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because there are so many conditionals and early-exits in this method, I think it's worth adding a comment describing the state of the app in which this condition is reached:

if (this.isHashingEnabled()) {
  // If Kibana has been loaded for the first time with RISON states URL, and the user has has hashed states
  // enabled, then update the URL to show the hashed states instead of the RISON states.
}

// RISON can find its way into the URL any number of ways, including the navbar links or
// shared urls with the entire state embedded. These values need to be translated into
// hashes and replaced in the browser history when state-hashing is enabled
search[this._urlParam] = this.toQueryParam(risonEncoded);
$location.search(search).replace();
return risonEncoded;
}

return null;
return risonEncoded;
};

/**
Expand Down Expand Up @@ -212,13 +218,17 @@ export default function StateProvider(Private, $rootScope, $location, config) {
return rison.encode(this._parseQueryParamValue(hash));
};

State.prototype.isHashingEnabled = function () {
return !!config.get('state:storeInSessionStorage');
};

/**
* Produce the hash version of the state in it's current position
*
* @return {string}
*/
State.prototype.toQueryParam = function (state = this.toObject()) {
if (!config.get('state:storeInSessionStorage')) {
if (!this.isHashingEnabled()) {
return rison.encode(state);
}

Expand Down