From 398e5783c167f3d0860e6d4acd43c0009597c170 Mon Sep 17 00:00:00 2001 From: Mark Stacey Date: Thu, 5 Dec 2024 14:46:44 -0330 Subject: [PATCH] fix: Fix create/update metric fragment The functions `createEventFragment` and `updateEventFragment` of the MetaMetricsController were broken recently in the migration to BaseControllerV2 (#28113). We ended up trying to mutate a piece of Immer state, resulting in an error. The affected lines were updated to use `cloneDeep` prior to mutating, so that we're no longer attempting to mutate a frozen object. Fixes ##28599 --- app/scripts/controllers/metametrics-controller.ts | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/app/scripts/controllers/metametrics-controller.ts b/app/scripts/controllers/metametrics-controller.ts index e286a3a47d02..a9e9b0a4a3b2 100644 --- a/app/scripts/controllers/metametrics-controller.ts +++ b/app/scripts/controllers/metametrics-controller.ts @@ -1,4 +1,5 @@ import { + cloneDeep, isEqual, memoize, merge, @@ -584,7 +585,8 @@ export default class MetaMetricsController extends BaseController< : {}; this.update((state) => { - state.fragments[id] = merge(additionalFragmentProps, fragment); + // @ts-expect-error this is caused by a bug in Immer, not being able to handle recursive types like Json + state.fragments[id] = merge(cloneDeep(additionalFragmentProps), fragment); }); if (fragment.initialEvent) { @@ -669,7 +671,7 @@ export default class MetaMetricsController extends BaseController< } this.update((state) => { - state.fragments[id] = merge(state.fragments[id], { + state.fragments[id] = merge(cloneDeep(state.fragments[id]), { ...payload, lastUpdated: Date.now(), });