Skip to content

Commit 3e0714c

Browse files
committed
refactor: remove usage of clone deep
1 parent b994c8d commit 3e0714c

12 files changed

Lines changed: 46 additions & 46 deletions

File tree

packages/api-aco/src/utils/ListCache.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import cloneDeep from "lodash/cloneDeep.js";
2-
31
export type Constructor<T> = new (...args: any[]) => T;
42

53
export interface IListCachePredicate<T> {
@@ -34,7 +32,7 @@ export class ListCache<T> implements IListCache<T> {
3432
}
3533

3634
getItems(): T[] {
37-
return cloneDeep(this.state);
35+
return structuredClone(this.state);
3836
}
3937

4038
addItems(items: T[]): void {

packages/api-core/src/features/security/roles/shared/decorators/ListCache.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import cloneDeep from "lodash/cloneDeep.js";
2-
31
export type Constructor<T> = new (...args: any[]) => T;
42

53
export interface IListCachePredicate<T> {
@@ -34,7 +32,7 @@ export class ListCache<T> implements IListCache<T> {
3432
}
3533

3634
getItems(): T[] {
37-
return cloneDeep(this.state);
35+
return structuredClone(this.state);
3836
}
3937

4038
addItems(items: T[]): void {

packages/app-aco/src/components/AdvancedSearch/domain/FilterRepository.ts

Lines changed: 33 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
1-
import cloneDeep from "lodash/cloneDeep.js";
2-
import { makeAutoObservable, runInAction } from "mobx";
1+
import { makeAutoObservable, observable, runInAction, toJS } from "mobx";
32
import { mdbid } from "@webiny/utils";
4-
53
import type { FilterDTO } from "../domain/index.js";
64
import { FilterMapper, Loading, Sorter } from "../domain/index.js";
75
import type { FiltersGatewayInterface } from "../gateways/index.js";
@@ -10,19 +8,20 @@ export class FilterRepository {
108
private gateway: FiltersGatewayInterface;
119
private sorter: Sorter<FilterDTO>;
1210
private loading: Loading;
13-
private filters: FilterDTO[] = [];
11+
private filters;
1412
public readonly namespace: string;
1513

1614
constructor(gateway: FiltersGatewayInterface, namespace: string) {
1715
this.gateway = gateway;
1816
this.loading = new Loading();
1917
this.namespace = namespace;
18+
this.filters = observable.array<FilterDTO>([]);
2019
this.sorter = new Sorter(["createdOn_DESC"]);
2120
makeAutoObservable(this);
2221
}
2322

2423
getFilters() {
25-
return cloneDeep(this.filters);
24+
return structuredClone(toJS(this.filters));
2625
}
2726

2827
getLoading() {
@@ -58,15 +57,23 @@ export class FilterRepository {
5857
}
5958

6059
runInAction(() => {
61-
this.filters = this.sorter.sort(response.map(filter => FilterMapper.toDTO(filter)));
60+
this.filters.replace(
61+
this.sorter.sort(
62+
response.map(filter => {
63+
return FilterMapper.toDTO(filter);
64+
})
65+
)
66+
);
6267
});
6368
}
6469

6570
async getFilterById(id: string) {
66-
const filterInCache = this.filters.find(filter => filter.id === id);
71+
const filterInCache = this.filters.find(filter => {
72+
return filter.id === id;
73+
});
6774

6875
if (filterInCache) {
69-
return cloneDeep(filterInCache);
76+
return structuredClone(toJS(filterInCache));
7077
}
7178

7279
const response = await this.runWithLoading<FilterDTO>(this.gateway.get(id));
@@ -77,10 +84,10 @@ export class FilterRepository {
7784

7885
const filterDTO = FilterMapper.toDTO(response);
7986
runInAction(() => {
80-
this.filters = this.sorter.sort([filterDTO, ...this.filters]);
87+
this.filters.replace(this.sorter.sort([filterDTO, ...toJS(this.filters)]));
8188
});
8289

83-
return cloneDeep(filterDTO);
90+
return structuredClone(toJS(filterDTO));
8491
}
8592

8693
async createFilter(filter: FilterDTO) {
@@ -99,10 +106,10 @@ export class FilterRepository {
99106

100107
const filterDTO = FilterMapper.toDTO(response);
101108
runInAction(() => {
102-
this.filters = this.sorter.sort([filterDTO, ...this.filters]);
109+
this.filters.replace(this.sorter.sort([filterDTO, ...toJS(this.filters)]));
103110
});
104111

105-
return cloneDeep(filterDTO);
112+
return structuredClone(toJS(filterDTO));
106113
}
107114

108115
async updateFilter(filter: FilterDTO) {
@@ -122,14 +129,17 @@ export class FilterRepository {
122129
const filterDTO = FilterMapper.toDTO(response);
123130

124131
runInAction(() => {
125-
this.filters = this.sorter.sort([
126-
...this.filters.slice(0, filterIndex),
127-
{
128-
...this.filters[filterIndex],
129-
...filterDTO
130-
},
131-
...this.filters.slice(filterIndex + 1)
132-
]);
132+
const filters = toJS(this.filters);
133+
this.filters.replace(
134+
this.sorter.sort([
135+
...filters.slice(0, filterIndex),
136+
{
137+
...filters[filterIndex],
138+
...filterDTO
139+
},
140+
...filters.slice(filterIndex + 1)
141+
])
142+
);
133143
});
134144
}
135145
}
@@ -149,7 +159,9 @@ export class FilterRepository {
149159

150160
if (response) {
151161
runInAction(() => {
152-
this.filters = this.sorter.sort(this.filters.filter(filter => filter.id !== id));
162+
this.filters.replace(
163+
this.sorter.sort(toJS(this.filters.filter(filter => filter.id !== id)))
164+
);
153165
});
154166
}
155167
}

packages/app-admin/src/components/IconPicker/IconRepository.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
import cloneDeep from "lodash/cloneDeep.js";
2-
import { makeAutoObservable, runInAction } from "mobx";
3-
1+
import { makeAutoObservable, runInAction, toJS } from "mobx";
42
import { Loading } from "./Loading.js";
53
import type { IconPackProviderInterface as IconPackProvider, IconType } from "./config/index.js";
64
import type { Icon } from "./types.js";
@@ -39,7 +37,7 @@ export class IconRepository {
3937
}
4038

4139
getIcons() {
42-
return cloneDeep(this.icons);
40+
return structuredClone(toJS(this.icons));
4341
}
4442

4543
addIcon(icon: Icon) {

packages/app-headless-cms/src/admin/components/FieldEditor/EditFieldDialog.tsx

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import React, { useMemo, useState } from "react";
2-
import cloneDeep from "lodash/cloneDeep.js";
32
import type { FormOnSubmit } from "@webiny/form";
43
import { Form } from "@webiny/form";
54
import { i18n } from "@webiny/app/i18n/index.js";
@@ -14,7 +13,7 @@ import { EditFieldDrawerContainer } from "./EditFieldDrawerContainer.js";
1413
const t = i18n.namespace("app-headless-cms/admin/components/editor");
1514

1615
function setupState(field: CmsModelField, contentModel: CmsEditorContentModel): EditFieldState {
17-
const clonedField = cloneDeep(field);
16+
const clonedField = structuredClone(field);
1817

1918
if (!clonedField.renderer || !clonedField.renderer.name) {
2019
const [renderPlugin] = useRendererPlugins();

packages/app-headless-cms/src/admin/components/FieldEditor/EditFieldDialog/ValidationTab/ValidatorsList.tsx

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import React, { Fragment } from "react";
22
import { css } from "@emotion/css";
3-
import cloneDeep from "lodash/cloneDeep.js";
43
import debounce from "lodash/debounce.js";
54
import { plugins } from "@webiny/plugins";
65
import { Switch } from "@webiny/ui/Switch/index.js";
@@ -68,10 +67,10 @@ const onEnabledChange = (params: OnEnabledChangeParams): void => {
6867
};
6968

7069
const onFormChange = debounce(({ data, validationValue, onChangeValidation, validatorIndex }) => {
71-
const newValidationValue = cloneDeep(validationValue);
70+
const newValidationValue = structuredClone(validationValue);
7271
newValidationValue[validatorIndex] = {
7372
...newValidationValue[validatorIndex],
74-
...cloneDeep(data)
73+
...structuredClone(data)
7574
};
7675
onChangeValidation(newValidationValue);
7776
}, 200);

packages/app-headless-cms/src/admin/plugins/fieldRenderers/dynamicZone/MultiValueDynamicZone.tsx

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import React from "react";
2-
import cloneDeep from "lodash/cloneDeep.js";
32
import { ReactComponent as DeleteIcon } from "@webiny/icons/delete_outline.svg";
43
import { ReactComponent as CloneIcon } from "@webiny/icons/library_add.svg";
54
import { ReactComponent as ArrowUpIcon } from "@webiny/icons/expand_less.svg";
@@ -210,7 +209,7 @@ export const MultiValueDynamicZone = (props: MultiValueDynamicZoneProps) => {
210209
};
211210

212211
const cloneValue = (value: TemplateValue, index: number) => {
213-
bind.appendValue(cloneDeep(value), index + 1);
212+
bind.appendValue(structuredClone(value), index + 1);
214213
};
215214

216215
const values: TemplateValue[] = bind.value || [];

packages/app-utils/src/features/List/ListQueryParamsRepository.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { makeAutoObservable, runInAction } from "mobx";
1+
import { makeAutoObservable, runInAction, toJS } from "mobx";
22
import {
33
ListQueryParamsRepository as Abstraction,
44
type BaseListParams,
@@ -75,7 +75,7 @@ export class ListQueryParamsRepositoryImpl<TParams extends BaseListParams>
7575
}
7676

7777
private clone(value: TParams): TParams {
78-
return structuredClone ? structuredClone(value) : JSON.parse(JSON.stringify(value));
78+
return structuredClone(toJS(value));
7979
}
8080
}
8181

packages/cli/files/references.json

Lines changed: 1 addition & 1 deletion
Large diffs are not rendered by default.

packages/pulumi/package.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,7 @@
1717
"dependencies": {
1818
"@pulumi/pulumi": "^3.230.0",
1919
"@webiny/project": "0.0.0",
20-
"find-up": "^8.0.0",
21-
"lodash": "^4.18.1"
20+
"find-up": "^8.0.0"
2221
},
2322
"devDependencies": {
2423
"@types/lodash": "^4.17.24",

0 commit comments

Comments
 (0)