From 1ccb297b10807ad9ad3eff657303add7cf3dab9b Mon Sep 17 00:00:00 2001
From: Chris Beer <cabeer@stanford.edu>
Date: Fri, 14 Aug 2020 10:51:18 -0700
Subject: [PATCH] Avoid lodash-merge, which is recursive and can lead to
 surprises; fixes #3240

---
 src/state/reducers/companionWindows.js |  5 ++---
 src/state/reducers/elasticLayout.js    |  3 +--
 src/state/reducers/windows.js          | 13 +++++++------
 3 files changed, 10 insertions(+), 11 deletions(-)

diff --git a/src/state/reducers/companionWindows.js b/src/state/reducers/companionWindows.js
index cd619e6fe..29b42ca1e 100644
--- a/src/state/reducers/companionWindows.js
+++ b/src/state/reducers/companionWindows.js
@@ -1,7 +1,6 @@
 import omit from 'lodash/omit';
 import set from 'lodash/fp/set';
 import update from 'lodash/fp/update';
-import merge from 'lodash/fp/merge';
 import ActionTypes from '../actions/action-types';
 
 /** */
@@ -31,14 +30,14 @@ export function companionWindowsReducer(state = {}, action) {
         return object;
       }, {});
     case ActionTypes.UPDATE_COMPANION_WINDOW:
-      return update([action.id], orig => merge(orig, action.payload), state);
+      return update([action.id], orig => ({ ...(orig || {}), ...action.payload }), state);
 
     case ActionTypes.REMOVE_COMPANION_WINDOW:
       return omit(state, action.id);
     case ActionTypes.IMPORT_MIRADOR_STATE:
       return action.state.companionWindows || [];
     case ActionTypes.TOGGLE_TOC_NODE:
-      return update([action.id, 'tocNodes'], orig => merge(orig || {}, action.payload), state);
+      return update([action.id, 'tocNodes'], orig => ({ ...(orig || {}), ...action.payload }), state);
     default:
       return state;
   }
diff --git a/src/state/reducers/elasticLayout.js b/src/state/reducers/elasticLayout.js
index fbbf9f11a..c35ee12d5 100644
--- a/src/state/reducers/elasticLayout.js
+++ b/src/state/reducers/elasticLayout.js
@@ -1,6 +1,5 @@
 import update from 'lodash/fp/update';
 import omit from 'lodash/omit';
-import merge from 'lodash/fp/merge';
 import ActionTypes from '../actions/action-types';
 
 /**
@@ -17,7 +16,7 @@ export const elasticLayoutReducer = (state = {}, action) => {
       };
 
     case ActionTypes.UPDATE_ELASTIC_WINDOW_LAYOUT:
-      return update([action.windowId], orig => merge(orig, action.payload), state);
+      return update([action.windowId], orig => ({ ...(orig || {}), ...action.payload }), state);
 
     case ActionTypes.REMOVE_WINDOW:
       return omit(state, action.windowId);
diff --git a/src/state/reducers/windows.js b/src/state/reducers/windows.js
index 5d20799ac..37ef215d0 100644
--- a/src/state/reducers/windows.js
+++ b/src/state/reducers/windows.js
@@ -1,6 +1,5 @@
 import update from 'lodash/fp/update';
 import omit from 'lodash/omit';
-import merge from 'lodash/fp/merge';
 import ActionTypes from '../actions/action-types';
 
 /**
@@ -29,7 +28,7 @@ export const windowsReducer = (state = {}, action) => {
       };
 
     case ActionTypes.UPDATE_WINDOW:
-      return update([action.id], orig => merge(orig, action.payload), state);
+      return update([action.id], orig => ({ ...(orig || {}), ...action.payload }), state);
 
     case ActionTypes.REMOVE_WINDOW:
       return omit(state, [action.windowId]);
@@ -72,10 +71,12 @@ export const windowsReducer = (state = {}, action) => {
     case ActionTypes.SET_CANVAS:
       if (!state[action.windowId]) return state;
 
-      return update([action.windowId], orig => merge(orig, {
-        canvasId: action.canvasId,
-        visibleCanvases: action.visibleCanvases || [],
-      }), state);
+      return update([action.windowId], orig => (
+        {
+          ...(orig || {}),
+          canvasId: action.canvasId,
+          visibleCanvases: action.visibleCanvases || [],
+        }), state);
     case ActionTypes.ADD_COMPANION_WINDOW:
       return {
         ...state,
-- 
GitLab