Skip to content

Commit

Permalink
Merge pull request #1247 from scireum/feature/sbi/OX-10141
Browse files Browse the repository at this point in the history
Continuity: Only calls state handler restore when entry is actually changed
  • Loading branch information
sabieber authored Jul 5, 2023
2 parents 88288d1 + 67b3cd3 commit 3f70af5
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 2 deletions.
41 changes: 41 additions & 0 deletions src/main/resources/default/assets/common/core.js.pasta
Original file line number Diff line number Diff line change
Expand Up @@ -521,3 +521,44 @@ sirius.isDragleaveEventLeavingWindow = function (event) {
}
return false;
}

/**@
* Compares the two given objects for equality, allowing for nested objects.
*
* @param object1 the first object to compare
* @param object2 the second object to compare
* @returns {boolean} true if the objects are equal, false otherwise
*/
sirius.areObjectsDeeplyEqual = function (object1, object2) {
const keys1 = Object.keys(object1);
const keys2 = Object.keys(object2);

if (keys1.length !== keys2.length) {
return false;
}

// var is used here instead of const to keep the code compatible with IE11.
for (var key of keys1) {
const value1 = object1[key];
const value2 = object2[key];
const areObjects = sirius.isObject(value1) && sirius.isObject(value2);
if (areObjects && !sirius.areObjectsDeeplyEqual(value1, value2)) {
return false;
}
if (!areObjects && value1 !== value2) {
return false;
}
}

return true;
}

/**@
* Checks if the given value is an object.
*
* @param object the value to check
* @returns {boolean} true if the value is an object, false otherwise
*/
sirius.isObject = function (object) {
return object != null && typeof object === 'object';
}
Original file line number Diff line number Diff line change
Expand Up @@ -137,8 +137,9 @@ window.sirius.Continuity = (function () {
* @param {boolean} dontSetLastState whether the internal last state should be replaced by the current history state (before the new one became active)
*/
Continuity.prototype.handleStateChange = function (state, dontSetLastState) {
let stateBeforeChange = Object.assign({}, this.currentState);
if (!dontSetLastState) {
this.lastState = Object.assign({}, this.currentState);
this.lastState = stateBeforeChange;
}
this.currentState = state;

Expand All @@ -148,7 +149,12 @@ window.sirius.Continuity = (function () {
if (handler) {
const entry = state[entryKey];
me.log('[CONTINUITY] [INTERNAL] State handler of type "%s" called for entry: %o', entryKey, entry);
handler.restore.call(me, entry);
if (!sirius.areObjectsDeeplyEqual(stateBeforeChange[entryKey], entry)) {
// We only call the handler if the specific entry has actually changed.
handler.restore.call(me, entry);
} else {
me.log('[CONTINUITY] [INTERNAL] State handler of type "%s" called for unchanged entry: %o', entryKey, entry);
}
} else {
me.log('[CONTINUITY] [INTERNAL] State handler of type "%s" not found!', entryKey);
}
Expand Down

0 comments on commit 3f70af5

Please sign in to comment.