Skip to content

Commit

Permalink
Merge pull request #8179 from spalger/fix/stateReplacement
Browse files Browse the repository at this point in the history
[state] don't make extra $location.replace() calls
  • Loading branch information
spalger authored Sep 8, 2016
2 parents 73c0c26 + 9c52032 commit d2aff6c
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 4 deletions.
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()) {
// 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

0 comments on commit d2aff6c

Please sign in to comment.