Skip to content

Commit

Permalink
fix: Fix create/update metric fragment
Browse files Browse the repository at this point in the history
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
  • Loading branch information
Gudahtt committed Dec 5, 2024
1 parent ceeb5b6 commit 398e578
Showing 1 changed file with 4 additions and 2 deletions.
6 changes: 4 additions & 2 deletions app/scripts/controllers/metametrics-controller.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {
cloneDeep,
isEqual,
memoize,
merge,
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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(),
});
Expand Down

0 comments on commit 398e578

Please sign in to comment.